首页 > 课堂 > 技术开发 > 正文

shell中条件测试

2020-05-27 13:35:52
字体:
来源:转载
供稿:网友

   1.test语句

  当我要检测系统上面某些档案戒者是相关的属性时,就得用test指令。

  (1) 关于某个档名的文件类型判断,如 test -e filename 表示存在

  -e 该『档名』是否存在?(常用)

  -f 该『档名』是否存在且为档案(file)?(常用)

  -d 该『文件名』是否存在且为目录(directory)?(常用) -b 该『档名』是否存在且为一个 block device 装置?

  -c 该『档名』是否存在且为一个character device 装置?

  -S 该『档名』是否存在且为一个 Socket 档案?

  -p 该『档名』是否存在且为一个 FIFO (pipe) 档案?

  -L 该『档名』是否存在且为一个连结档?

  (2) 关于档案的权限检测,如 test -r filename 表示可读否 (但 root 权限常有例外)

  -r 侦测该档名是否存在且具有『可读』的权限?

  -w 侦测该档名是否存在且具有『可写』的权限?

  -x 侦测该档名是否存在且具有『可执行』的权限?

  -u 侦测该文件名是否存在且具有『SUID』的属性?

  -g 侦测该文件名是否存在且具有『SGID』的属性?

  -k 侦测该文件名是否存在且具有『Sticky bit』的属性?

  -s 侦测该档名是否存在且为『非空白档案』?

  (3)两个文档之间的比较 test file1 -nt file2

  -nt (newer than)判断 file1 是否比 file2 新

  -ot (older than)判断 file1 是否比 file2 旧

  -ef 判断 file1 和file2 是否为同一档案,可用在判断 hard link 的判定上。

  (4) 关于两个整数之间的判定,例如 test n1 -eq n2

  -eq 两数值相等 (equal)

  -ne 两数值不等 (not equal)

  -gt n1 大于 n2 (greater than)

  -lt n1 小于 n2 (less than)

  -ge n1 大于等于 n2 (greater than or equal)

  -le n1 小于等于 n2 (less than or equal)

  (5)判定字符串的数据

  test -z string 判定字符串是否为 0 ?若 string 为空字符串,则为 true

  test -n string 判定字符串是否非为 0 ?若 string 为空字符串,则为 false。注: -n 亦可省略

  test str1 = str2 判定 str1 是否等于 str2 ,若相等,则回传 true

  test str1 != str2 判定 str1 是否不等于 str2 ,若相等,则回传 false

  (6)多重条件判定,例如: test -r filename -a -x filename

  -a (and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与x 权限时,才回传 true。

  -o (or)两状况任何一个成立!例如 test -r file -o -x file,则 file 具有 r 或者 x 权限时,就可回传 true。

  ! 反相状态, 如 test ! -x file ,当 file 不具有 x 时,回传 true。

  例子:

  # 1. 让使用者输入档名,并判断使用者是否真的有输入字符串?

  echo -e "Please input a filename, I will check the filenames type and

  permission. nn"

  read -p "Input a filename : " filename

  test -z $filename && echo "You MUST input a filename." && exit 0

  # 2. 判断档案是否存在?若不存在则显示讯息结束脚本

  test ! -e $filename && echo "The filename $filename DO NOT exist" &&

  exit 0

  # 3. 开始判断文件类型与属性

  test -f $filename && filetype="regulare file"

  test -d $filename && filetype="directory"

  test -r $filename && perm="readable"

  test -w $filename && perm="$perm writable"

  test -x $filename && perm="$perm executable"

  # 4. 开始输出信息!

  echo "The filename: $filename is a $filetype"

  echo "And the permissions are : $perm"

  2.利用判断符号 [ ]

  除了我们很喜欢使用的 test之外,其实,我们还可以常用判断符号[](就是中括号啦)来进行数据的判断呢!但要注意以下几点:

  (1)在中括号 []内的每个组件都需要有空格键来分隔;

  (2)在中括号内的发数,最好都以双引号括号起来;

  (3)在中括号内的常数,最好都以单或者双引号括号起来。

  例子:

  #!/bin/bash

  PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

  export PATH

  read -p "Please input (Y/N): " yn

  [ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0

  [ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0

  echo "I dont know what your choice is" && exit 0

  3.shell中经常在测试中用到的几个常数

  (1)执行的脚本档名为 $0这个变量,第一个接的参数就是 $1,依次类推。

  (2) $# :代表后接的参数『个数』,$0不算在里面。

  (3) $@:代表『 "$1" "$2" "$3" "$4"』之意,每个变量是独立的(用双引号括起来)。

  (4) $*:代表『 "$1c$2c$3c$4"』,其中 c 为分隔字符,默认为空格键,所以本例中代表『 "$1 $2 $3 $4"』之意。

  例子:

  脚本如下:

  echo "The script name is ==> $0"

  echo "Total parameter number is ==> $#"

  [ "$#" -lt 2 ] && echo "The number of parameter is less than 2. Stop

  here."

  && exit 0

  echo "Your whole parameter is ==> $@"

  echo "The 1st parameter ==> $1"

  echo "The 2nd parameter ==> $2"

  执行结果:

  [root@www scripts]# sh sh07.sh theone haha quot

  The script name is ==> sh07.sh <==檔名

  Total parameter number is ==> 3 <==果然有三个参数

  Your whole parameter is ==> theone haha quot <==参数的全部内容

  The 1st parameter ==> theone <==第一个参数

  The 2nd parameter ==> haha <==第二个参数

  注:还可以用shift进行参数偏移

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表