1.流程控制
if-elif-else
语法:
if condition
then
command1
command2
...
commandN
else
commandM
fi
if … (开头判断)
then(开始输入)
…
elif…(二次判断)
then(二次开始输入)
…
else
…
fi(finish结束)
举例:
num1=$1
num2=$2
if test $[num1] -eq $[num2]
then
echo '两个数字相等!'
else
echo '两个数字不相等!'
fi
举例二:
a=$1
b=$2
if [ $a == $b ]
then
echo "a 等于 b"
elif [ $a -gt $b ]
then
echo "a 大于 b"
elif [ $a -lt $b ]
then
echo "a 小于 b"
else
echo "没有符合的条件"
fi
for循环
与C语言有异曲同工之妙
第一类:数字循环
数字循环-1
语法:
for(())
for((i=1;i<=10;i++)); # for需要两个括号(),不是C语言的一个
do
echo $i
done
result:
1
2
3
4
5
6
7
8
9
10
数字循环-2
语法:
$(seq FIRST LAST FIRST INCREMENT)
生成一组从first到last的数列
参数:
FIRST :开始数字
LAST :结束数字
FIRST INCREMENT:数字之间的间隔
for i in $(seq 1 10)
do
echo $i
done
result:
1
2
3
4
5
6
7
8
9
10
数字循环-3
语法:
for i in {first..last}
for i in {1..10}
do
echo $i
done
result:
1
2
3
4
5
6
7
8
9
10
第二类:字符性循环
- 把当前目录下的所有文件列出:
for i in `ls`;
do
echo $i is file name\! ;
done
result:
444 is file name!
demo01.sh is file name!
demo02.sh is file name!
demo03.sh is file name!
demo04.sh is file name!
demo05.sh is file name!
demo06.sh is file name!
demo07.sh is file name!
demo08.sh is file name!
demo09.sh is file name!
demo10.sh is file name!
demo11.sh is file name!
demo.sh is file name!
for01.sh is file name!
nohup.out is file name!
test is file name!
tests01 is file name!
- 把当前所有输入的参数列出:
for i in $* ;
do
echo $i is input chart\! ;
done
[root@admin world]# ./for01.sh 1 2 3 4 5 6
1 is input chart!
2 is input chart!
3 is input chart!
4 is input chart!
5 is input chart!
6 is input chart!
- 把f1,f2,f3字符输出
for i in f1 f2 f3 ;
do
echo $i is appoint ;
done
[root@admin world]# ./for01.sh
f1 is appoint
f2 is appoint
f3 is appoint
- 把句子中每个单词拆分开输出
list="rootfs usr data data2"
for i in $list;
do
echo $i is appoint ;
done
[root@admin world]# ./for01.sh
rootfs is appoint
usr is appoint
data is appoint
data2 is appoint
while循环和until循环
- while循环:语法while 判断
while condition
do
command
done
若判断为true则会一直执行,直到判断为false,则跳出循环
int=1
while(( $int<=5 ))
do
echo $int
let "int++"
done
result:
1
2
3
4
5
无限循环
while :
do
command
done
或者
while true
do
command
done
或者
for (( ; ; ))
- until循环:语法until 判断
until condition
do
command
done
若判断为false则会一直执行,直到判断为true,则跳出循环
举例:
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
result:
0
1
2
3
4
5
6
7
8
9
case … esac循环
与switch语法类似,每一个1)之后都需要两个分号结尾;;
case 值 in
模式1)
command1
command2
…
commandN
;;
模式2)
command1
command2
…
commandN
;;
esac
举例:
echo '输入 1 到 4 之间的数字:'
echo '你输入的数字为:'
read aNum
case $aNum in
1) echo '你选择了 1'
;;
2) echo '你选择了 2'
;;
3) echo '你选择了 3'
;;
4) echo '你选择了 4'
;;
*) echo '你没有输入 1 到 4 之间的数字'
;;
esac
result:
输入 1 到 4 之间的数字:
你输入的数字为:
2
你选择了 2
跳出循环
break命令
break命令允许跳出所有循环(终止执行后面的所有循环)。
continue
continue命令与break命令类似,它不会跳出所有循环,仅仅跳出当前循环。
2.函数的使用
Shell可以自定义函数
function_name ){
command1
...
commandN
[return int] 可加可不加
}
函数返回值在调用该函数后通过 $?
来获得。
举例:
funWithReturn(){
echo "这个函数会对输入的两个数字进行相加运算..."
echo "输入第一个数字: "
read aNum
echo "输入第二个数字: "
read anotherNum
echo "两个数字分别为 $aNum 和 $anotherNum "
return $(($aNum+$anotherNum))
}
funWithReturn
echo "输入的两个数字之和为 $? "
result:
这个函数会对输入的两个数字进行相加运算...
输入第一个数字:
1
输入第二个数字:
2
两个数字分别为 1 和 2
输入的两个数字之和为 3
函数当中的参数:
参数处理 | 说明 |
---|---|
$# | 传递到脚本或函数的参数个数 |
$* | 以一个单字符串显示所有向脚本传递的参数 |
$$ | 脚本运行的当前进程ID号 |
$! | 后台运行的最后一个进程的ID号 |
$@ | 与$*相同,但是使用时加引号,并在引号中返回每个参数。 |
$- | 显示Shell使用的当前选项,与set命令功能相同。 |
$? | 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 |
$?必须紧紧跟在函数的下一条语句使用才可以调出函数的返回值
否则则会被用来验证上一条语句是否运行成功
成功的时候返回0(true)
失败的时候返回其他数字
举例:
functionParam(){
echo "第一个参数$1"
echo "第十个参数$10"
echo "第十个参数${10}"
echo "参数总数$#"
echo "显示所有参数*:$*"
echo "显示所有参数@:$@"
}
functionParam 1 2 3 4 5 6 7 8 9 0 a s d f
第一个参数1
第十个参数10
第十个参数0
参数总数14
显示所有参数*:1 2 3 4 5 6 7 8 9 0 a s d f
显示所有参数@:1 2 3 4 5 6 7 8 9 0 a s d f
函数中若需要加入参数,则需要把参数加入到调取函数时函数的后面
functionParam 1 2 3 4 5 6 7 8 9 0 a s d f
函数调取参数 $1调取第一个,以此类推
$1——$9 调取1-9个参数
当参数超过十个,则参数不能使用$10
应当使用${10}才可以把第十个元素调出来