第四张 控制流结构
控制结构
if then else 语句
case语句
for循环
until循环
while循环
break控制
continue控制。
流控制是什么?
#!/bin/bash
#创建一个目录
make /home/chinaitlab/shell/txt
#复制所有txt文件到
/home/chinaitlab/shell/txt/
cp *.txt /home/chinaitlab/shell/txt/
rm -f *.txt
上面的三个命令其中一个出错了怎办,这样就需要使用流控制语句
if语句:
if 条件1
then 命令1
elif 条件2
then 命令2
else 命令3
fi 完成
注意:if语句必须以fi结束。
For example:
#!/bin/bash
#iftest
#this is a comment line,all comment lines start with a #
if["10" -lt "12"]
then
#yes 10 is less than 12
echo "Yes ,10 is less than 12"
else
echo "NO"
fi
test[expression]/[!expression]/[expression1 -a expression2 两个都为真则整个条件就为真]
[expression1 -o expression2 其中一个都为真则整个条件就为真]
[-n]STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
STRING1=STRING2
the strings are equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
>=
INTEGER1 -gt INTEGER2
>
INTEGER1 -le INTEGER2
<=
INTEGER1 -lt INTEGER2
<
INTEGER1 -ne INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
稍后自己查test命令。
man test
脚本测试
#!/bin/bash
#iftest2
echo -n "Enter your name:"
read NAME
#did the user just hit return
if["$NAME"==""];then
echo "You did not enter any information"
fi
#!bin/bash
#ifcp
if cp myfile.bak myfile;then
echo"good copy"
else
echo "`basename $0`:error could not copy the files">&2
fi
#!/bin/bash
#ifelif
echo -n "Enter your name:"
read NAME
if[ -z $NAME] ||["$NAME"=""];then
echo "You did not enter a name."
elif["$NAME"="root"];then
echo "Hello root"
elif["$NAME"="chinaitlab"];then
echo "Hello chinaitlab"
else
echo "You are not root or chinaitlab,but hi,#NAME"
fi
set nu
控制结构
if then else 语句
case语句
for循环
until循环
while循环
break控制
continue控制。
流控制是什么?
#!/bin/bash
#创建一个目录
make /home/chinaitlab/shell/txt
#复制所有txt文件到
/home/chinaitlab/shell/txt/
cp *.txt /home/chinaitlab/shell/txt/
rm -f *.txt
上面的三个命令其中一个出错了怎办,这样就需要使用流控制语句
if语句:
if 条件1
then 命令1
elif 条件2
then 命令2
else 命令3
fi 完成
注意:if语句必须以fi结束。
For example:
#!/bin/bash
#iftest
#this is a comment line,all comment lines start with a #
if["10" -lt "12"]
then
#yes 10 is less than 12
echo "Yes ,10 is less than 12"
else
echo "NO"
fi
test[expression]/[!expression]/[expression1 -a expression2 两个都为真则整个条件就为真]
[expression1 -o expression2 其中一个都为真则整个条件就为真]
[-n]STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
STRING1=STRING2
the strings are equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
>=
INTEGER1 -gt INTEGER2
>
INTEGER1 -le INTEGER2
<=
INTEGER1 -lt INTEGER2
<
INTEGER1 -ne INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
稍后自己查test命令。
man test
脚本测试
#!/bin/bash
#iftest2
echo -n "Enter your name:"
read NAME
#did the user just hit return
if["$NAME"==""];then
echo "You did not enter any information"
fi
#!bin/bash
#ifcp
if cp myfile.bak myfile;then
echo"good copy"
else
echo "`basename $0`:error could not copy the files">&2
fi
#!/bin/bash
#ifelif
echo -n "Enter your name:"
read NAME
if[ -z $NAME] ||["$NAME"=""];then
echo "You did not enter a name."
elif["$NAME"="root"];then
echo "Hello root"
elif["$NAME"="chinaitlab"];then
echo "Hello chinaitlab"
else
echo "You are not root or chinaitlab,but hi,#NAME"
fi
set nu