无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

控制流结构——if then else

Posted on 2009-11-29 12:05 Gavin.lee 阅读(639) 评论(0)  编辑  收藏 所属分类: Linux shell 入门

 

if then else语句
Shell代码
  1. If  条件1    //如果条件1为真   
  2. Then         //那么   
  3. 命令1       //执行命令1  
  4. elif  条件2    //如果条件1不成立   
  5. then           //那么   
  6. 命令2        //执行命令2  
  7. else           //如果条件12均不成立   
  8. 命令3       //那么执行命令3  
  9. fi             //完成  



简单的if语句
最普通的if语句是:
if条件
then  命令
if
使用if语句时,必须将then部分放在新行,否则会产生错误。如果要不分行,必须使用命令分隔符。本

书其余部分将采取这种形式。现在简单 if语句变为:
if  条件;then
命令
if

Shell代码
  1. /home/l/g/tomotoboy >cat iftest   
  2. #!/bin/sh   
  3. #iftest   
  4. #this is a comment line,all comment lines start with a#   
  5. if [ "12" -lt "14" ]   
  6. then   
  7.  #yes 12 is less than 14  
  8.  echo  "Yes, 12 is less than 14"  
  9. fi   
  10.   
  11. /home/l/g/tomotoboy >chmod u+x iftest   
  12. /home/l/g/tomotoboy > ./iftest   
  13. Yes, 12 is less than 14  
变量值测试
不必拘泥于变量或数值测试,也可以测知系统命令是否成功返回。对 grep使用if语句找出grep是否成功

返回信息。下面的例子中 grep用于查看tomotoboy是否在数据文件sed.txt中,注意'tomotoboy'用于精

确匹配。
Shell代码
  1. /home/l/g/tomotoboy >cat grepif   
  2. #!/bin/sh   
  3. #grep if   
  4. if grep 'tomotoboy' sed.txt >/dev/null 2>&1  
  5. then   
  6.  echo "tomotoboy is  in the file"  
  7. else   
  8.  echo "tomotoboy is not in the file"  
  9. fi   
  10.   
  11. /home/l/g/tomotoboy >./grepif   
  12. tomotoboy is  in the file  

用变量测试grep输出
正像前面看到的,可以用grep作字符串操作。下面的脚本中,用户输入一个名字列表,grep在变量中查找,要求其查找指定字符串

Shell代码
  1. /home/l/g/tomotoboy >cat grepstr   
  2. #!/bin/sh   
  3. #grepstr   
  4. echo -n "Enter a piece of text file:"  
  5. read TEXT   
  6. echo -n "Enter a string to query: "  
  7. read QUERY   
  8. if grep $QUERY $TEXT >/dev/null 2>&1  
  9. then   
  10.    echo "$QUERY is in $TEXT"  
  11.    #could do some processing here...   
  12. else   
  13.    echo "$QUERY is not in  $TEXT"  
  14. fi   
  15.   
  16. /home/l/g/tomotoboy >./grepstr   
  17. -n Enter a piece of text file:   
  18. sed.txt   
  19. -n Enter a string to query:   
  20. tomotoboy   
  21. tomotoboy is in sed.txt  

文件拷贝输出检查
下面测试文件拷贝是否正常,如果 cp命令并没有拷贝文件myfile到myfile.bak,则打印错误信息。注意错误信息中` basename $0`打印脚本名。如果脚本错误退出,一个好习惯是显示脚本名并将之定向到标准错误中。用户应该知道产生错误的脚本名。
Shell代码
  1. /home/l/g/tomotoboy >chmod u+x ifcp   
  2. /home/l/g/tomotoboy >ifcp   
  3. cp: cannot access myfile   
  4. ifcp: error could not copy the file   
  5. /home/l/g/tomotoboy >cat ifcp   
  6. #!/bin/sh   
  7. #ifcp   
  8. if cp myfile myfile.bak; then   
  9.  echo "good copy"  
  10. else   
  11.  echo "`basename $0`: error could not copy the file" >&2  
  12. fi   
  13. /home/l/g/tomotoboy >touch myfile   
  14. /home/l/g/tomotoboy >ifcp   
  15. good copy   
  16. /home/l/g/tomotoboy >  


当前目录测试
当运行一些管理脚本时,可能要在根目录下运行它,特别是移动某种全局文件或进行权限改变时。一个简单的测试可以获知是否运行在根目录下。下面脚本中变量DIRECTORY使用当前目录的命令替换操作,然后此变量值与 " / "字符串比较(/为根目录) 。如果变量值与字符串不等,则用户退出脚本,退出状态为1意味错误信息产生。
Shell代码
  1. /home/l/g/tomotoboy >ifpwd   
  2. You need to be in the root directory not /home/l/g/tomotoboy to run this script   
  3. /home/l/g/tomotoboy >cd /etc   
  4. /etc >cd /   
  5. / >/home/l/g/tomotoboy/ifpwd   
  6. / >cat ifpwd   
  7. cat: cannot open ifpwd   
  8. / >cat /home/l/g/tomotoboy/ifpwd   
  9. #!/bin/sh   
  10. #ifpwd   
  11. DIRECTORY=`pwd`   
  12. #grab the current directory   
  13. if [ "$DIRECTORY" != "/" ];then   
  14. #is it the root directory?   
  15. #no ,the direct output to standard  error,which is the screen by default.   
  16. echo "You need to be in the root directory not $DIRECTORY to run this script" >&2  
  17. #exit with a value of 1, an error   
  18. exit 1  
  19. fi  


文件权限测试
可以用i f语句测试文件权限,下面简单测试文件sed.txt是否可写
Shell代码
  1. /home/l/g/tomotoboy >ifwr sed.txt   
  2. You can write to sed.txt   
  3. /home/l/g/tomotoboy >cat ifwr   
  4. #!/bin/sh   
  5. #ifwr   
  6. if [ ! -w "$1" ]; then   
  7.   echo "You cannot write to $1" >&2  
  8. else   
  9.   echo "You can write to $1"  
  10. fi  


测试传递到脚本中的参数
if语句可用来测试传入脚本中参数的个数。使用特定变量$#,表示调用参数的个数。可以测试所需参数个数与调用参数个数是否相等。以下测试确保脚本有三个参数。如果没有,则返回一个可用信息到标准错误,然后代码退出并显示退出状态。如果参数数目等于3,则显示所有参数。
Shell代码
  1. /home/l/g/tomotoboy >cat ifparam   
  2. #!/bin/sh   
  3. #ifparam   
  4. if [ $# -lt 3 ]; then   
  5. #less than 3 parameters called,echo  a usage message and exit   
  6. echo "Usage: `basename $0` arg1 arg2 arg3" >&2  
  7. exit   
  8. fi   
  9. #good ,receive 3 params, let's echo them   
  10. echo "arg1: $1"  
  11. echo "arg2: $2"  
  12. echo "arg3: $3"  
  13. /home/l/g/tomotoboy >ifparam yang shi hai   
  14. arg1: yang   
  15. arg2: shi   
  16. arg3: hai  


决定脚本是否为交互模式
有时需要知道脚本运行是交互模式(终端模式)还是非交互模式(cron或at) 。脚本也许需要这个信息以决定从哪里取得输入以及输出到哪里,使用test命令并带有-t选项很容易确认这一点。如果test返回值为1,则为交互模式。
Shell代码
  1. /home/l/g/tomotoboy >cat ifinteractive   
  2. #!/bin/sh   
  3. #ifinteractive   
  4. if [ -t ];then   
  5. echo "We are interactive with a terminal"  
  6. else   
  7. echo "We must be running from some background process probably cron or at"  
  8. fi   
  9. /home/l/g/tomotoboy >ifinteractive   
  10. We are interactive with a terminal  

简单的if else语句
下一个if语句有可能是使用最广泛的:
Shell代码
  1. if条件   
  2. then   
  3. 命令1  
  4. else   
  5. 命令2  
  6. fi  
使用if语句的else部分可在条件测试为假时采取适当动作。
变量设置测试,下面的例子测试环境变量EDITOR是否已设置。如果EDITOR变量为空,将此信息通知用户。如果已设置,在屏幕上显示编辑类型。
Shell代码
  1. /home/l/g/tomotoboy >echo $EDITOR   
  2.   
  3. /home/l/g/tomotoboy >cat ifeditor   
  4. #!/bin/sh   
  5. #ifeditor   
  6. if [ -z "$EDITOR" ];then   
  7. #the variable has not been set   
  8. echo "Your EDITOR environment is not set"  
  9. else   
  10. #let's  us see what is it   
  11. echo "Using $EDITOR as the default editor"  
  12. fi   
  13. /home/l/g/tomotoboy >ifeditor   
  14. Your EDITOR environment is not set   
  15. /home/l/g/tomotoboy >  


检测最后命令状态
前面将目录名传入脚本创建了一个目录,脚本然后提示用户是否应创建目录。下面的例子创建一个目录,并从当前目录将所有 *.txt文件拷入新目录。但是这段脚本中用最后状态命令检测了每一个脚本是否成功执行。如果命令失败则通知用户。
Shell代码
  1. /home/l/g/tomotoboy >cat ifmkdir   
  2. #!/bin/sh   
  3. #ifmkdir   
  4. DIR_NAME=testdirec   
  5. #where we are?   
  6. THERE=`pwd`   
  7. #send all output to system dustbin   
  8. mkdir $DIR_NAME >/dev/null 2>&1  
  9. #is it a directory?   
  10. if [ -d $DIR_NAME ];then   
  11. #can we cd to the directory   
  12.   cd $DIR_NAME   
  13.   if [ $? = 0 ];then   
  14.   #yes we can   
  15.   HERE=`pwd`   
  16.   echo "$HERE"  
  17.   cp $THERE/*.txt $HERE   
  18.   else   
  19.    echo "Cannot cd to $DIR_NAME" >&2  
  20.    exit 1  
  21.   fi   
  22. else   
  23.   echo "Cannot create directory $DIR_NAME" >&2  
  24.   exit 1  
  25. fi  

简单的安全登录脚本
以下是用户登录时启动应用前加入相应安全限制功能的基本框架。首先提示输入用户名和密码,如果用户名和密码均匹配脚本中相应字符串,用户登录成功,否则用户退出。脚本首先设置变量为假—总是假定用户输入错误,stty当前设置被保存,以便隐藏passwd域中字符,然后重新保存stty设置。如果用户I D和密码正确(密码是myday) ,明亮INVALID_USER和INVALID_PASSWD设置为no表示有效用户或密码,然后执行测试,如果两个变量其中之一为yes,缺省情况下,脚本退出用户。键入有效的ID和密码,用户将允许进入。这是一种登录脚本的基本框架。下面的例子中有效用户ID为dave或tomotoboy。
Shell代码
  1. #!/bin/sh   
  2. #ifpass   
  3. #set the variables to false   
  4. INVALID_USER=yes   
  5. INVALID_PASSWD=yes   
  6. #set the current stty settings   
  7. SAVEDSTTY=`stty -g`   
  8. echo "You are logging into a sensitive area"  
  9. echo -n "Enter your ID name:"  
  10. read NAME   
  11. #hide the characters typed in   
  12. stty -echo   
  13. echo "Enter your password :"  
  14. read PASSWORD   
  15. #back on again   
  16. stty $SAVEDSTTY   
  17. if [ "$NAME" = "tomotoboy" ] || [ "$NAME" = "dave" ]; then   
  18.  #if a valid then set variable   
  19.     INVALID_USER=no   
  20. fi   
  21. if [ "$PASSWORD" = "myday" ];then   
  22.  #if valid password then set variable   
  23.     INVALID_PASSWD=no   
  24. fi   
  25. if [ "$INVALID_USER" = "yes" ] || [ "$INVALID_PASSWD" = "yes" ];then   
  26.    echo "`basename $0` : Sorry wrong password or userid"  
  27.    exit 1  
  28. fi   
  29. echo "corrent user id and password given"  


只有注册用户登录后才能发表评论。


网站导航: