当前位置:文档之家› Linux上机练习题shell程序设计基础

Linux上机练习题shell程序设计基础

Linux上机练习题shell程序设计基础
Linux上机练习题shell程序设计基础

上机实习题:

1.用Shell编程,判断一文件是不是字符设备文件,如果是将其拷贝到 /dev

目录下。a

#!/bin/bash

directory=/dev

for file in /bin/a2p

do

if [ -f $file ]

then

cp $file $directory/$file.bak

Fi

done

2.编写一个 shell 脚本,完成功能: 1)显示文字“Waiting for a while….”

2)长格式显示当前目录下面的文件和目录,并输出重定向到

/home/file.txt 文件 3)定义一个变量,名为 s,初始值“Hello” 4)使该变量输出重定向到/home/string.txt 文件 m

#!/bin/bash

echo "waiting for a while… "

ls -l.>/home/file.txt

s=hello

echo $s > /home/string.txt

3.编写一个 shell 脚本,它把第二个位置参数及其以后的各个参数指定的文

件复制到第一个位置参数指定的目录中。b

#!/bin/bash

dir=$1

shift

while [$1]

do

file=$1

cp $1 $dir

shift

done

ls $dir

4.编写一个 shell 脚本,利用 for 循环将当前目录下的.c 文件移动到指定

的目录,并按文件大小显示出移动后指定的目录的内容。d

#!/bin/bash

for file in *.c

{

mv /$file/bin/a

}

ls -lS /bin/a

5.利用数组形式存放 10 个城市的名字,然后利用 for 循环把它们打印出

来。c

#!/bin/bash

city=(jinan,qingdao,rizhao,weifang,beijing,shanghai,shenzhen,guangzhou,chongqi ng,nanjing)

for i in ${name[*]}

do

echo $i

done

6.设计一个shell程序,添加一个新组为class1,然后添加属于这个组的30

个用户,用户名的形式为stdxx,其中xx从01到30。 f

!/bin/bash

groupadd class1

for ((i=1;i<=30;i++))

do

if [ $i -lt 10 ];then

username="std0"$i

else

username="std"$i

fi

useradd -G class1 $username

done

7.编写shell程序,实现自动删除50个账号的功能。账号名为stud1至stud50。

#!/bin/bash

i=1

while [$i-lt10];do

if [$i-ne10];then

uesrdel stud0${i}

else

usedel stud${i}

fi

i='expr $i+1'

done

8.请写出下列程序在命令行执行#./task3 100 Fran USA Eng China

运行后的结果(程序名为task3)。

#!/bin/bash

echo “received $# params.”

echo “program is:$0”

echo “Arg1 is:$1”

shift

shift

echo “Arg2 is:$1”

echo “Arg3 is:$3”

9.设计一个菜单驱动程序。如下:

Use one of the following options:

P: To display current directory

S: To display the name of running file

D: To display today’s date and present time

L: To see the listing of files in your present working directory W: To see who is logged in

Q: To quit this program

Enter your option and hit :

菜单程序将根据用户输入的选择项给出相应信息。要求对用户的输入忽略大小写,对于无效选项的输入给出相应提示。要求使用case语句实现以上功能,输入响应的字母后应该执行响应的命令完成每项功能,如输入P,就执行pwd命令。

#! /bin/sh

# mymenu.sh

echo "use one of the following options:"

echo "D:To display today's date and present time"

echo "L:To see the listing of files in youy present working directory"

echo "W:To see who is logged in"

echo "Q:To quit the program"

read option

case "$option" in

D) date;;

L) ls;;

W) who;;

Q) exit 0;;

*) echo "invalid option:try running the program again" exit 1;;

esac

exit 0

~

相关主题
文本预览
相关文档 最新文档