Chinaunix

标题: 有关判断变量是否为空的疑问---新年也不忘写脚本啊有木有! [打印本页]

作者: wwr    时间: 2013-01-01 20:51
标题: 有关判断变量是否为空的疑问---新年也不忘写脚本啊有木有!
本帖最后由 wwr 于 2013-01-01 20:54 编辑

我要做一个脚本,让他自动判断login.defs中某些参数是否正确。但并不能保证这些参数肯定存在这个文件中。所以在分析参数值之前首先要判断这个参数是否存在,我用-z,-n,-!z,-!n去判断,都没有成功(系统永远认为是真),不知道是怎么回事,看来是我的方法用错了吧?
  1. #!/bin/bash
  2. mil=`cat /etc/login.defs |grep ^[^#]|grep PASS_MIN_LEN|cut -f 2`
  3. if [ -!z $mil ]
  4. then
  5. echo "PASS_MIN_LEN ok!"
  6. else
  7. echo "PASS_MIN_LEN error!"
  8. fi
复制代码
-z   是检查字符串为空时判断为真
-n 和-z的作用相反
作者: wwr    时间: 2013-01-01 21:08
碰到非常奇怪的事情了,刚刚是在sles上测试的。现在同样的脚本放到rhel上就没有这样的问题,但我发现只能用-z来做判断
作者: reyleon    时间: 2013-01-01 22:41
if [ -!z $mil ] ????


if [ ! -z $mil ]

or


if [ -n $mil ]
作者: seesea2517    时间: 2013-01-01 22:55
新年留贴。
作者: wwr    时间: 2013-01-01 23:54
回复 3# reyleon


    原来叹号放在最前面啊,谢谢了啦:)  另外我发现z和n貌似回馈的是一样的结果~
作者: blackold    时间: 2013-01-01 23:59
新年好!

啥事?
作者: waker    时间: 2013-01-02 09:06
[ -n "$mil" ]


作者: waker    时间: 2013-01-02 09:09
1.不要忘记命令行都是shell吃过一遍后喂给命令的
2.-z string/-n string用来判断一个字符串的长度是否为0,和变量有蛋的关系?
作者: blackold    时间: 2013-01-02 09:50
回复 8# waker

确实有个圆蛋关系!
   
作者: zooyo    时间: 2013-01-02 11:05
提示: 作者被禁止或删除 内容自动屏蔽
作者: reyleon    时间: 2013-01-02 11:40
回复 5# wwr


    [ ! -z "$var" ] 跟 [ -n "$var" ] 是一个效果
  1. [root@centos ~]# man test
  2. TEST(1)                          User Commands                         TEST(1)

  3. NAME
  4.        test - check file types and compare values

  5. SYNOPSIS
  6.        test EXPRESSION
  7.        test

  8.        [ EXPRESSION ]
  9.        [ ]
  10.        [ OPTION

  11. DESCRIPTION
  12.        Exit with the status determined by EXPRESSION.

  13.        --help display this help and exit

  14.        --version
  15.               output version information and exit

  16.        An omitted EXPRESSION defaults to false.  Otherwise, EXPRESSION is true or false and sets exit status.  It is one of:

  17.        ( EXPRESSION )
  18.               EXPRESSION is true

  19.        ! EXPRESSION
  20.               EXPRESSION is false

  21.        EXPRESSION1 -a EXPRESSION2
  22.               both EXPRESSION1 and EXPRESSION2 are true

  23.        EXPRESSION1 -o EXPRESSION2
  24.               either EXPRESSION1 or EXPRESSION2 is true

  25.        -n STRING
  26.               the length of STRING is nonzero

  27.        STRING equivalent to -n STRING

  28.        -z STRING
  29.               the length of STRING is zero

  30.        STRING1 = STRING2
  31.               the strings are equal

  32.        STRING1 != STRING2
  33.               the strings are not equal

  34.        INTEGER1 -eq INTEGER2
  35.               INTEGER1 is equal to INTEGER2

  36.        INTEGER1 -ge INTEGER2
  37.               INTEGER1 is greater than or equal to INTEGER2

  38.        INTEGER1 -gt INTEGER2
  39.               INTEGER1 is greater than INTEGER2

  40.        INTEGER1 -le INTEGER2
  41.               INTEGER1 is less than or equal to INTEGER2

  42.        INTEGER1 -lt INTEGER2
  43.               INTEGER1 is less than INTEGER2

  44.        INTEGER1 -ne INTEGER2
  45.               INTEGER1 is not equal to INTEGER2

  46.        FILE1 -ef FILE2
  47.               FILE1 and FILE2 have the same device and inode numbers

  48.        FILE1 -nt FILE2
  49.               FILE1 is newer (modification date) than FILE2

  50.        FILE1 -ot FILE2
  51.               FILE1 is older than FILE2

  52.        -b FILE
  53.               FILE exists and is block special

  54.        -c FILE
  55.               FILE exists and is character special

  56.        -d FILE
  57.               FILE exists and is a directory

  58.        -e FILE
  59.               FILE exists

  60.        -f FILE
  61.               FILE exists and is a regular file

  62.        -g FILE
  63.               FILE exists and is set-group-ID

  64.        -G FILE
  65.               FILE exists and is owned by the effective group ID

  66.        -h FILE
  67.               FILE exists and is a symbolic link (same as -L)

  68.        -k FILE
  69.               FILE exists and has its sticky bit set

  70.        -L FILE
  71.               FILE exists and is a symbolic link (same as -h)

  72.        -O FILE
  73.               FILE exists and is owned by the effective user ID

  74.        -p FILE
  75.               FILE exists and is a named pipe

  76.        -r FILE
  77.               FILE exists and read permission is granted

  78.        -s FILE
  79.               FILE exists and has a size greater than zero

  80.        -S FILE
  81.               FILE exists and is a socket

  82.        -t FD  file descriptor FD is opened on a terminal

  83.        -u FILE
  84.               FILE exists and its set-user-ID bit is set

  85.        -w FILE
  86.               FILE exists and write permission is granted

  87.        -x FILE
  88.               FILE exists and execute (or search) permission is granted

  89.        Except  for  -h and -L, all FILE-related tests dereference symbolic links.  Beware that parentheses need to be escaped (e.g., by back-
  90.        slashes) for shells.  INTEGER may also be -l STRING, which evaluates to the length of STRING.

  91.        NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here.  Please refer to your
  92.        shell’s documentation for details about the options it supports.

  93. AUTHOR
  94.        Written by Kevin Braunsdorf and Matthew Bradburn.

  95. REPORTING BUGS
  96.        Report bugs to <bug-coreutils@gnu.org>.

  97. COPYRIGHT
  98.        Copyright ? 2006 Free Software Foundation, Inc.
  99.        This   is   free   software.    You   may   redistribute   copies   of   it  under  the  terms  of  the  GNU  General  Public  License
  100.        <http://www.gnu.org/licenses/gpl.html>.  There is NO WARRANTY, to the extent permitted by law.

  101. SEE ALSO
  102.        The full documentation for test is maintained as a Texinfo manual.  If the info and test programs are properly installed at your site,
  103.        the command

  104.               info test

  105.        should give you access to the complete manual.

  106. test 5.97                        February 2010                         TEST(1)
复制代码

作者: winway1988    时间: 2013-01-02 21:07
回复 8# waker


    学习了




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2