if then else语句
- If 条件1 //如果条件1为真
- Then //那么
- 命令1 //执行命令1
- elif 条件2 //如果条件1不成立
- then //那么
- 命令2 //执行命令2
- else //如果条件1,2均不成立
- 命令3 //那么执行命令3
- fi //完成
If 条件1 //如果条件1为真
Then //那么
命令1 //执行命令1
elif 条件2 //如果条件1不成立
then //那么
命令2 //执行命令2
else //如果条件1,2均不成立
命令3 //那么执行命令3
fi //完成
简单的if语句
最普通的if语句是:
if条件
then 命令
if
使用if语句时,必须将then部分放在新行,否则会产生错误。如果要不分行,必须使用命令分隔符。本
书其余部分将采取这种形式。现在简单 if语句变为:
if 条件;then
命令
if
- /home/l/g/tomotoboy >cat iftest
- #!/bin/sh
- #iftest
- #this is a comment line,all comment lines start with a#
- if [ "12" -lt "14" ]
- then
- #yes 12 is less than 14
- echo "Yes, 12 is less than 14"
- fi
-
- /home/l/g/tomotoboy >chmod u+x iftest
- /home/l/g/tomotoboy > ./iftest
- Yes, 12 is less than 14
/home/l/g/tomotoboy >cat iftest
#!/bin/sh
#iftest
#this is a comment line,all comment lines start with a#
if [ "12" -lt "14" ]
then
#yes 12 is less than 14
echo "Yes, 12 is less than 14"
fi
/home/l/g/tomotoboy >chmod u+x iftest
/home/l/g/tomotoboy > ./iftest
Yes, 12 is less than 14
变量值测试
不必拘泥于变量或数值测试,也可以测知系统命令是否成功返回。对 grep使用if语句找出grep是否成功
返回信息。下面的例子中 grep用于查看tomotoboy是否在数据文件sed.txt中,注意'tomotoboy'用于精
确匹配。
- /home/l/g/tomotoboy >cat grepif
- #!/bin/sh
- #grep if
- if grep 'tomotoboy' sed.txt >/dev/null 2>&1
- then
- echo "tomotoboy is in the file"
- else
- echo "tomotoboy is not in the file"
- fi
-
- /home/l/g/tomotoboy >./grepif
- tomotoboy is in the file
/home/l/g/tomotoboy >cat grepif
#!/bin/sh
#grep if
if grep 'tomotoboy' sed.txt >/dev/null 2>&1
then
echo "tomotoboy is in the file"
else
echo "tomotoboy is not in the file"
fi
/home/l/g/tomotoboy >./grepif
tomotoboy is in the file
用变量测试grep输出
正像前面看到的,可以用grep作字符串操作。下面的脚本中,用户输入一个名字列表,grep在变量中查找,要求其查找指定字符串
- /home/l/g/tomotoboy >cat grepstr
- #!/bin/sh
- #grepstr
- echo -n "Enter a piece of text file:"
- read TEXT
- echo -n "Enter a string to query: "
- read QUERY
- if grep $QUERY $TEXT >/dev/null 2>&1
- then
- echo "$QUERY is in $TEXT"
- #could do some processing here...
- else
- echo "$QUERY is not in $TEXT"
- fi
-
- /home/l/g/tomotoboy >./grepstr
- -n Enter a piece of text file:
- sed.txt
- -n Enter a string to query:
- tomotoboy
- tomotoboy is in sed.txt
/home/l/g/tomotoboy >cat grepstr
#!/bin/sh
#grepstr
echo -n "Enter a piece of text file:"
read TEXT
echo -n "Enter a string to query: "
read QUERY
if grep $QUERY $TEXT >/dev/null 2>&1
then
echo "$QUERY is in $TEXT"
#could do some processing here...
else
echo "$QUERY is not in $TEXT"
fi
/home/l/g/tomotoboy >./grepstr
-n Enter a piece of text file:
sed.txt
-n Enter a string to query:
tomotoboy
tomotoboy is in sed.txt
文件拷贝输出检查
下面测试文件拷贝是否正常,如果 cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息。注意错误信息中` basename $0`打印脚本名。如果脚本错误退出,一个好习惯是显示脚本名并将之定向到标准错误中。用户应该知道产生错误的脚本名。
- /home/l/g/tomotoboy >chmod u+x ifcp
- /home/l/g/tomotoboy >ifcp
- cp: cannot access myfile
- ifcp: error could not copy the file
- /home/l/g/tomotoboy >cat ifcp
- #!/bin/sh
- #ifcp
- if cp myfile myfile.bak; then
- echo "good copy"
- else
- echo "`basename $0`: error could not copy the file" >&2
- fi
- /home/l/g/tomotoboy >touch myfile
- /home/l/g/tomotoboy >ifcp
- good copy
- /home/l/g/tomotoboy >
/home/l/g/tomotoboy >chmod u+x ifcp
/home/l/g/tomotoboy >ifcp
cp: cannot access myfile
ifcp: error could not copy the file
/home/l/g/tomotoboy >cat ifcp
#!/bin/sh
#ifcp
if cp myfile myfile.bak; then
echo "good copy"
else
echo "`basename $0`: error could not copy the file" >&2
fi
/home/l/g/tomotoboy >touch myfile
/home/l/g/tomotoboy >ifcp
good copy
/home/l/g/tomotoboy >
当前目录测试
当运行一些管理脚本时,可能要在根目录下运行它,特别是移动某种全局文件或进行权限改变时。一个简单的测试可以获知是否运行在根目录下。下面脚本中变量DIRECTORY使用当前目录的命令替换操作,然后此变量值与 " / "字符串比较(/为根目录) 。如果变量值与字符串不等,则用户退出脚本,退出状态为1意味错误信息产生。
- /home/l/g/tomotoboy >ifpwd
- You need to be in the root directory not /home/l/g/tomotoboy to run this script
- /home/l/g/tomotoboy >cd /etc
- /etc >cd /
- / >/home/l/g/tomotoboy/ifpwd
- / >cat ifpwd
- cat: cannot open ifpwd
- / >cat /home/l/g/tomotoboy/ifpwd
- #!/bin/sh
- #ifpwd
- DIRECTORY=`pwd`
- #grab the current directory
- if [ "$DIRECTORY" != "/" ];then
- #is it the root directory?
- #no ,the direct output to standard error,which is the screen by default.
- echo "You need to be in the root directory not $DIRECTORY to run this script" >&2
- #exit with a value of 1, an error
- exit 1
- fi
/home/l/g/tomotoboy >ifpwd
You need to be in the root directory not /home/l/g/tomotoboy to run this script
/home/l/g/tomotoboy >cd /etc
/etc >cd /
/ >/home/l/g/tomotoboy/ifpwd
/ >cat ifpwd
cat: cannot open ifpwd
/ >cat /home/l/g/tomotoboy/ifpwd
#!/bin/sh
#ifpwd
DIRECTORY=`pwd`
#grab the current directory
if [ "$DIRECTORY" != "/" ];then
#is it the root directory?
#no ,the direct output to standard error,which is the screen by default.
echo "You need to be in the root directory not $DIRECTORY to run this script" >&2
#exit with a value of 1, an error
exit 1
fi
文件权限测试
可以用i f语句测试文件权限,下面简单测试文件sed.txt是否可写
- /home/l/g/tomotoboy >ifwr sed.txt
- You can write to sed.txt
- /home/l/g/tomotoboy >cat ifwr
- #!/bin/sh
- #ifwr
- if [ ! -w "$1" ]; then
- echo "You cannot write to $1" >&2
- else
- echo "You can write to $1"
- fi
/home/l/g/tomotoboy >ifwr sed.txt
You can write to sed.txt
/home/l/g/tomotoboy >cat ifwr
#!/bin/sh
#ifwr
if [ ! -w "$1" ]; then
echo "You cannot write to $1" >&2
else
echo "You can write to $1"
fi
测试传递到脚本中的参数
if语句可用来测试传入脚本中参数的个数。使用特定变量$#,表示调用参数的个数。可以测试所需参数个数与调用参数个数是否相等。以下测试确保脚本有三个参数。如果没有,则返回一个可用信息到标准错误,然后代码退出并显示退出状态。如果参数数目等于3,则显示所有参数。
- /home/l/g/tomotoboy >cat ifparam
- #!/bin/sh
- #ifparam
- if [ $# -lt 3 ]; then
- #less than 3 parameters called,echo a usage message and exit
- echo "Usage: `basename $0` arg1 arg2 arg3" >&2
- exit
- fi
- #good ,receive 3 params, let's echo them
- echo "arg1: $1"
- echo "arg2: $2"
- echo "arg3: $3"
- /home/l/g/tomotoboy >ifparam yang shi hai
- arg1: yang
- arg2: shi
- arg3: hai
/home/l/g/tomotoboy >cat ifparam
#!/bin/sh
#ifparam
if [ $# -lt 3 ]; then
#less than 3 parameters called,echo a usage message and exit
echo "Usage: `basename $0` arg1 arg2 arg3" >&2
exit
fi
#good ,receive 3 params, let's echo them
echo "arg1: $1"
echo "arg2: $2"
echo "arg3: $3"
/home/l/g/tomotoboy >ifparam yang shi hai
arg1: yang
arg2: shi
arg3: hai
决定脚本是否为交互模式
有时需要知道脚本运行是交互模式(终端模式)还是非交互模式(cron或at) 。脚本也许需要这个信息以决定从哪里取得输入以及输出到哪里,使用test命令并带有-t选项很容易确认这一点。如果test返回值为1,则为交互模式。
- /home/l/g/tomotoboy >cat ifinteractive
- #!/bin/sh
- #ifinteractive
- if [ -t ];then
- echo "We are interactive with a terminal"
- else
- echo "We must be running from some background process probably cron or at"
- fi
- /home/l/g/tomotoboy >ifinteractive
- We are interactive with a terminal
/home/l/g/tomotoboy >cat ifinteractive
#!/bin/sh
#ifinteractive
if [ -t ];then
echo "We are interactive with a terminal"
else
echo "We must be running from some background process probably cron or at"
fi
/home/l/g/tomotoboy >ifinteractive
We are interactive with a terminal
简单的if else语句
下一个if语句有可能是使用最广泛的:
- if条件
- then
- 命令1
- else
- 命令2
- fi
if条件
then
命令1
else
命令2
fi
使用if语句的else部分可在条件测试为假时采取适当动作。
变量设置测试,下面的例子测试环境变量EDITOR是否已设置。如果EDITOR变量为空,将此信息通知用户。如果已设置,在屏幕上显示编辑类型。
- /home/l/g/tomotoboy >echo $EDITOR
-
- /home/l/g/tomotoboy >cat ifeditor
- #!/bin/sh
- #ifeditor
- if [ -z "$EDITOR" ];then
- #the variable has not been set
- echo "Your EDITOR environment is not set"
- else
- #let's us see what is it
- echo "Using $EDITOR as the default editor"
- fi
- /home/l/g/tomotoboy >ifeditor
- Your EDITOR environment is not set
- /home/l/g/tomotoboy >
/home/l/g/tomotoboy >echo $EDITOR
/home/l/g/tomotoboy >cat ifeditor
#!/bin/sh
#ifeditor
if [ -z "$EDITOR" ];then
#the variable has not been set
echo "Your EDITOR environment is not set"
else
#let's us see what is it
echo "Using $EDITOR as the default editor"
fi
/home/l/g/tomotoboy >ifeditor
Your EDITOR environment is not set
/home/l/g/tomotoboy >
检测最后命令状态
前面将目录名传入脚本创建了一个目录,脚本然后提示用户是否应创建目录。下面的例子创建一个目录,并从当前目录将所有 *.txt文件拷入新目录。但是这段脚本中用最后状态命令检测了每一个脚本是否成功执行。如果命令失败则通知用户。
- /home/l/g/tomotoboy >cat ifmkdir
- #!/bin/sh
- #ifmkdir
- DIR_NAME=testdirec
- #where we are?
- THERE=`pwd`
- #send all output to system dustbin
- mkdir $DIR_NAME >/dev/null 2>&1
- #is it a directory?
- if [ -d $DIR_NAME ];then
- #can we cd to the directory
- cd $DIR_NAME
- if [ $? = 0 ];then
- #yes we can
- HERE=`pwd`
- echo "$HERE"
- cp $THERE/*.txt $HERE
- else
- echo "Cannot cd to $DIR_NAME" >&2
- exit 1
- fi
- else
- echo "Cannot create directory $DIR_NAME" >&2
- exit 1
- fi
/home/l/g/tomotoboy >cat ifmkdir
#!/bin/sh
#ifmkdir
DIR_NAME=testdirec
#where we are?
THERE=`pwd`
#send all output to system dustbin
mkdir $DIR_NAME >/dev/null 2>&1
#is it a directory?
if [ -d $DIR_NAME ];then
#can we cd to the directory
cd $DIR_NAME
if [ $? = 0 ];then
#yes we can
HERE=`pwd`
echo "$HERE"
cp $THERE/*.txt $HERE
else
echo "Cannot cd to $DIR_NAME" >&2
exit 1
fi
else
echo "Cannot create directory $DIR_NAME" >&2
exit 1
fi
简单的安全登录脚本
以下是用户登录时启动应用前加入相应安全限制功能的基本框架。首先提示输入用户名和密码,如果用户名和密码均匹配脚本中相应字符串,用户登录成功,否则用户退出。脚本首先设置变量为假—总是假定用户输入错误,stty当前设置被保存,以便隐藏passwd域中字符,然后重新保存stty设置。如果用户I D和密码正确(密码是myday) ,明亮INVALID_USER和INVALID_PASSWD设置为no表示有效用户或密码,然后执行测试,如果两个变量其中之一为yes,缺省情况下,脚本退出用户。键入有效的ID和密码,用户将允许进入。这是一种登录脚本的基本框架。下面的例子中有效用户ID为dave或tomotoboy。
- #!/bin/sh
- #ifpass
- #set the variables to false
- INVALID_USER=yes
- INVALID_PASSWD=yes
- #set the current stty settings
- SAVEDSTTY=`stty -g`
- echo "You are logging into a sensitive area"
- echo -n "Enter your ID name:"
- read NAME
- #hide the characters typed in
- stty -echo
- echo "Enter your password :"
- read PASSWORD
- #back on again
- stty $SAVEDSTTY
- if [ "$NAME" = "tomotoboy" ] || [ "$NAME" = "dave" ]; then
- #if a valid then set variable
- INVALID_USER=no
- fi
- if [ "$PASSWORD" = "myday" ];then
- #if valid password then set variable
- INVALID_PASSWD=no
- fi
- if [ "$INVALID_USER" = "yes" ] || [ "$INVALID_PASSWD" = "yes" ];then
- echo "`basename $0` : Sorry wrong password or userid"
- exit 1
- fi
- echo "corrent user id and password given"
#!/bin/sh
#ifpass
#set the variables to false
INVALID_USER=yes
INVALID_PASSWD=yes
#set the current stty settings
SAVEDSTTY=`stty -g`
echo "You are logging into a sensitive area"
echo -n "Enter your ID name:"
read NAME
#hide the characters typed in
stty -echo
echo "Enter your password :"
read PASSWORD
#back on again
stty $SAVEDSTTY
if [ "$NAME" = "tomotoboy" ] || [ "$NAME" = "dave" ]; then
#if a valid then set variable
INVALID_USER=no
fi
if [ "$PASSWORD" = "myday" ];then
#if valid password then set variable
INVALID_PASSWD=no
fi
if [ "$INVALID_USER" = "yes" ] || [ "$INVALID_PASSWD" = "yes" ];then
echo "`basename $0` : Sorry wrong password or userid"
exit 1
fi
echo "corrent user id and password given"