【控制流结构】 --空命令永远为真标签:case,bin,控制流,echo,----------------------------------------------------,while,sh, From: https://blog.51cto.com/hzc2012/6000172
========================
if
elif
else
fi
========================
copyfile:
#!/bin/sh
#copyfile
if cp surfile desfile.bak
then echo "haved copy"
else
echo "`basename $0`:error could not copy the files">%2
fi
$ sh copyfile
haved copy
----------------------------------------------------
#!/bin/sh
#copyfile
if cp nothisfile desfile.bak >/dev/null 2>&1 不在屏幕显示
then [:]
echo "haved copy"
else
echo "`basename $0`:error could not copy the files">&2
fi
$ sh copyfile
copyfile:error could not copy the files
----------------------------------------------------
filename=text.txt
if [ ! -w "$filename" ] 判断文件 text.txt
if [ $# -lt 3] 调用传入脚本的参数个数小于3
if [ -t ] 返回值为1,则为交互模式(终端模式)
if [ -z $EDITOR ] --环境变量$EDITOR是否为空变量
$ ["$DIRECTORY" = ""] 环境变量$DIRECTORY是否为空变量
if [ -z $LOGNAME != "root" ] 登录账户为非root账户
----------------------------------------------------
========================
case [value] in
esac
========================
* 任意字符。
? 任意单字符。
[..] 类或范围中任意字符
----------------------------------------------------
#!/bin/sh
#text
echo -n "Enter a number from 1 to 5 :"
read ANS
case $ANS in
1) echo "You select 1"
;;
2) echo "You select 2"
;;
3) echo "You select 3"
;;
4) echo "You select 4"
;;
5|6) echo "You select 5/6"
;;
yes|y) echo "You select 4"
;;
*) echo "`basename $0 ` : This is not between 1 and 5 ">&2
[break]
exit 1
;;
esac
$ sh text
Enter a number from 1 to 5 :3
You select 3
$ sh text
Enter a number from 1 to 5 :8
text : This is not between 1 and 5
----------------------------------------------------
========================
for varname in list
do
command1
command2
...
done
========================
#!/bin/sh
#text
for loop in 1 2 3 4 5
do
echo $loop
done
$ sh text
1
2
3
4
5
----------------------------------------------------
for loop in "orange red blue grey"
do echo $loop ; done 打印字符串列表
----------------------------------------------------
for loop in `ls`
do echo $loop ; done 打印当前ls的内容
----------------------------------------------------
========================
for varname in list1
do
for
done
done
========================
until format:
until condition
done
========================
while command
do
done
========================
#!/bin/sh
#text
count=0
while [ $count -lt 5 ]
do
count=`expr $count + 1 `
echo $count
done
----------------------------------------------------
#!/bin/sh
#text
echo -n "enter your most liked film"
while read film
do
echo "Yeah,great film the $film"
done
----------------------------------------------------