(二)SHELL
1 退出脚本
1.1 在SHELL中每个命令都有结束的状态码。
状态嘛 描述
0 命令成功退出
1 通用未知错误
2 错误使用SHELL命令
126 命令不可执行
127 没找到命令
128 无效退出参数
128+x Linux信号x的严重错误
130 命令通过Ctrl+C 终止
255 退出状态码越界
查看状态码:
echo $?
[root@localhost shellscript]# ifconfig -al
ifconfig: option '-al' not recognised.
ifconfig: '--help' gives usage information.
[root@localhost shellscript]# echo $?
1
[root@localhost shellscript]# sdf
-bash: sdf: command not found
[root@localhost shellscript]# echo $?
127
[root@localhost shellscript]# find | more |grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
[root@localhost shellscript]# echo $?
2
1.2 exit命令
exit命令允许你在脚本执行结束时指定一个退出状态码
#!/bin/bash
echo "hello word"
echo "Exit status "
exit 5
[root@localhost shellscript]# sh test1.sh
hello word
Exit status
[root@localhost shellscript]# echo $?
5
2 逻辑结构
2.1 使用if-then语句
格式如下:
if command_if
then
command_then
fi
command_if 不同于其他语言中的TURE和FALSE,command_if是根据命令返回的状态码决定是否执行then后面的语句
当返回的状态码为0时才能执行then语句后面的命令
e.g:
#!/bin/bash
#if statement
if date
then
echo "It\`s OK"
fi
[root@localhost shellscript]# sh if_staemtn.sh
Sat Aug 17 05:01:36 EDT 2013
It`s OK
if-then 另一种写法
if command;then
commands
fi
2.2 if-then-else语句
if command
then
commands
else
commands
fi
#!/bin/bash
if Hello_kitty; then
echo "Oh yes"
else
echo "It is False"
fi
[root@localhost shellscript]# sh hello_kitty.sh
hello_kitty.sh: line 3: Hello_kitty: command not found
It is False
2.3 嵌套if
if commands
then
commands2
elif commands2
then
commands
fi
#!/bin/bash
if adc
then
echo "A"
elif echo ' '
then
echo "B"
fi
3 test命令
test 命令可以判断3类条件
(1) 数值比较
(2) 字符串比较
(3) 文件比较
3.1
数值比较:
n1 -eq n2 检查是否相等
n1 -ge n2 n1是否大于等于n2
n1 -gt n2 检查n1大于n2
n1 -le n2 检查n1是否小于等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2
#!/bin/bash
var1=1
if test $var1 -eq 1
then
echo "var equal to 1"
fi
还有另为一种写法:
if [ $var -eq 1 ]
then
echo "var equal to 1"
fi
注意的是[] 内 各加一个空格
字符串之间的比较
str1 = str2 检查是否相同
str1 != str2 检查是否不同
str1 < str2 检查str1是否比str2小
str1 > str2 检查str1是否比str2大
-n str1 检查str1的长度是否非0
-z str1 检查str1的长度是否为0
#!/bin/bash
var1=Hello
var2=hello
var3=Word
var4=word
if [ $var1 \= $var2 ]
then
echo "$var1 $var3 "
else
echo "$var2 $var4 "
fi
[root@localhost shellscript]# sh test7.sh
Hello Word
#!/bin/bash
if [ -n "$str" ]
then
echo "legth = ${#str}"
else
echo "str Not found"
fi
[root@localhost shellscript]# sh test9.sh
str Not found
注意 ${#var1} 能够返回变量的长度
这个变量没有在脚本中被定义那么默认情况下长度为零
-n/z 之后要加双引号
3.2 文件的比较
-d file检查file是否存在并是个一目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否可以读
-s file 检查file是否为空
-w file 检查file是否可写
-x file 检查file是够可执行
-O file 检查file文件是否属于当前用户所有
-G file 检查file文件是否属于当前用户组
file1 -nt file2 检查file1文件是否比file2新
file1 -ot file2 检查file1文件是否比file2旧
#!/bin/bash
dir=/root
if [ -d "$dir" ]
then
echo "/root is directory"
fi
[root@localhost shellscript]# sh test10.sh
/root dir is directory
#!/bin/bash
dir1=/shellscript/test3.sh
dir2=/shellscript/test4.sh
if [ "$dir1" -nt "$dir2" ]
then
echo "$dir1 file is newer than $dir2"
else
echo "$dir2 file is newer than $dir2"
fi
[root@localhost shellscript]# sh test12.sh
/shellscript/test4.sh file is newer than /shellscript/test4.sh
3.3 符合条件判断:
[ conditon1 ]&&[ conditon2 ]
[ conditon1 ]||[ conditon2 ]
3.4 高级特性
使用双括号,必须是数字表达式才能使用双括号
#!/bin/bash
var=10
if(( $var ** 2 > 90 ))
then
echo "100 > 90"
fi
[root@localhost shellscript]# sh test13.sh
100 > 90
#!/bin/bash
var=10
if(( $var ** 2 > 90 && $var == 11 ))
then
echo "100 > 90"
else
echo " var != 10 "
fi
[root@localhost shellscript]# sh test14.sh
var != 10
使用双方括号来比较字符串(使用到正则表达)
` expression `
#!/bin/bash
var=hello
if [[ $var == h* ]]
then
echo "$var"
else
echo "sorry"
fi
[root@localhost shellscript]# sh test15.sh
hello
4 case
case 语法
case variable in pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
#!/bin/bash
var=A
case $var in "A" | "B") echo "var is A or B";;
"C") echo "var is C";;
*) echo "var is nothing";;
esac
[root@localhost shellscript]# sh test16.sh
var is A or B