9. shell脚本结构化命令
if then 结构
if command
then
command s
. ......
fi
当if 后command的执行退出状态为0的时候,执行then后的命令。fi 为结束
if then else 结构
if command
then
commands
........
else
commands
......
fi
如果if 后 command执行的退出状态码为0, 则执行then后的命令,否则执行else 的命令。
嵌套if语句结构
if command1
then
commands
.....
elif command2
then
commands
.....
fi
test 命令
test命令提供一种检测if then语句中不同条件的方法,如果test命令中列出的条件评估值为true,test命令以0 为退出状态码退出。则为 if then 提供了逻辑判断。
if test condition
then
commands
fi
在bash shell中提供了一种在if then 语句中声明test命令的另一种方法:
if [空格 condition 空格] 注意在[]中,condition前后要有空格
then
commands
fi
test 命令能够评估以下3类条件:
(1)数值比较
测试两个值的条件参数如下:
n1 -eq n2 检测n1是否等于n2
n1 -le n2 检测n1是否小于或等于n2
-ge大于或等于 -lt 小于, -gt 大于, -ne 是否不小于
if [$var1 gt 4]
then
xxxx
fi
(2)字符串比较
str1 = str2 比较相等(和赋值号区别注意, 赋值号=前后不能有空格,而比较相等前后要有空格)。 != 不等 \< 小于 \> 大于 -n str1 检查str1的长度是否大于0 -z str1 检查长度是否等于0 这些比较运算符的前后均要有空格,即 str1 空格 比较符 空格 str2
其中小于和大于符号要进行转义,否则会被认为是重定向。
if [ $var1 \> $var2]
then
xxxx
fi
if [ -n $var1 ] //判断是否为非零值。 -z 判断字符串长度是否为0
then
xxx
fi
(3)文件比较
一些参数:
-d file 判断file 是否存在并且是一个目录
-e file 判断file 是否存在
-f file 判断file 是否是一个文件
-r -w -x 是否可读,可写,可执行
-s file 判断file是否为空
-o file 判断当前用户是否是file文件的所有者
-G file 比较检查文件的默认组,如果他和用户的默认组相匹配,则成功
file1 -nt file2 判断file1 是否比file2 新
file1 -ot file2 判断file1是否比file2 旧
复合条件检查
[ condition1 ] && [condition2] //逻辑与
[condition1] || [condition2] //逻辑或
if then 的高级特征:
(1) 使用双圆括号 ((expression))
双圆括号允许在比较中使用更加高级的数学公式在expression中允许使用的数学符号:
a++,++a, a -- ,--a, <<, >>, ! 逻辑否定, ~ 逐位取反, &按位与, | 按位或, && 逻辑与, || 逻辑或 ** 取幂
if (( $var1 ** 3 > 90)) //在双括号中,不必担心大于,小于号的转义问题。
then
xx
fi
case 语句
case variable in
pattern1)
command1;;
pattern3)
command2;;
*)
command_default;;
esac
#test bash shell
case $USER in
jason)
echo "hello jason";;
bourne)
echo "hello bourne";;
*)
echo "hello others";;
esac
流程控制命令:
for 命令
for var in list
do
commands
done
for test in About Abandon Abort Araba " hello are you" 这里使用双引号,防止将三个单词分开单独使用
do
echo the next word is $test
done
//读取命令输出中的数据
file="states"
for state in `cat $file`
do
echo "Visit beautiful $state"
done
改变字段分隔符
系统中内部字段分隔符 IFS的特殊环境变量,环境变量IFS 定义了bash shell用作字段分隔符的字符列表。默认情况下,bash shell将 空格、制表符 tab、换行符当做分隔符。
如果bash shell在数据中遇到这几种字符之一,则会认为在列表中启动新的数据字段。当处理能够包含空格的数据(例如文件名)时,会产生干扰。
解决方法可以在shell脚本中暂时更改环境变量IFS的值,限制bash shell看做是字段分隔符的字符。例如 IFS='$\n'
文件操作使用通配符
for file in /home/jason/.b* /home/jason/test
do
if [ -d "$file" ]
then
echo $file is a dir
elif [ -f "$file" ]
then
echo $file is a file
fi
done
bash shell中也可以使用类似 c语言的for 语句
for ((a=1; a < 10; a++)) //使用双括号
do
command
done
while命令
while test command
do
other command
done
while 语句,使用多条测试命令。
while命令允许在while语句行定义多条test命令。只有最后一条测试命令的退出状态用来决定循环是否停止。
var=10
while echo $var
[ $var -ge 0 ]
do
echo this is inside the loop
var=$[$var1 - 1]
done
until 命令
until test commands
do
other commands
done
嵌套循环
for ((a=1; a < 4; a ++))
do
xxx
for ((b=1; b < 3;b ++))
do
xxx
done
xxxx
done
文件数据的循环
通常需要迭代存储在文件内部的项,这需要使用嵌套循环和更改环境变量IFS
IFS.OLD=$IFS
IFS=$'\n'
for entry in `cat /etc/passwd`
do
echo "value in $entry -"
IFS=;
for value in $entry
do
echo " $value"
done
done
break语句跳出循环
if test command
then
break;
默认一次跳出单个循环。可以在任何循环语句中使用,类似C语言。如果使用 break -n 指定跳出n重循环
continue 语句结束此次循环 类似C语言
if test command
then
continue
处理循环的输出
可以在shell脚本中使用或者管道重定向循环输出结果,在done结尾添加 处理命令来实现。
例如:
for ((a=1;a < 4; a ++))
do
command
done > test.txt
或者
for ((a=1;a < 4; a ++))
do
command
done|sort