鸟哥的Linux私房菜读书笔记--判断式

版权声明:本文属博主原创,转载请联系QQ528055624 https://blog.csdn.net/qq_41825534/article/details/82997383

1、利用test指令的测试功能

命令:$  test  -e  文件绝对路径    ##测试文件是否存在。此命令不显示信息,我们可以通过  $?   或  &&  及  ||来展现结果如下

命令:$  test  -e  文件绝对路径  &&   echo  "exist"  ||  echo  "no  exist"

测试的标志 代表意义
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 的判定上。 主要意义在判定,两个文件是否均指向同一个 inode 哩!
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) 这个文件是否存在,若不存在则给予一个『Filename does not exist』的讯息,并中断程序;
(2) 若这个文件存在,则判断他是个文件或目录,结果输出『Filename is regular file』或 『Filename is directory』
(3) 判断一下,执行者的身份对这个文件或目录所拥有的权限,并输出权限数据!

#!/bin/bash
#program
#    user input a dilename ,program will check the flowing
#    1>exist    2>file/directory    3>file permissions
#history
#    20181010    lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export  PATH

echo -e "please input a filename ,I will check the filename's type and permission\n\t"
read -p "filename:" filename
test -z ${filename}  && echo "you nust input a filename" && exit 0
test ! -e ${filename} && echo "the filename '${filename}'don't exist" && exit 0
#测试输入文件名
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} execuated"
#测试文件属性

echo "the filename :${filename} is a ${filetype}"
echo "and the permissions for you are :${perm}" 

上述脚本执行时需要输入脚本的绝对路径,即我们可以切换到其他目录查询相关的文件,但是在使用source命令时需要输入完整的脚本文件路径。注意,由于root权限在很多限制上面都是无效的,所以使用root执行这个脚本时,其与ls命令观察到的结果并不相同。建议使用执行者来执行该命令

2、利用判断符号[]

使用判断符号来进行数据的判断,例如我想知道${HOME}变量是否为空

命令: $  [ -z "{HOME}"] ; echo $?    ##中括号的两端需要用空格(□)进行分隔

[ "$HOME" == "$MAIL" ]
[□"$HOME"□==□"$MAIL"□]
  ↑               ↑     ↑             ↑

在bash当中= 与 == 结果是一样的,但是在惯用的程序写法中,一个等号代表变量的设定,两个等号代表逻辑判断之意。

注意:(1)在[ ]中的每个组件都需要空格键来分隔

(2)在中括号内的变数最好都以双引号扩起来

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

例:  当执行一个程序的时候,这个程序会让用户选择 Y 或 N ; 如果用户输入 Y 或 y 时,就显示『 OK, continue 』;如果用户输入 n 或 N 时,就显示『 Oh, interrupt !』;如果不是 Y/y/N/n 之内的其他字符,就显示『 I don't know what your choice is 』
利用中括号、 && 与 || 来继续吧!
 

#!/bin/bash
#program
#    this program shows the user's choice
#history
#    20181010    lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH
echo -e "you should enter Y or N\n"
read -p "please input Y/N to express your think:" yn
[ "${yn}" == "y" ] -o [ "${yn}" == "Y"] && echo "ok,will eontinue" && exit 2
[ "${yn}" == "n" ] -o [ "${yn}" == "N"] && echo "no.will stop" && exit 8
echo "I don't know whar your choice is " && exit 0                                                    

3、shell script的默认变数($0,$1……)

在指令中,我们可以使用参数与选项,那么shell script 后面可不可以带有参数呢?例如重启网络

命令:  $  file /etc/init.d/network    ##使用file查询后,系统告知该文件是bash可执行的script

命令 : $  /etc/init.d/network  restart     ##重启网络

命令 : $  /etc/init.b/network  stop    ##关闭网络

如果依据程序的执行给予一些变量去进行不用的任务时,可以使用read功能,但是read的缺陷是需要手动输入一些判断式。我们可以通过指令后面接参数,一个指令就能够处理完毕而不需要再次加入一些变量行为。

    /path/to/scriptname   opt1  opt2  opt3  opt4                                                                                                                                                  $0                     $1      $2     $3     $4

执行脚本档名为$0的变量,第一个接的参数就是$1。除了以上的数字变量外,还有较为特殊的变量可以在script中使用来呼叫这些参数。

    · $#   :代表后接的参数『个数』,以上表为例这里显示为『 4 』;
    · $@ :代表『 "$1" "$2" "$3" "$4" 』之意,每个变量是独立的(用双引号括起来);
    · $*   :代表『 "$1c$2c$3c$4" 』,其中 c 为分隔字符,默认为空格键, 所以本例中代表『 "$1 $2 $3 $4" 』之意。
例:执行一个携带参数的script。显示如下数据:程序名为何?共有几个参数?若参数小于2则告知使用者参数数量太少。全部的参数内容为何?第一个参数内容为何?第二个参数内容为何?

#!/bin/bash
#program
#    program shows the scripts name, parameters
#history
#    20181010    lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH

echo "this script name is      ==>${0}"    #显示文档名
echo "total parameter is       ==>$#  "    #显示参数数量
[ "$#" -lt 2 ] && echo "the number of parameter is less than 2.stop here." && exit 0
#如果参数数量小于2,告知使用者参数过少,大于而则执行下一步
echo "your total parameter is  ==>'$@'"    #显示参数的全部内容
echo "your first parameter is  ==>${1}"    #显示参数1
echo "your second parameter is ==>${2}"    #显示参数2hoe

#执行如下
$ sh how_paras.sh x w g
this scripts name is           ==>how_paras.sh
total parameter is             ==>3
your total parameter is        ==>x w g
your first parameter is        ==>x
your second parameter is       ==>w

<1>shift:造成参数变量号码偏移

#!/bin/bash
#program
#    program shows the effect of shift function
#history
#    20181010    lile    first release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/temp
export PATH

echo "total parameter number is      ==>$#"
echo "your whole parameter is        ==>'$@'"
shift
echo "total parameter number is      ==>$#"
echo "your whole parameter is        ==>'$@'"
shift
echo "total parameter number is      ==>$#"
echo "your whole parameter is        ==>'$@'"


[hadoop1@hadoop1 temp]$ source shift_paras.sh 1 2 3 4 5 6 7
total parameter number is      ==>7                  <==最原始的参数变量情况
your whole parameter is        ==>'1 2 3 4 5 6 7'
total parameter number is      ==>6                  <==第一次偏移,偏移量为1,参数1消失
your whole parameter is        ==>'2 3 4 5 6 7'
total parameter number is      ==>3                  <==第二次偏移。偏移量为3,参数234消失
your whole parameter is        ==>'5 6 7'

由上可知,shift具有移动变量的功能,shift后面接数字,即为拿掉几个变量的意思。

猜你喜欢

转载自blog.csdn.net/qq_41825534/article/details/82997383
今日推荐