shell 的一些条件判断

1. shell判断文件,目录是否存在或者具有权限
2. #!/bin/sh
3.
4. myPath="/var/log/httpd/"
5. myFile="/var /log/httpd/access.log"
6.
7. # 这里的-x 参数判断$myPath是否存在并且是否具有可执行权限
8. if [ ! -x "$myPath"]; then
9. mkdir "$myPath"
10. fi
11.
12. # 这里的-d 参数判断$myPath是否存在
13. if [ ! -d "$myPath"]; then
14. mkdir "$myPath"
15. fi
16.
17. # 这里的-f参数判断$myFile是否存在
18. if [ ! -f "$myFile" ]; then
19. touch "$myFile"
20. fi
21.
22. # 其他参数还有-n,-n是判断一个变量是否是否有值
23. if [ ! -n "$myVar" ]; then
24. echo "$myVar is empty"
25. exit 0
26. fi
27.
28. # 两个变量判断是否相等
29. if [ "$var1" = "$var2" ]; then
30. echo ‘$var1 eq $var2‘
31. else
32. echo ‘$var1 not eq $var2‘
33. fi

-f 和-e的区别
Conditional Logic on Files

-a file exists.
-b file exists and is a block special file.
-c file exists and is a character special file.
-d file exists and is a directory.
-e file exists (just the same as -a).
-f file exists and is a regular file.
-g file exists and has its setgid(2) bit set.
-G file exists and has the same group ID as this process.
-k file exists and has its sticky bit set.
-L file exists and is a symbolic link.
-n string length is not zero.
-o Named option is set on.
-O file exists and is owned by the user ID of this process.
-p file exists and is a first in, first out (FIFO) special file or
named pipe.
-r file exists and is readable by the current process.
-s file exists and has a size greater than zero.
-S file exists and is a socket.
-t file descriptor number fildes is open and associated with a
terminal device.
-u file exists and has its setuid(2) bit set.
-w file exists and is writable by the current process.
-x file exists and is executable by the current process.
-z string length is zero.

是用 -s 还是用 -f 这个区别是很大的!

时间: 2024-08-10 00:07:23

shell 的一些条件判断的相关文章

Shell脚本IF条件判断和判断条件总结

转自:http://m.jb51.net/article/56553.htm 这篇文章主要介绍了Shell脚本IF条件判断和判断条件总结,本文先是给出了IF条件判断的语法,然后给出了常用的判断条件总结,需要的朋友可以参考下 前言:      无论什么编程语言都离不开条件判断.SHELL也不例外. if list then           do something here       elif list then           do another thing here       e

Shell中的条件判断语句if~then~fi

Shell中的条件判断语句是前面一篇"Shell中的条件测试语句"的升级篇,也就是说,前面的测试语句是为了现在的判断语句if~then~fi语句服务的. 我们还是按照注意点和代码实现的方式铺开: 1)基本的if-then-fi语句可以用来判断基本的单层的分支结构,其形式如下: 其中if后面的测试语句一般都使用[]命令来做.如下面的例子: #-----------------------------/chapter4/ex4-18.sh------------------ #! /bin

Bourne Shell中的条件判断

条件判断是一个程序获得智能的基础,而Bourne Shell脚本则通过命令 [ 来模拟大多数编程语言中的条件表达式. shell中支持的控制结构有: (1) if then else fi (2) for in do done (3) while do done 第二种主要用于遍历,可能不需要条件判断,其它两种则免不了和 [ 命令共同使用了.下面讲解这个命令如何模拟条件表达式. 文件/目录判断[ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真.[ -c FILE ] 如果 FI

Shell学习笔记 - 条件判断式

1. 判断格式 1) test 参数 文件 例: test -e /root/install.log 2) [ 参数 文件 ]  -- 推荐使用 例: [ -e /root/install.log ] 注意:中括号后面和前面需要有空格 2. 判断文件类型参数 1)-d 文件:判断该文件是否存在,并且是否为目录文件 2)-e 文件:判断文件是否存在 3)-f 文件:判断文件是否存在,并且是否为普通文件 4)-s 文件:判断文件是否存在,并且是否为非空 5)其他文件类型判断: -b 块设备文件:-c

shell 编程if条件判断与if 真假判断

if条件判断与if真假判断 目录: 1.正确写法 2.错误写法 3.总结 一.正确写法 在编写shell脚本时,为简化代码的行号及结构的简约型,通常将命令执行结果和判断通过一条语句进行编写(在C语言编写程序时,经常遇到此种写法),如: [[email protected] ~]#touch test.sh if  useradd root &>/dev/null ; then    #如果用户添加成功,则不显示,否则显示用户添加失败     echo "user1 created

Linux Shell脚本 之 条件判断

首先想到的就是if else条件判断语句了,下面给出一个全面的语句: if condition then condition is true execute all commands up to elif statement elif condition1 then condition1 is true execute all commands up to elif statement elif condition2 then condition2 is true execute all com

Shell编程之条件判断与流程控制

一.条件判断式语句 1-1.概述 用来判断是与否,客观的而非主观判断,用于进行基本的管理 1-2.按文件类型判断 补充: && 命令1&&命令2 逻辑与(没有数据传递) 当命令1正确执行,则命令2才会执行 当命令1执行不正确,则命令2不会执行 || 命令1||命令2 逻辑或(没有数据传递) 当命令1执行不正确时,命令2才会执行 当命令1正确执行时,命令2不会执行 按文件类型判断 记住蓝色部分 两种判断格式: test –e /root/install.log [-e /ro

Shell脚本编程---条件判断if

SHELL脚本编程---判断 1.条件判断 条件判断在bash分为整数判断,字符判断和文件判断. 1-1条件判断的表达式: ① [ expression ] (注意单词词头和词尾都需要空格) ② [[ expression ]](注意同上) ③ test expression 1-2 整数比较 ① -eq  测试两个数是否相等,相等为真,不等为假 ② -ne  测试两个数是否不等, 反之. ③ -gt (大于),-lt (小于),-ge(大于或等于),-le(小于或等于) 逻辑与:&&  

linux基础之shell编程(2)-条件判断,算数运算,测试

bash中如果实现条件判断? 条件测试类型 整数测试 字符测试 文件测试 条件测试的表达式 有三种 [ expression ] --方括号与表达式之间一定要有一个空格 [[ expression ]] test expression 整数测试 -eq: 测试两个整数是否相等 例:$A -eq $B -ne: 测试两个整数是否不等 不等为真,相等为假 -gt: 测试一个数是否大于另一个数:大于,为真:否则,为假: -lt: 测试一个数是否小于另一个数:小于,为真:否则,为假: -ge: 大于或等

linux shell中的条件判断

1. 退出状态 在Linux系统中,每当一条命令执行完成后,系统都会返回一个退出状态,这个状态被存放在$? 这个变量中,是一个整数值,我们可以根据这个值来判断命令运行的结果是否正确. 通常情况下,退出状态值为0,表示执行成功,不为0的时候表示执行失败. POSIX规定的退出状态和退出状态的含义: 0       运行成功 1-255   运行失败,脚本命令.系统命令错误或参数传递错误 126     找到了该命令但无法执行 127     未找到要运行的命令 128     命令被系统强行结束