文章目录
一、条件判断语句
(1)创建一个简单的shell程序,输入学生的成绩,给出该成绩对应的等级,大于90分为A,80-89为B,70-79为C,60-69为D,小于60分为E。要求使用if…elif….else fi实现。
vim t4.sh
,编写内容如下:
#!/bin/bash
num=$# # get the number of parameter
if [ $num -lt 1 ]; then
echo "The number of paramter is less than one!"
else
if [ $1 -lt 0 -o $1 -gt 100 ]; then
echo "The grade must be between 0 and 100!"
elif [ $1 -ge 90 ]; then
echo "A"
elif [ $1 -ge 80 -a $1 -lt 90 ]; then # >=80 && <90
echo "B"
elif [ $1 -ge 70 -a $1 -lt 80 ]; then
echo "C"
elif [ $1 -ge 60 -a $1 -lt 70 ]; then
echo "D"
else
echo "E"
fi
fi
测试:
(2)创建一个简单的Shell程序,实现输入目录名,查看当前文件夹下有没有这个目录。如果没有则创建该目录,若已存在则输出“exist”。
vim t3.sh
,编写内容如下:
#!/bin/bash
num=$# # get the number of parameter
if [ $num -lt 1 ]; then
echo "The number of paramter is less than one!"
else
if test -d $1; then
echo "The dirctory already exists!"
else
mkdir $1
echo "The new dirctory $1 has been made!"
fi
fi
测试:
(3)创建一个简单的Shell程序,要求带一个参数,判断该参数是否是水仙花数。所谓水仙花数是指一个 3位数,它的每个位上的数字的 3次幂之和等于它本身。例如153=13+33+53,153是水仙花数。编写程序时要求首先进行参数个数判断,判断是否带了一个参数,如果没有参数则给出提示信息,否则给出该数是否是水仙花数。要求对153,124,370分别进行测试判断。
vim t2.sh
,编写内容如下:
#!/bin/bash
num=$# # get the number of parameter
if [ $num -lt 1 ]; then
echo "The number of paramter is less than one!"
else
x=$1 # get the first parameter
if [ $x -gt 999 -o $x -lt 100 ]; then
echo "The number must be between 100 and 999!"
else
a=$(($x/100))
b=$(($x/10%10))
c=$(($x%10))
echo "a=$a b=$b c=$c"
a=$(($a*$a*$a))
b=$(($b*$b*$b))
c=$(($c*$c*$c))
echo "a^3=$a b^3=$b c^3=$c"
if [ $(($a+$b+$c)) = $x ]; then
echo "Yes! $a + $b + $c = $x"
else
echo "No! $a + $b+ $c != $x"
fi
fi
fi
测试:
二、循环语句
(1)编写一个shell脚本,利用for循环把当前目录下的所有*.sh文件复制到指定的目录中,并为没有执行权限的文件添加执行权限。(可以在当前目录下先建立几个*.sh文件,用来测试,复制到的指定目录可以自己建立一个)
#!/bin/bash
filelist=`ls ./*sh`
echo $filelist
read -p "Please input dir path: " dir
test -e $dir || mkdir $dir # dir不存在则自动创建
for i in $filelist
do
cp $i $dir
chmod a+x $dir/$i
done
在当前目录中准备好t1.sh,t2.sh,t3.sh,然后进行测试:
(2) 编写shell脚本,输入10个数,利用while循环求其偶数之和。
#!/bin/bash
i=1
sum=0
while [ $i -le 10 ]
do
read -p "Please input the number $i: " x
let i++
if [ $(($x%2)) == 0 ]; then
let sum=sum+x
fi
echo "Now sum=$sum"
done
echo "Final sum=$sum"
注意,这里使用到了let命令,ubuntu可能会找不到let命令,因为/bin/sh指向了dash而不是bash,dash不支持let命令,解决办法是输入命令 sudo dpkg-reconfigure dash
,之后选择否即可。
测试:
(3) 编写shell脚本,利用until循环求1到10的平方和。(until循环的条件其实就是while循环中条件的补集)
#!/bin/bash
i=1
sum=0
until [ $i -gt 10 ] # 等价于 while [ $i -le 10 ]
do
let sum=sum+i*i
let i++
done
echo "sum=$sum"
测试:
(4)运行下列程序,观察程序的运行结果。”break 或者 continue“语句分别为break,break 2,continue,continue 2,观察四种情况下的实验结果。
#!/bin/bash
for i in a b c d
do
echo -n $i # -n表示不换行
for j in 1 2 3 4 5 6 7 8 9 10
do
if [ $j == 5 ]; then
break 或者 continue # 修改这行
fi
echo -n $j
done
echo # 换行
done
①语句改成break,运行结果:
②语句改成break 2,运行结果:
③语句改成continue,运行结果:
④语句改成continue 2,运行结果:
结论:break 2表示先跳出两层循环再结束当前层的循环,continue 2表示先跳出两层循环再继续当前层的下一次循环。
三、函数的编写和调用
注意:shell中函数返回值范围为0–255!
(1)编写shell脚本fun1.sh,定义一个函数返回两个数的和。
#!/bin/bash
sum(){
return $(($1+$2))
}
read -p "Please input a: " a
read -p "Please input b: " b
sum $a $b
echo $a + $b = $? # We can use "$?" to get the return value of the function
测试:
测试结果表明,函数的返回值的范围为[0,255],否则就溢出了。
如果把代码改成没有返回值的函数,编写fun11.sh如下:
#!/bin/bash
sum(){
read -p "Please input a: " a
read -p "Please input b: " b
echo $a + $b = $(($a+$b))
}
sum
此时fun11.sh运行结果就正确了:
(2)编写shell脚本,该脚本中定义一个递归函数,求n的阶乘。
#!/bin/bash
f(){
local n=$1
if [ $n == 0 ]; then
return 1
else
f $(($n-1)) # calculate (n-1)!
return $(($n*$?)) # return n*(n-1)!
fi
}
read -p "Please input a number: " n
f $n
echo "The factorial of n is $?"
测试(如果函数要递归返回值的话则答案不能超过255):
(3)已知shell脚本test.sh内容如下所示,试运行下列程序,观察程序运行结果,理解函数嵌套的含义。
#!/bin/bash
first() {
second() {
third() {
echo "This is the third"
}
echo "This is the second"
third
}
echo "This is the first"
second
}
echo "start..."
first
运行结果: