Chinaunix
标题:
有关判断变量是否为空的疑问---新年也不忘写脚本啊有木有!
[打印本页]
作者:
wwr
时间:
2013-01-01 20:51
标题:
有关判断变量是否为空的疑问---新年也不忘写脚本啊有木有!
本帖最后由 wwr 于 2013-01-01 20:54 编辑
我要做一个脚本,让他自动判断login.defs中某些参数是否正确。但并不能保证这些参数肯定存在这个文件中。所以在分析参数值之前首先要判断这个参数是否存在,我用-z,-n,-!z,-!n去判断,都没有成功(系统永远认为是真),不知道是怎么回事,看来是我的方法用错了吧?
#!/bin/bash
mil=`cat /etc/login.defs |grep ^[^#]|grep PASS_MIN_LEN|cut -f 2`
if [ -!z $mil ]
then
echo "PASS_MIN_LEN ok!"
else
echo "PASS_MIN_LEN error!"
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" ] 是一个效果
[root@centos ~]# man test
TEST(1) User Commands TEST(1)
NAME
test - check file types and compare values
SYNOPSIS
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
DESCRIPTION
Exit with the status determined by EXPRESSION.
--help display this help and exit
--version
output version information and exit
An omitted EXPRESSION defaults to false. Otherwise, EXPRESSION is true or false and sets exit status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-G FILE
FILE exists and is owned by the effective group ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and read permission is granted
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and write permission is granted
-x FILE
FILE exists and execute (or search) permission is granted
Except for -h and -L, all FILE-related tests dereference symbolic links. Beware that parentheses need to be escaped (e.g., by back-
slashes) for shells. INTEGER may also be -l STRING, which evaluates to the length of STRING.
NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here. Please refer to your
shell’s documentation for details about the options it supports.
AUTHOR
Written by Kevin Braunsdorf and Matthew Bradburn.
REPORTING BUGS
Report bugs to <bug-coreutils@gnu.org>.
COPYRIGHT
Copyright ? 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of the GNU General Public License
<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law.
SEE ALSO
The full documentation for test is maintained as a Texinfo manual. If the info and test programs are properly installed at your site,
the command
info test
should give you access to the complete manual.
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