shell判断

退出shell脚本

exit  n (n的数值自定义)

if后面跟命令,if指令会直接取命令状态的返回结果,0为真,其他数为假。

文件测试

-e filename :测试文件是否存在,单目操作符。

-f filename :测试文件是否为普通文件。

-d filename :测试指定路径是否为目录。

-r、-w、-x :测试当前用户对指定文件是否用rwx权限。

测试脚本是否有语法错误

bash -n 脚本名称

bash -x 单步执行脚本

特殊变量

$?:上一个命令状态返回值

$#:参数的个数

$*:参数列表

[email protected]:参数列表

位置变量

$1

shift:引用一个参数后,执行shift,下一个参数成为$1

练习:

写一脚本,能接受1个参数

判断该参数存在,显示“ok”,否则显示“No Such File”

练习:

给脚本传递2个参数(整数),计算和与积。

时间: 2024-08-03 10:24:49

shell判断的相关文章

shell 判断文件、目录是否存在

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

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. #

[SHELL]判断一个命令是否存在

首先要说明的是,不要使用which来进行判断,理由如下: 1.which非SHELL的内置命令,用起来比内置命令的开销大,并且非内置命令会依赖平台的实现,不同平台的实现可能不同. # type type type is a shell builtin # type command command is a shell builtin # type which which is hashed (/usr/bin/which) 2.很多系统的which并不设置退出时的返回值,即使要查找的命令不存在,

shell判断文件是否存在

转自:http://www.cnblogs.com/sunyubo/archive/2011/10/17/2282047.html 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 "

shell判断一个变量是否为空

判断一个变量是否为空 . 1. 变量通过" "引号引起来 如下所示:,可以得到结果为 IS NULL. #!/bin/sh para1= if [ ! -n "$para1" ]; then echo "IS NULL" else echo "NOT NULL" fi 2. 直接通过变量判断 如下所示:得到的结果为: IS NULL #!/bin/sh para1= if [ ! $para1 ]; then echo &qu

[转] Linux shell判断文件和文件夹是否存在

shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #这里的-x 参数判断$myPath是否存在并且是否具有可执行权限 if [ ! -x "$myPath"]; then mkdir "$myPath" fi #这里的-d 参数判断$myPath是否存在 if [ ! -d "$m

shell判断文件类型和权限

shell  判断文件类型. -d 文件 判断该文件是否存在,并且是否为目录(是目录为真) -e文件 判断该文件是否存在(存在为真) -f文件 判断该文件是否存在,并且是否为文件(是普通文件为真) -r 如果有文件存在 ,判断文件是否具有读权限有读权限返回真-w如果有文件存在 ,判断文件是否具有写权限有写权限返回真-x如果有文件存在 ,判断文件是否具有执行权限有执行权限返回真 在shell中的写法一般是 eg:[空格-e 文件路径 空格] [ -e /tmp/index.php ] [ -e /

shell判断和比较

http://blog.chinaunix.net/uid-7553302-id-183648.html 1  shell 的$! ,$?, $$,[email protected] $n        $1 the first parameter,$2 the second... $#        The number of command-line parameters. $0        The name of current program. $?        Last comma

shell判断变量内容里包含特定字符串

shell判断变量内容里包含特定字符串 shell [ "$str" =~ "IEEE80211" ] && echo "it contains IEEE80211" [email protected] 2017-5-11

Shell 判断文件或文件夹是否存在

#shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文件,目录是否存在或者具有权限 folder="/var/www/" file="/var/www/log" # -x 参数判断 $folder 是否存在并且是否具有可执行权限 if [ ! -x "$folder"]; then mkdir &quo