饮冰三年-人工智能-linux-49-shell编程习题
1:编写Shell程序,首先显示提示信息:“below is the information of command line”,然后显示命令行中的程序名称,接着一行显示实际给出的所有实参字符串,最后一行再显示命令行上参数的个数。
echo "below is the infomaion of command line" echo $0 echo $* echo $#
2编写Shell程序,首先显示进程状态信息,接着把值12345赋给变量A1,然后输出一行信息“the value of A1 is: ”,然后输出变量A1的值。
#!/bin/bash #!/bin/sh ps -ef A1=12345 echo "the value of A1 is:" echo $A1
3编写Shell程序test4,首先着输出一行提示信息“please input a integer”,然后从键盘读入一个整数,接着显示该整数值,最后显示文件3.sh的内容。
echo “please input a integer!” read x echo $x cat 3.sh
4编写Shell程序,首先显示一行信息:“input x and y”,然后从键盘读入两个整数,接着显示“the sum of two input is ”,然后把两个数的和输出到屏幕上,最后输出系统当前日期和时间信息。
echo "input x and y" read X Y echo "the sum of two input is" ((s=X+Y)) echo $s date
5编写Shell程序,首先输出一行“this is the information of users”的提示信息,接着查看当前注册的用户信息,然后查找目录/bin中的所有以字母l开头的文件,最后显示当前工作路径。
echo "this is the information of users" who find /bin/ -name "i*" -print pwd
6编写Shell程序,首先输出提示信息"please input x",然后从键盘读入一个整数,接着判断该数是否大于11,如果是则输出2012年12月份的月历,否则输出2013年11月份的月历。
echo "please input x" read x if ((x>=11)) then cal 12 2012 else cal 11 2012 fi
7编写Shell程序,首先输出一行提示信息:“please input a integer ”,接着从键盘输入一个整数,然后把该整数乘以10之后的值输出。
echo "please input a number " read x ((x=x*10)) echo $x
8编写Shell程序,首先输出一行提示信息:“hello world”,接着给变量A赋值54321,然后把变量A的值减去10,接着输出变量A的值,最后显示该文件的内容。
echo "hello world " A=54321 ((A=A-10)) echo $A cat 8.sh
9编写Shell程序,首先显示日期和时间,然后显示当前工作目录,接着添加一个名为ecnomics的组,最后查找/usr目录下字母m开头的文件。
date
pwd
groupadd ecnomics
find /usr -name “m*”
10 编写Shell程序,首先显示命令ps的帮助信息,然后查看当前注册用户,查询域名 HYPERLINK "http://www.ntu.edu.cn" www.ntu.edu.cn 对应的IP地址,最后显示“all done”信息。
man ps
w
nslookup www.ntu.edu.cn
echo “all done “
11编写Shell程序,首先输出一行“hello everybody”的提示信息,接着添加一个名为xyz的用户信息,然后查找目录/proc中的所有以字母c开头的文件,最后删除名为yyy的文件。
echo “hello everybodyman” useradd xyz find /proc -name “c*” rm -f yyy
12编写Shell程序test4,首先显示当前与主机连接的主机名和网卡硬件信息,然后查询域名 HYPERLINK "http://www.edn.cn" www.edn.cn 对应的IP地址,接着查找/bin目录下属于用户root的文件,最后显示文件test4的内容。
arp nslookup www.edu.cn find /bin -user root cat test4
13编写Shell程序,使其执行时在屏幕上输出整数1~50,但其中位于20和30之间的偶数不输出。
for ((i=1;i<=50;i++)) do if ((i>=20 && i<=30 && i%2==0)) then continue fi echo $i done
14编写Shell程序,在屏幕上输出10行,第一行输出1个“*”号,第二行输出3个“*”号,第二行输出5个“*”号,依此类推。
for ((i=1;i<=10;i++)) do for ((j=1;j<=2*i-1;j++)) do echo -n "*" done echo done
15编写Shell程序,输入一批正整数(以0为结束标志),请统计其中奇数的个数,并把统计结果输出。
sum=0 echo "please enter the interger,ended by 0" read num while((num!=0)) do if((num%2==1)) then ((sum=sum+1)) fi read num done echo "the number of ji shu is $sum"
16 编写Shell程序,求1*1+2*2+3*3+…….+50*50的总和。
sum=0 for ((i=1;i<=50;i++)) do ((ji=i*i)) ((sum=sum+ji)) done echo $sum
17编写Shell程序,列出等差数列1,3,5,7,9,…的前20项及其总和。(上机)
n=1 s=0 for ((i=1;i<=20;i++)) do echo $n ((s=s+n)) ((n=n+2)) done echo "sum=$s"
18编写Shell程序,把100分转换为5级计分制,从键盘输入百分值,根据情况,显示具体的等级,其中90-100之间对应A,80-89之间对应B,70-79之间对应C,60-69之间对应D,60以下对应E。
echo "please input x=" read x if(($x<=100 && $x>=90)) then echo "A" else if(($x>=80 && $x<=89)) then echo "B" else if(($x>=70 && $x<=79)) then echo "C" else if(($x>=60 && $x<=69)) then echo "D" else echo "E" fi fi fi fi
19编写Shell程序,在屏幕上输出如下格式的九九表。
1*1=1
2*1=2 2*2=4
……………….
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
for ((i=1;i<=9;i++)) do for ((j=1;j<=i;j++)) do ((b=i*j)) echo -n "$i*$j=$b " done echo done
20编写程序test5,对命令行传给它的数字参数求和并显示结果,例如
用户键入test5 10 20 30回车,则显示如下:
10 + 20 + 30 = 60
count=$# s=0 i=1 while [ $i -le $count ] do ((i=i+1)) echo -n "$1" ((s=s+$1)) shift if [ $i -le $count ] then echo -n "+" fi done echo "=$s"
21编写程序test5,对命令行传给它的数字参数求积并显示结果,例如用户键入test5 2 4 3后回车,则屏幕显示如下:2 * 4 * 3 = 24。
count=$# s=1 i=1 while [ $i -le $count ] do ((i=i+1)) echo -n "$1" ((s=s*$1)) shift if [ $i -le $count ] then echo -n "*" fi done echo "=$s"
22编写Shell程序test5,使之从命令行接受数字,输出最大值,例如,键入test5 1 2 3,则屏幕显示:the largest number is : 3。
n=$# if (($#<1)) then echo "error number" exit else max=$1 while ((n>=1)) do if (($1>=max)) then max=$1 fi shift ((n=n-1)) done echo " the largest number is :$max" fi
23编写程序test5,使之从命令行接受数字,输出最小值,例如,键入test5 1 2 3屏幕显示:the smallest number is : 1。
n=$# if (($#<1)) then echo "error number" exit else min=$1 while ((n>=1)) do if (($1<=min)) then min=$1 fi shift ((n=n-1)) done echo " the smallest number is : $min" fi
24编写Shell程序,求1!+ 2!+ 3!+….+ 10!的值。
s=0 echo "please input number:" read x for ((i=1;i<=x;i++)) do t=1 for ((j=1;j<=i;j++)) do ((t=t*j)) done ((s=s+t)) done echo "the sum is $s" PAGE \* MERGEFORMAT8 PAGE PAGE 8

浙公网安备 33010602011771号