2023年8月2日发(作者:)
linuxshell中的条件判断语句
shell 判断语句流程控制 "if" 表达式 如果条件为真则执⾏then后⾯的部分:if ....; then .... elif ....; then .... else .... fi ⼤多数情况下,可以使⽤测试命令来对条件进⾏测试。⽐如可以⽐较字符串、判断⽂件是否存在及是否可读等等… 通常⽤" [ ] "来表⽰条件测试。注意这⾥的空格很重要。要确保⽅括号的空格。[ -f "somefile" ] :判断是否是⼀个⽂件[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执⾏权限[ -n "$var" ] :判断$var变量是否有值[ "$a" = "$b" ] :判断$a和$b是否相等
-r file ⽤户可读为真 -w file ⽤户可写为真 -x file ⽤户可执⾏为真 -f file ⽂件为正规⽂件为真 -d file ⽂件为⽬录为真 -c file ⽂件为字符特殊⽂件为真 -b file ⽂件为块特殊⽂件为真 -s file ⽂件⼤⼩⾮0时为真 -t file 当⽂件描述符(默认为1)指定的设备为终端时为真
-n variable 判断⼀个变量是否有值
-z variable 判断⼀个变量是否为⾮空字符串#########################################################含条件选择的shell脚本 对于不含变量的任务简单shell脚本⼀般能胜任。但在执⾏⼀些决策任务时,就需要包含if/then的条件判断了。shell脚本编程⽀持此类运算,包括⽐较运算、判断⽂件是否存在等。 基本的if条件命令选项有: -eq —⽐较两个参数是否相等(例如,if [ 2 –eq 5]) -ne —⽐较两个参数是否不相等 -lt —参数1是否⼩于参数2 -le —参数1是否⼩于等于参数2 -gt —参数1是否⼤于参数2 -ge —参数1是否⼤于等于参数2 -f — 检查某⽂件是否存在(例如,if [ -f "filename" ]) -d — 检查⽬录是否存在 ⼏乎所有的判断都可以⽤这些⽐较运算符实现。脚本中常⽤-f命令选项在执⾏某⼀⽂件之前检查它是否存在。################################################################# # 判断⽂件是否存在 #!/bin/sh today=`date -d yesterday+%y%m%d` file="apache_$" cd /home/chenshuo/shell if [ -f "$file" ];then echo "OK" else echo "error $file" > mail -s "failbackup from test" < fi =================================================判断⽂件,⽬录是否存在或者具有权限 2. #!/bin/sh 3. = "/var/log/httpd/" = "/var /log/httpd/" 6. 7.#这⾥的-x 参数判断$myPath是否存在并且是否具有可执⾏权限 [ ! -x "$myPath" ]; then "$myPath" 11. 12.#这⾥的-d参数判断$myPath是否存在 [ ! -d "$myPath" ]; then "$myPath" 16. 17.#这⾥的-f参数判断$myFile是否存在
[ ! -f "$myFile" ]; then "$myFile" 21. 22.#其他参数还有-n,-n是判断⼀个变量是否有值 [ ! -n "$myVar" ];then "$myVar is empty" 0 27. 28.#两个变量判断是否相等 [ "$var1" = "$var2" ]; then '$var1eq $var2' '$var1 not eq $var2'
/****************************************************************************/1 判断⼀个变量是否被定义if [ -z $EDITOR ]
2 判断交互模式if [ -t ] 3 测试⽂件权限if [ ! -w "$LOGFILE"]
4 测试SHELL命令if echo $list | grep "Peter" > /dev/null 2>&1
5 测试数值if [ "10" -lt "12" ] * -b file = True if the file exists and is block special file. 如果该⽂件存在并且是块特殊⽂件。
* -c file = True if the file exists and is character special file.如果该⽂件存在并且是字符特殊⽂件
* -d file = True if the file exists and is a directory. 如果该⽂件存在并且是⼀个⽬录。
* -e file = True if the file exists. 如果该⽂件存在
* -f file = True if the file exists and is a regular file 如果该⽂件存在并且是⼀个普通⽂件
* -g file = True if the file exists and the set-group-id bit is set. 如果该⽂件存在并且设置了组ID位。
* -k file = True if the files’ “sticky” bit is set. 如果⽂件的sticky “粘性”位被设置。
* -L file = True if the file exists and is a symbolic link. 该⽂件存在并且是⼀个符号链接。
* -p file = True if the file exists and is a named pipe. 该⽂件存在并且是⼀个命名管道。
* -r file = True if the file exists and is readable. ⽂件存在并且是可读的
* -s file = True if the file exists and its size is greater than zero. ⽂件存在,它的⼤⼩是⼤于零
* -S file = True if the file exists and is a socket. ⽂件存在并且是⼀个套接字
* -t fd = True if the file descriptor is opened on a terminal. ⽂件描述符是在⼀个终端上打开的
* -u file = True if the file exists and its set-user-id bit is set. ⽂件存在,它的设置⽤户ID位被设置了
* -w file = True if the file exists and is writable. ⽂件存在并且可写
* -x file = True if the file exists and is executable. ⽂件存在并且是可执⾏的
* -O file = True if the file exists and is owned by the effective user id. ⽂件存在并且是所拥有的有效⽤户ID
* -G file = True if the file exists and is owned by the effective group id. ⽂件存在并且拥有有效的gruop id。* file1 -nt file2 = True if file1 is newer, by modification date, than file2. 如果file1更新
* file1 ot file2 = True if file1 is older than file2. 如果file1更旧
* file1 ef file2 = True if file1 and file2 have the same device and inode 1和file2有相同的设备和节点号
* -z string = True if the length of the string is 0. 字符串的长度为0
* -n string = True if the length of the string is non-zero. 字符串的长度不为零
* string1 = string2 = True if the strings are equal.
*string1 != string2 = True if the strings are not equal.
!expr = True if the expr evaluates to false.
* expr1 -a expr2 = True if both expr1 and expr2 are true. 且为真
* expr1 -o expr2 = True is either expr1 or expr2 is true. 或两个档案之间的判断与⽐较 ;例如『 test file1 -nt file2 』 * -nt 第⼀个档案⽐第⼆个档案新 * -ot 第⼀个档案⽐第⼆个档案旧个档案与第⼆个档案为同⼀个档案( link 之类的档案)逻辑的『和(and)』『或(or)』 * && 逻辑的 AND 的意思 * || 逻辑的 OR 的意思第⼀ * -ef 运算符号 代表意义 = 等于 != 不等于 < ⼩于 > ⼤于 -eq 等于 -ne 不等于 -lt ⼩于 -gt ⼤于 -le ⼩于或等于 -ge ⼤于或等于 -a 双⽅都成⽴(and)-o 单⽅成⽴(or) -z 空字符串 -n ⾮空字符串/************************************************************/格式如下,在⽐较时,数字和字符串⽤不同的⽐较符号1.如果a>b且a /************************************************************/test命令⽤法。功能:检查⽂件和⽐较值1)判断表达式 if test (表达式为真) if test !表达式为假 -----2. ⾃增的写法1. i=`expr $i + 1`;2. let i+=1;3. ((i++)); // not support by some shell.4. i=$[$i+1]; //not support by some shell.5. i=$(( $i + 1 )) 嵌⼊式平台上发现,3, 4不⽀持。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1690917741a463126.html
评论列表(0条)