Bash 判断(一)

来源:互联网 发布:lab series知乎 编辑:程序博客网 时间:2024/06/06 01:14

1、字符串判断

  1. str1 = str2      当两个串有相同内容、长度时为真
  2. str1 != str2      当串str1和str2不等时为真
  3. -n str1        当串的长度大于0时为真(串非空)
  4. -z str1        当串的长度为0时为真(空串)
  5. str1           当串str1为非空时为真
2、数字的判断
  1. int1 -eq int2    两数相等为真
  2. int1 -ne int2    两数不等为真
  3. int1 -gt int2    int1大于int2为真
  4. int1 -ge int2    int1大于等于int2为真
  5. int1 -lt int2    int1小于int2为真
  6. int1 -le int2    int1小于等于int2为真
3 文件的判断
  1. -b     当file存在并且是块文件时返回真
    -c     当file存在并且是字符文件时返回真
    -d     当pathname存在并且是一个目录时返回真
    -e     当pathname指定的文件或目录存在时返回真
    -f     当file存在并且是正规文件时返回真
    -g     当由pathname指定的文件或目录存在并且设置了SGID位时返回为真
    -h     当file存在并且是符号链接文件时返回真,该选项在一些老系统上无效
    -k     当由pathname指定的文件或目录存在并且设置了“粘滞”位时返回真
    -p     当file存在并且是命令管道时返回为真
    -r     当由pathname指定的文件或目录存在并且可读时返回为真
    -s     当file存在文件大小大于0时返回真
    -t file当文件描述符(默认为1)指定的设备为终端时为真
    -u     当由pathname指定的文件或目录存在并且设置了SUID位时返回真
    -w     当由pathname指定的文件或目录存在并且可执行时返回真。一个目录为了它的内容被访问必然是可执行的。
    -o     当由pathname指定的文件或目录存在并且被子当前进程的有效用户ID所指定的用户拥有时返回真。
    -x file用户可执行为真

3、复杂逻辑判断
  1. -a       与
  2. -o   或
  3. !    非



  4.   下面是一些使用实例:

  5. #!/bin/sh
  6. myPath="/var/log/httpd/"
  7. myFile="/var /log/httpd/access.log"

  8. #这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
  9. if [ ! -x "$myPath"]; then
  10. mkdir "$myPath"
  11. fi

  12. #这里的-d 参数判断$myPath是否存在
  13. if [ ! -d "$myPath"]; then
  14. mkdir "$myPath"
  15. fi

  16. #这里的-f参数判断$myFile是否存在
  17. if [ ! -f "$myFile" ]; then
  18. touch "$myFile"
  19. fi

  20. #其他参数还有-n,-n是判断一个变量是否是否有值
  21. if [ ! -n "$myVar" ]; then
  22. echo "$myVar is empty"
  23. exit 0
  24. fi

  25. #两个变量判断是否相等
  26. if [ "$var1" == "$var2" ]; then
  27. echo '$var1 eq $var2'
  28. else
  29. echo '$var1 not eq $var2'
  30. fi




  31.        if list then
  32.            do something here
  33.        elif list then
  34.            do another thing here
  35.        else
  36.          do something else here
  37.        fi   
EX1:
  1. #!/bin/sh

  2. SYSTEM=`uname -s`     #获取操作系统类型,我本地是linux

  3. if [ $SYSTEM = "Linux" ] ; then     #如果是linux的话打印linux字符串
  4. echo "Linux"
  5. elif [ $SYSTEM = "FreeBSD" ] ; then   
  6. echo "FreeBSD"
  7. elif [ $SYSTEM = "Solaris" ] ; then
  8. echo "Solaris"
  9. else
  10. echo "What?"
  11. fi     #ifend
基本上和其他脚本语言一样。没有太大区别。不过值得注意的是。[]里面的条件判断。

=========================================================================
  1. #. $HOME/.bash_profile
  2. cd $HOME
  3. sh .bash_profile
  4. cd $HOME/jnjzapp/sos_dx/
  5. echo -e '开始'
  6. ./jn_card_dx.sh
  7. wait
  8. #sqlplus $DBSTR <<START_A
  9. #@jn_card_dx.sql
  10. #/
  11. #commit;
  12. #exit;
  13. #START_A
  14. cd $HOME/jnjzapp/sos_dx/data/
  15. if [ ! -d  BQMSG ]; then 
  16. mkdir BQMSG 
  17. fi 
  18. #mkdir BQMSG
  19. cd $HOME/jnjzapp/sos_dx/
  20. sqlplus $DBSTR <<START
  21. @sos.sql;
  22. commit;
  23. exit;
  24. START
  25. objdir=`date -d ' days' +%Y%m%d`
  26. echo $objdir
  27. #cp jn_card_sos.txt $objdir.txt

  1. #!/bin/bash  
  2. # 问题描述:输入一个路径,如果该路径是指目录,则将该目录中的所有文件  
  3. # (包括文件夹)全部拷贝到另一个已经存在的用户test_user根目录下tmp  
  4. # 目录中的一个自定义的文件夹里,并更改其用户为test_user。  
  5. # (假设对所涉及到的相关目录及文件具有相应的读、写或可执行权限)  
  6.   
  7. echo "请输入路径:"  
  8. read PATH  
  9. if [ -d $PATH ]  
  10. then  
  11.     echo "该路径为目录,将执行拷贝。"  
  12.     echo ".................."  
  13.     /bin/mkdir  -p  ~test_user/tmp/dest_fold  
  14.     /bin/cp  -r  $PATH/*  ~test_user/tmp/dest_fold/  
  15.     /bin/chown  -R  test_user  ~test_user/tmp/dest_fold/  
  16.     echo "拷贝任务完成!"  
  17. else  
  18.     echo "该路径不是目录,退出。"  
  19. fi  

参考链接

http://www.blogjava.net/Jcat/archive/2008/04/13/192563.html

http://bbs.chinaunix.net/thread-3678071-1-1.html

http://blog.csdn.net/a454400828a/article/details/7240300