SHELL控制流结构
比较符号:-lt小于 -le小于等于 -gt大于 -ge大于等于 -ne不等于 -eq等于
< 小于(需要双括号),如:(("$a" < "$b"))
<= 小于等于(需要双括号),如:(("$a" <= "$b"))
> 大于(需要双括号),如:(("$a" > "$b"))
>= 大于等于(需要双括号),如:(("$a" >= "$b"))
=或==(需要双括号),如:if [ "$a" == "$b" ]
-b file 若文件存在且是一个块特殊文件,则为真
-c file 若文件存在且是一个字符特殊文件,则为真
-f file 若文件存在且是一个规则文件,则为真
-g file 若文件存在且设置了SGID位的值,则为真
-h file 若文件存在且为一个符合链接,则为真
-k file 若文件存在且设置了"sticky"位的值
-p file 若文件存在且为一已命名管道,则为真
-u file 若文件存在且设置了SUID位,则为真
-o file 若文件存在且被有效用户ID所拥有,则为真
-z string 若string长度为0,则为真
-n string 若string长度不为0,则为真
string1 = string2 若两个字符串相等,则为真
string1 != string2 若两个字符串不相等,则为真
如果if和then在同一行那命令格式为 if 条件1;then
echo -n "Enter you name:"
if [ "$NAME" == " " ]; then
echo "you did not enter you name"
echo "you name is: $NAME"
####保存退出,chmod +x name.sh
[root@localhost ~]# ./name.sh
Enter you name:tony (这个名字你是输入的)
eg:copy一个文件,如果文件不存在会提示系统错误的信息,和提示自己给的信息
if cp test1.txt myfile.txt; then
echo "copy is successful"
echo "`basename $0`:no such test1.txt file" >&2
####保存退出,chmod +x ifcp.sh
####运行 ./ifcp.sh -n (-n参数可以检查脚本是否有语法错误)
[root@localhost ~]# ./ifcp.sh
cp: cannot stat `test1.txt': No such file or directory
ifcp.sh:no such test1.txt file
eg:copy一个文件,文件不存在系统提示的信息不显示在屏幕上,显示提示自己给的信息
if cp test1.txt myfile.txt 2>/dev/null; then
echo "copy is successful"
echo "`basename $0`:no such test1.txt file" >&2
####保存退出,chmod +x ifcp.sh
[root@localhost ~]# ./ifcp.sh
ifcp.sh:no such test1.txt file
eg:copy一个文件,文件存在则提示copy is successful
if cp 1.txt myfile.txt 2>/dev/null; then
echo "copy is successful"
echo "`basename $0`:no such test1.txt file"
####保存退出,chmod +x ifcp.sh
[root@localhost ~]# ./ifcp.sh
[root@localhost ~]# cat myfile.txt
解释:`bsename $0`值显示当前脚本或命令的名字,$0显示会包括当前脚本或命令的路径
eg:一个if---elif---elif--else的语句, -z的参数不知道是什么意思,自己可以man test查看一下,注意空格和分号,引号
echo -n "Enter your name:"
if [ -z $NAME ] || [ "$NAME" = " " ];then
echo "you did not enter a name"
elif [ "$NAME" = "root" ];then
elif [ "$NAME" = "tony" ];then
####保存退出,chmod +x ifelse.sh
[root@localhost ~]# ./ifelse.sh
[root@localhost ~]# ./ifelse.sh
[root@localhost ~]# ./ifelse.sh
case取值后面必须为单词in,每一模式必须以右括号结束。取值可以为变量或常数,匹配发现取值符合某一模式后,期间
所有命令开始执行直至;;.模式匹配符合*表示任意字符,?表示任意单字符,[..]表示类或范围中任意字符
echo -n "Enter a number from 1 to 3:"
echo "`basename $0`:this is not between 1 and 3 ">&2
#####保存退出,chmod + case.sh
[root@localhost ~]# ./case.sh
Enter a number from 1 to 3:1
[root@localhost ~]# ./case.sh
Enter a number from 1 to 3:2
[root@localhost ~]# ./case.sh
Enter a number from 1 to 3:3
[root@localhost ~]# ./case.sh
Enter a number from 1 to 3:5
case.sh:this is not between 1 and 3
当变量值在列表里,for循环即执行一次所有命令,使用变量名访问列表中取值,命令可为任何有效的shell命令和语句,变量名
为任何单词,in列表用法是可选的,如果不用它,for循环使用命令行的位置参数。in列表可以包含替换,字符串和文件名。
####保存退出,chmod +x for1.sh
[root@localhost ~]# ./for1.sh
for loop in "orange red bue grey"
####保存退出,chmod +x for2.sh
[root@localhost ~]# ./for2.sh
把for2.sh里面的内容for loop in "orange red bue grey" 改成for loop in orange red bue greyz则in后面的分行显示
eg:in后面的参数为一个命令,``反引号里面的是系统的命令
for jie in `cat myfile.txt`
####保存退出,chmod +x for3.sh
[root@localhost ~]# cat myfile.txt
[root@localhost ~]# ./for3.sh
echo "zhe li mian end you yi ge end" >myfile.txt
for JIE in `cat myfile.txt`
if [ "$JIE" = "end" ];then
echo "it is not end,it is:$JIE"
#####保存退出,chmod +x for4.sh
[root@localhost ~]# ./for4.sh
条件可以为任意测试条件,测试发生在循环末尾,因此循环至少执行一次
eg:检查磁盘空间的大小,每隔300s检查磁盘空间,超过指定的数字就发邮件给root用户
LOOK_OUT=`df | grep "$Part" | awk '{print $5}'| sed 's/%//g'`
until [ " $LOOK_OUT" -gt "90" ]
echo "this Filesystem is empty" |mail root
LOOK_OUT=`df | grep "$Part" | awk '{print $5}'| sed 's/%//g'`
#####保存退出,chmod +x until.sh
在while和都之间虽然通常指使用一个命令,但可以放几个命令,命令通常用作测试条件
echo -e "zhui jia \n jin qu \n yi juhua " >> $NAME
echo -e "zhe ge wen jian \n shi xin \n jian de " > $NAME
######保存退出,chmod +x while.sh
if [ -e "$NAME" ] //判断这个文件有木有,若果有则会追加一句话,没有则会新建一个文件,然后会添加一句话
然后通过循环把他显示输出,如果没有这个文件,运行第一遍则只会出现echo -e "zhe ge wen jian \n shi xin \n jian de " > $NAME
这个里面的,如果运行第二遍,则 echo -e "zhe ge wen jian \n shi xin \n jian de " > $NAME会显示一次,然后
echo -e "zhui jia \n jin qu \n yi juhua " >> $NAME会输入一次,运行第三遍,则echo -e "zhui jia \n jin qu \n yi juhua " >> $NAME
退出循环,如果是在一个嵌入循环里,可以指定n来跳出循环的个数,
echo -n "Enter any number [ 1...5 ]:"
echo "Your enter a number between 1 and 5."
######保存退出,chmod +x break.sh
[root@localhost ~]# ./break.sh
Enter any number [ 1...5 ]:1
Your enter a number between 1 and 5.
Enter any number [ 1...5 ]:3
Your enter a number between 1 and 5.
Enter any number [ 1...5 ]:7
解释:while : ,while后面接一个: 表示while语句永远为真,用break跳出循环。
#####vim breakcontinue.sh
echo -n "Enter any number [ 1...5 ]:"
echo "Your enter a number between 1 and 5."
echo -n "Wrong number,continue(y/n?)."
######保存退出, chmod +x breakcontinue.sh
#####运行, ./breakcontine.sh
[root@localhost ~]# ./breakcontinue.sh
Enter any number [ 1...5 ]:3
Your enter a number between 1 and 5.
Enter any number [ 1...5 ]:7
Wrong number,continue(y/n?).y
Enter any number [ 1...5 ]:6
Wrong number,continue(y/n?).n
echo "this script will to find which service have started"
testing=`netstat -tlun | grep ":80"`
if [ -n "$testing" ];then ##if no null is true
echo "WWW server has started!"
testing=`netstat -tlun | grep ":21"`
if [ "$testing" != "" ];then ###if no null is true
echo "vsftpd server has started!"
testing=`netstat -tlun | grep ":22"`
if [ -n "$testing" ];then
echo "SSH server has started!"
testing=`netstat -tlun | grep ":25"`
if [ "$testing" != "" ];then
echo "MAIL server has started!"
拥有内建变量,$0表示函数名称,后续接的变量标记为$1,$2,$3....
printinfo;echo $1 | tr -s 'a-z' 'A-Z'
printinfo;echo $1 | tr -s 'a-z' 'A-z'
for animal in cat dog pig
echo "$animal miao miao jiao"
echo "$animal wang wang jiao"
echo "$animal luo luo jiao"
echo "$animal jiao mei jiao"