shell脚本编程测试类型下
?
?
?
?
?
一bash的數(shù)值測試
?
-v VAR
變量VAR是否設置
數(shù)值測試:
-gt 是否大于greater
-ge 是否大于等于
-eq 是否等于
-ne 是否不等于 ?not equal
-lt 是否小于
-le 是否小于等于
?
?
?
?
?
?
?
-eq是否等于表示變量值是數(shù)字,=表示變量值是字符串
?
[root@centos7 ~]# num=10; [[ "$num" -eq 10 ]] && echo true || echo false true [root@centos7 ~]# num=50; [[ "$num" -eq 10 ]] && echo true || echo false false [root@centos7 ~]# num=50; [[ "$num" = 10 ]] && echo true || echo false false [root@centos7 ~]# num=50; [[ "$num" = 50 ]] && echo true || echo false true?
?
?
?
?
[root@centos7 ~]# num=abcd; [ "$num" = 50 ] && echo true || echo false false [root@centos7 ~]# num=abcd; [ "$num" -eq 50 ] && echo true || echo false -bash: [: abcd: integer expression expected #語法錯誤,要求整數(shù)表達式 false [root@centos7 ~]# num=50; [ "$num" = 50 ] && echo true || echo false true [root@centos7 ~]# num=20; [ "$num" -ne 50 ] && echo true || echo false true [root@centos7 ~]# num=20; [ "$num" -le 50 ] && echo true || echo false true [root@centos7 ~]# num=20; [ "$num" -ge 50 ] && echo true || echo false false [root@centos7 ~]# num=20; [ "$num" -gt 50 ] && echo true || echo false false?
?
?
?
?
示例:判斷變量的參數(shù)是否存在
?
?
完整腳本:
[root@centos73 shell_scripts]# cat createuser1.sh #!/bin/bash #Author=wang [ $# -ne 1 ] && echo -e "the arg must one\nUsage:createuser1.sh usename" && exit 20#如果參數(shù)的個數(shù)不為1,那么就顯示必須要有一個參數(shù),并且退出。
#\n表示空一行,-e表示啟用反斜線轉(zhuǎn)義,對\進行轉(zhuǎn)義
id $1 &> /dev/null && echo " $1 is exist " && exit 8
#因為不是正常結束的命令,所以退出的狀態(tài)碼為非0
useradd $1 && echo "$1 is created"
#因為是最后一個命令了,不寫退出狀態(tài)也表示退出了。
?
?
?
?
?
執(zhí)行結果:
[root@centos7 bin]# chmod +x createuser1.sh [root@centos7 bin]# ll createuser1.sh -rwxr-xr-x 1 root root 342 Dec 15 17:52 createuser1.sh [root@centos7 bin]# createuser1.sh /root/bin/createuser1.sh: line 2: [: ne: binary operator expected is exist [root@centos7 bin]# vim createuser1.sh [root@centos7 bin]# createuser1.sh the arg must one Usage:createuser1.sh usename [root@centos7 bin]# createuser1.sh wang wang is exist [root@centos7 bin]# createuser1.sh tom tom is exist [root@centos7 bin]# createuser1.sh abcd abcd is created [root@centos7 bin]# id abcd uid=2001(abcd) gid=2001(abcd) groups=2001(abcd)?
?
?
?
?腳本解析:
echo -e
-e enable interpretation of backslash escapes 啟用反斜杠轉(zhuǎn)義的解釋
?
?
?
?
?
?
判斷變量是否已經(jīng)設置了
?查看test的幫助文檔
[root@centos73 ~]# help test | grep VAR-v VAR True if the shell variable VAR is set?
?
?
?
?
注意這里的VAR是不需要加$的
在字符串前面加$就是用來調(diào)用這個變量,相當于變量引用。
[root@centos7 bin]# var="";[ -v var ] && echo true || echo false#定義了空值 true [root@centos7 bin]# var=" ";[ -v var ] && echo true || echo false
#有內(nèi)容,是空格 true [root@centos7 bin]# var=123;[ -v var ] && echo true || echo false
#定義了數(shù)字 true [root@centos7 bin]# var=abcd;[ -v var ] && echo true || echo false true [root@centos7 bin]# var="abc";[ -v var ] && echo true || echo false
#定義了字符串 true [root@centos7 bin]# unset var;[ -v var ] && echo true || echo false false
?
?
?
?
?
判斷變量是否定義了?
[root@centos73 shell_scripts]# cat createuser2.sh #!/bin/bash #Author=wang[ -v $1 ] || ( echo -e "the arg must one\nUsage:$0.sh usename" && exit 20; ) #\n表示空一行,-e表示啟用反斜杠轉(zhuǎn)義的解釋,因為后面要空行。 #如果為假才執(zhí)行后面括號里面的命令,但是小括號會報錯,小括號開了一個子進程,exit 20退出的是子進程,但是沒有退出整個腳本。 #系統(tǒng)里面本身就有$1,只不過默認是沒有賦值,不能判斷$1是否存在。? id $1 &> /dev/null && echo " $1 is exist " && exit 8 #因為不是正常結束的命令,所以退出的狀態(tài)碼為非0 useradd $1 && echo "$1 is created" #因為是最后一個命令了,不寫退出狀態(tài)也表示退出了。?
?
?
?
?
執(zhí)行結果報錯,說明上面的腳本有問題,還要對其修改
[root@centos73 shell_scripts]# bash createuser2.sh is exist [root@centos73 shell_scripts]# bash createuser2.sh wang the arg must one Usage:createuser1.sh usename wang is exist [root@centos73 shell_scripts]# id wang uid=1022(wang) gid=1022(wang) groups=1022(wang) [root@centos73 shell_scripts]# id xixixi id: xixixi: no such user [root@centos73 shell_scripts]# bash createuser2.sh xixixi the arg must one Usage:createuser1.sh usename xixixi is created [root@centos73 shell_scripts]# id xixixi uid=1025(xixixi) gid=1025(xixixi) groups=1025(xixixi)?
?
?
?
?
判斷變量$1的值是否有內(nèi)容
使用-n,如果字符串為非空就為真,為空返回的就是假,假就執(zhí)行后續(xù)的命令。
[root@centos73 shell_scripts]# cat createuser3.sh #!/bin/bash #Author=wang [ -n "$1" ] || { echo -e "the arg must one\nUsage:$0.sh usename" && exit 20; } #\n表示空一行,-e表示啟用反斜杠轉(zhuǎn)義的解釋,因為后面要空行。#[-n]表示后面接的字符串不為空。 #-n STRING the length of STRING is nonzero id $1 &> /dev/null && echo "$1 is exist"
?
?
?
?
?
在系統(tǒng)腳本里面使用了很多的函數(shù),用大括號來表示
[root@centos7 bin]# cat /etc/init.d/functions systemctl_redirect () {local slocal prog=${1##*/}local command=$2 local options="" case "$command" in start) s=$"Starting $prog (via systemctl): " ;; stop) s=$"Stopping $prog (via systemctl): " ;; reload|try-reload) s=$"Reloading $prog configuration (via systemctl): " ;; restart|try-restart|condrestart) s=$"Restarting $prog (via systemctl): " ;; esac if [ -n "$SYSTEMCTL_IGNORE_DEPENDENCIES" ] ; then options="--ignore-dependencies" fi if ! systemctl show "$prog.service" > /dev/null 2>&1 || \ systemctl show -p LoadState "$prog.service" | grep -q 'not-found' ; then action $"Reloading systemd: " /bin/systemctl daemon-reload fi action "$s" /bin/systemctl $options $command "$prog.service" }?
?
?
?
?
?
執(zhí)行結果
[root@centos73 shell_scripts]# bash createuser3.sh the arg must one Usage:createuser3.sh.sh usename [root@centos73 shell_scripts]# bash createuser3.sh wang wang is exist [root@centos73 shell_scripts]# bash createuser3.sh zhang zhang is exist [root@centos73 shell_scripts]# bash createuser3.sh zhao zhao is exist [root@centos73 shell_scripts]# bash createuser3.sh hahaha [root@centos73 shell_scripts]# id hahaha id: hahaha: no such user [root@centos73 shell_scripts]# id wuwuwu id: wuwuwu: no such user [root@centos73 shell_scripts]# bash createuser3.sh wuwuwu [root@centos73 shell_scripts]# id wuwuwu id: wuwuwu: no such user?
?
?
?
?
?
?
二Bash的文件測試
?
如果在編程時需要處理一個對象,應先對對象進行測試。
只有在確定它符合要求時,才應進行操作處理,這樣做的好處就是避免程序出錯及無謂的系統(tǒng)資源消耗。
這個需要測試的對象可以是文件、字符串、數(shù)字等。
Bash的文件測試也就是判斷各種文件是否存在.
?
?
(一)常用文件測試操作符
?
下面的操作符號對于[[ ]]、[ ]、test的測試表達式幾乎是通用的,更多的操作符可以man test獲得幫助。
?
-a文件,表示文件存在則為真,即測試表達式成立。
?
-b文件, b的全拼為block表示文件存在且為塊設備則為真,即測試表達式成立。
?
-c文件, c的全拼為character表示文件存在且為字符設備則為真,即測試表達式成立。
?
-d文件, d的全拼為directory表示文件存在且為目錄則為真,即測試表達式成立。
?
注意目錄也是文件,是一種特殊的文件。
?
-e文件, e的全拼為exist表示文件存在則為真,即測試表達式成立。
?
-f文件,f的全拼為file表示文件存在且為普通文件則為真,即測試表達式成立。
?
注意區(qū)別于"-f",-e不辨別是目錄還是文件。
?
-L文件, L的全拼為link表示文件存在且為鏈接文件則為真,即測試表達式成立
?
-p 文件:p的全拼為pipe表示文件存在且為命名管道文件則為真,即測試表達式成立。
?
-r文件, r的全拼為read表示文件存在且可讀則為真,即測試表達式成立
?
-s文件, s的全拼為size表示文件存在且文件大小不為0則為真,即測試表達式成立
?
-S文件, S的全拼為socket表示文件存在且為套接字文件則為真,即測試表達式成立
?
-w文件, w的全拼為write表示文件存在且可寫則為真,即測試表達式成立
?
-x文件, x的全拼為executable表示文件存在且可執(zhí)行則為真,即測試表達式成立
?
f1-nt f2, nt的全拼為newer than表示文件f1比文件2舊則為真,即測試表達式成立。根據(jù)文件的修改時間來計算
?
fl-ot f2, ot的全拼為older than表示文件f1比文件12新則為真,即測試表達式成立。根據(jù)文件的修改時間來計算
?
?
查看test的幫助文檔
[root@centos73 shell_scripts]# help test test: test [expr]Evaluate conditional expression.Exits with a status of 0 (true) or 1 (false) depending on the evaluation of EXPR. Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. There are string operators and numeric comparison operators as well. The behavior of test depends on the number of arguments. Read the bash manual page for the complete specification. File operators: -a FILE True if file exists. -b FILE True if file is block special. -c FILE True if file is character special. -d FILE True if file is a directory. -e FILE True if file exists. -f FILE True if file exists and is a regular file. -g FILE True if file is set-group-id. -h FILE True if file is a symbolic link. -L FILE True if file is a symbolic link. -k FILE True if file has its `sticky' bit set. -p FILE True if file is a named pipe. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -S FILE True if file is a socket. -t FD True if FD is opened on a terminal. -u FILE True if the file is set-user-id. -w FILE True if the file is writable by you. -x FILE True if the file is executable by you. -O FILE True if the file is effectively owned by you. -G FILE True if the file is effectively owned by your group. -N FILE True if the file has been modified since it was last read. FILE1 -nt FILE2 True if file1 is newer than file2 (according to modification date). FILE1 -ot FILE2 True if file1 is older than file2. FILE1 -ef FILE2 True if file1 is a hard link to file2. String operators: -z STRING True if string is empty. -n STRING STRING True if string is not empty. STRING1 = STRING2 True if the strings are equal. STRING1 != STRING2 True if the strings are not equal. STRING1 < STRING2 True if STRING1 sorts before STRING2 lexicographically. STRING1 > STRING2 True if STRING1 sorts after STRING2 lexicographically. Other operators: -o OPTION True if the shell option OPTION is enabled. -v VAR True if the shell variable VAR is set ! EXPR True if expr is false. EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne, -lt, -le, -gt, or -ge. Arithmetic binary operators return true if ARG1 is equal, not-equal, less-than, less-than-or-equal, greater-than, or greater-than-or-equal than ARG2. Exit Status: Returns success if EXPR evaluates to true; fails if EXPR evaluates to false or an invalid argument is given.?
?
?
?
?
?
?
?
?
(二)測試文件存在性
?
注意中括號里面的內(nèi)容要和中括號左右空一格
[root@centos73 ~]# [ -a /etc/] && echo true -bash: [: missing `]' [root@centos73 ~]# [ -a /etc/ ] && echo true true [root@centos73 ~]# [ -a /etc ] && echo true true?
?
?
?-e和-a都是判斷文件是否存在
[root@centos73 ~]# [ -e /etc/] && echo true -bash: [: missing `]' [root@centos73 ~]# [ -e /etc] && echo true -bash: [: missing `]' [root@centos73 ~]# [ -e /etc ] && echo true true [root@centos73 ~]# [ -e /etc/ ] && echo true true?
?
?
?
?
?
?
示例
1、編寫腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數(shù)。
如果參數(shù)個數(shù)小于1,則提示用戶“至少應該給一個參數(shù)”,并立即退出。
如果參數(shù)個數(shù)不小于1,則顯示第一個參數(shù)所指向的文件中的空白行數(shù)。
涉及到正則表達式
法1:
完整腳本
[root@centos73 shell_scripts]# cat argsnum.sh #!/bin/bash #Author=wang [ $# -lt 1 ] && echo "must one parameter" && exit 1 [ ! -f $1 ] && echo " file not exist" && exit 2 #[ -f $1 ] 表示判斷文件存在,!表示取反,也就是文件不存在 echo `cat $1 |grep "^[[:space:]]*$" |wc -l`?
?
?
?
執(zhí)行結果
[root@centos73 shell_scripts]# bash argsnum.sh must one parameter [root@centos73 shell_scripts]# bash argsnum.sh /etc/issue 1 [root@centos73 shell_scripts]# bash argsnum.sh /etc/passwd 0 [root@centos73 shell_scripts]# bash argsnum.sh /etc/services 17 [root@centos73 shell_scripts]# bash argsnum.sh xxxxxx file not exist?
?
?
?
?
法2:
完整腳本
[root@centos73 shell_scripts]# cat argsnum1.sh #!/bin/bash #Author=wang [ $# -lt 1 ] && echo "You shound give a parameter at least!" && exit 10 [ -e $1 ] && echo "The blankLine is `grep '^[[:space:]]*$' $1 | wc -l`" || echo "No such file or directory!" #注意要使用反引號調(diào)用命令執(zhí)行的結果。#如果文件存在那么就打印文件的空白行,否則就顯示文件不存在。
?
?
?
?
?
?注意一定要加上*?
[root@centos73 shell_scripts]# grep '^[[:space:]]*$' /etc/issue | wc -l 1 [root@centos73 shell_scripts]# grep '^[[:space:]]$' /etc/issue | wc -l 0?
?
?
?
?
執(zhí)行結果
[root@centos73 shell_scripts]# bash argsnum1.sh You shound give a parameter at least! [root@centos73 shell_scripts]# bash argsnum1.sh /etc/issue The blankLine is 1 [root@centos73 shell_scripts]# bash argsnum1.sh /etc/passwd The blankLine is 0 [root@centos73 shell_scripts]# bash argsnum1.sh /etc/services The blankLine is 17 [root@centos73 shell_scripts]# bash argsnum1.sh xxxxxx No such file or directory!?
?
?
?
?
3、編寫腳本/root/bin/checkdisk.sh,檢查磁盤分區(qū)空間和inode使用率,如果超過10%,就發(fā)廣播警告空間將滿
?完整腳本
[root@centos73 shell_scripts]# cat checkdisk.sh #!/bin/bash #Author=wang Check_D=`df |grep "/sd" |tr -s " " "%" |cut -d% -f5 |sort -n |tail -1` [ $Check_D -gt 10 ] && echo space of the disk will be full inode=`df -i |grep "/sd" |tr -s " " "%" |cut -d% -f5 |sort -n |tail -1` [ $inode -ge 1 ] && echo space of inode will be full?
?
?
?
查看磁盤分區(qū)空間
[root@centos73 shell_scripts]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 52403200 1509828 50893372 3% / devtmpfs 487964 0 487964 0% /dev tmpfs 498988 0 498988 0% /dev/shm tmpfs 498988 7776 491212 2% /run tmpfs 498988 0 498988 0% /sys/fs/cgroup /dev/sr0 4364408 4364408 0 100% /mnt /dev/sda3 20961280 87448 20873832 1% /app /dev/sda1 1038336 126596 911740 13% /boot tmpfs 99800 0 99800 0% /run/user/0?
?
?
?查看inode的使用率
[root@centos73 shell_scripts]# df -i Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda2 26214400 39365 26175035 1% / devtmpfs 121991 397 121594 1% /dev tmpfs 124747 1 124746 1% /dev/shm tmpfs 124747 716 124031 1% /run tmpfs 124747 16 124731 1% /sys/fs/cgroup /dev/sr0 0 0 0 - /mnt /dev/sda3 10485760 181 10485579 1% /app /dev/sda1 524288 326 523962 1% /boot tmpfs 124747 1 124746 1% /run/user/0?
?
?
?執(zhí)行結果
[root@centos73 shell_scripts]# bash checkdisk.sh space of the disk will be full space of inode will be full?
?
?
?
腳本解析
注意/dev/sd開頭的才是磁盤分區(qū)
首先過濾出磁盤分區(qū)
[root@centos7 bin]# df |grep "/sd" /dev/sda3 10475520 6837332 3638188 66% / /dev/sda1 201380 105340 96040 53% /boot?
?
?
?
分割符空格替換為%
把所有的空白空格壓縮成一個空格,并且替換成百分號。
分割符一定要加雙引號
[root@centos7 bin]# df |grep "/sd" |tr -s " " "%" /dev/sda3%10475520%6837332%3638188%66%/ /dev/sda1%201380%105340%96040%53%/boot?
?
?
?
[root@centos7 bin]# df |grep "/sd" |tr -s " " "%" |cut -d% -f5 66 53 [root@centos7 bin]# df |grep "/sd" |tr -s " " "%" |cut -d% -f5 | sort -n 53 66 [root@centos7 bin]# df |grep "/sd" |tr -s " " "%" |cut -d% -f5 | sort -n | tail -1 66 cut -d% -f5 百分號作為分隔符,取第5列 sort -n :使用『純數(shù)字』進行排序 tail -1 最后1行?
?
?
?
?
(三)測試文件類型
?
?1是否為普通文件
[root@centos73 ~]# [ -f /etc/issue ] && echo true || echo false true [root@centos73 ~]# [ -f /etc/ ] && echo true || echo false false [root@centos73 ~]# [ -f /etc ] && echo true || echo false false?
?
?
?
?
?2是否為目錄
[root@centos73 ~]# [ -d /etc ] && echo true || echo false true [root@centos73 ~]# [ -d /etc/ ] && echo true || echo false true [root@centos73 ~]# [ -d /etc/issue ] && echo true || echo false false [root@centos73 ~]# [ -d /etc/passwd ] && echo true || echo false false?
?
?
?
3是否為鏈接文件
-h FILE True if file is a symbolic link.
-L FILE True if file is a symbolic link.
注意有些文件是軟連接文件,也是普通文件,因為他指向軟連接的文件類型是普通文件。
?
?
?
?
4是否為套接字文件。
注意套接字文件是為了網(wǎng)絡通信用的。
啟動數(shù)據(jù)庫
[root@centos73 ~]# rpm -q mariadb mariadb-5.5.60-1.el7_5.x86_64 [root@centos73 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@centos73 ~]# systemctl start mariadb [root@centos73 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*?
?
?
?
?
[root@centos73 ~]# ls /var/lib/mysql/ aria_log.00000001 centos73.huawei.com.err ibdata1 ib_logfile1 mysql.sock test aria_log_control centos73.huawei.com.pid ib_logfile0 mysql performance_schema [root@centos73 ~]# ls /var/lib/mysql/ -l total 37860 -rw-rw----. 1 mysql mysql 16384 Apr 27 12:11 aria_log.00000001 -rw-rw----. 1 mysql mysql 52 Apr 27 12:11 aria_log_control -rw-rw----. 1 mysql mysql 1886 Apr 27 12:11 centos73.huawei.com.err -rw-rw----. 1 mysql mysql 5 Apr 27 12:11 centos73.huawei.com.pid -rw-rw----. 1 mysql mysql 18874368 Apr 27 12:11 ibdata1 -rw-rw----. 1 mysql mysql 5242880 Apr 27 12:11 ib_logfile0 -rw-rw----. 1 mysql mysql 5242880 Apr 27 12:11 ib_logfile1 drwx------. 2 mysql mysql 4096 Apr 27 12:11 mysql srwxrwxrwx. 1 mysql mysql 0 Apr 27 12:11 mysql.sock drwx------. 2 mysql mysql 4096 Apr 27 12:11 performance_schema drwx------. 2 mysql mysql 6 Apr 27 12:11 test?
?
?
?
?
只有啟動數(shù)據(jù)庫服務才會生成此文件
[root@centos73 ~]# [ -S /var/lib/mysql/mysql.sock ] && echo true || echo false true [root@centos73 ~]# ll /var/lib/mysql/mysql.sock srwxrwxrwx. 1 mysql mysql 0 Apr 27 12:11 /var/lib/mysql/mysql.sock?
?
?
?
停止數(shù)據(jù)庫服務
[root@centos73 ~]# systemctl stop mariadb [root@centos73 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@centos73 ~]# ls /var/lib/mysql/mysql.sock ls: cannot access /var/lib/mysql/mysql.sock: No such file or directory [root@centos73 ~]# ls /var/lib/mysql/mysql.sock -l ls: cannot access /var/lib/mysql/mysql.sock: No such file or directory [root@centos73 ~]# [ -S /var/lib/mysql/mysql.sock ] && echo true || echo false false?
?
?
?
?
?
?
(四)測試文件屬性
?
1文件是否可讀
[root@centos73 ~]# ll /etc/fstab -rw-r--r--. 1 root root 636 Feb 1 00:26 /etc/fstab [root@centos73 ~]# [ -r /etc/fstab] && echo true || echo false -bash: [: missing `]' false [root@centos73 ~]# [ -r /etc/fstab ] && echo true || echo false true?
?
?
?
?
2文件是否可寫
因為是root用戶登錄的
[root@centos73 ~]# ll /etc/fstab -rw-r--r--. 1 root root 636 Feb 1 00:26 /etc/fstab [root@centos73 ~]# [ -w /etc/fstab ] && echo true || echo false true?
?
?
?
?
?
3文件是否可執(zhí)行
[root@centos73 ~]# ll /etc/fstab -rw-r--r--. 1 root root 636 Feb 1 00:26 /etc/fstab [root@centos73 ~]# [ -x /etc/fstab ] && echo true || echo false false?
?
?
?
4文件是否有sgid權限
[root@centos73 ~]# touch a.txt [root@centos73 ~]# ll a.txt -rw-r--r--. 1 root root 0 Apr 27 11:56 a.txt [root@centos73 ~]# chmod g+s a.txt [root@centos73 ~]# ll a.txt -rw-r-Sr--. 1 root root 0 Apr 27 11:56 a.txt [root@centos73 ~]# [ -g a.txt ] && echo true || echo false true [root@centos73 ~]# touch b.txt [root@centos73 ~]# ll b.txt -rw-r--r--. 1 root root 0 Apr 27 11:56 b.txt [root@centos73 ~]# [ -g b.txt ] && echo true || echo false false?
?
5-k FILE:是否存在且擁有sticky權限
[root@centos73 ~]# ll -d /tmp/ drwxrwxrwt. 8 root root 112 Apr 27 12:16 /tmp/ [root@centos73 ~]# [ -k /tmp/ ] && echo true || echo false true?
?
?
6-u FILE:是否存在且擁有suid權限
[root@centos73 ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd [root@centos73 ~]# [ -u /usr/bin/passwd ] && echo true || echo false true [root@centos73 ~]# [ -u /etc/passwd ] && echo true || echo false false?
?
?
?
注意是以實際權限為標準,而不是表面的權限
在root用戶下面,顯示無權限不一定真的無權限。
root就像是領導,有權限查看、寫入的,但沒有權限執(zhí)行。
[root@centos73 ~]# ll /etc/shadow ----------. 1 root root 3418 Apr 26 23:08 /etc/shadow [root@centos73 ~]# [ -u /etc/shadow ] && echo true || echo false false [root@centos73 ~]# [ -r /etc/shadow ] && echo true || echo false true [root@centos73 ~]# [ -w /etc/shadow ] && echo true || echo false true [root@centos73 ~]# [ -x /etc/shadow ] && echo true || echo false false?
?
?
?使用普通用戶,文件顯示什么權限就是什么權限
[root@centos73 ~]# id zhao uid=1024(zhao) gid=1024(zhao) groups=1024(zhao) [root@centos73 ~]# getent passwd zhao zhao:x:1024:1024::/home/zhao:/bin/bash [root@centos73 ~]# su - zhao Last login: Sat Apr 27 15:45:44 CST 2019 on pts/0[zhao@centos73 ~]$ ll /etc/shadow ----------. 1 root root 3418 Apr 27 15:46 /etc/shadow [zhao@centos73 ~]$ [ -r /etc/shadow ] && echo true || echo false false [zhao@centos73 ~]$ [ -w /etc/shadow ] && echo true || echo false false [zhao@centos73 ~]$ [ -x /etc/shadow ] && echo true || echo false false [zhao@centos73 ~]$ exit logout
?
?
?
?
目前普通用戶是沒有權限查看的
[zhao@centos73 ~]$ cat /etc/shadow cat: /etc/shadow: Permission denied [zhao@centos73 ~]$ exit logout [root@centos73 ~]# cat /etc/shadow root:$6$L4X4itWo9U1UhZ7D$1gFlp6MFqlmLtvCAtCt9XSXBvwFemj/Ke7AV01XEexKucltKKzgMxbr7yPiVEUuiyVcpnDE0s5JZ096lSLnv70::0:99999:7::: bin:*:17632:0:99999:7::: daemon:*:17632:0:99999:7::: adm:*:17632:0:99999:7::: lp:*:17632:0:99999:7::: sync:*:17632:0:99999:7::: shutdown:*:17632:0:99999:7::: halt:*:17632:0:99999:7::: mail:*:17632:0:99999:7::: operator:*:17632:0:99999:7::: games:*:17632:0:99999:7::: ftp:*:17632:0:99999:7::: nobody:*:17632:0:99999:7::: systemd-network:!!:17905:::::: dbus:!!:17905:::::: polkitd:!!:17905:::::: sshd:!!:17905:::::: postfix:!!:17905:::::: dhcpd:!!:17905:::::: apache:!!:17927:::::: user1:$6$HLs6r0rh$XBgmqD/dFgU9W9J769cGPrSPX1xZt4lNKTjxXBJxiC.pY4BkR60DIOVo7vNCavLiutVQB5RaZwbl3fALys5yn0:18012:0:99999:7::: user2:$6$jzrP/9Ye$f4AaH6gQebHuiUHvdTPuuJ5D7OraGtdNt0nbpDp2rDSpHHMPJOn0iMeU2nHrw/pMLTYxlKH9gREr2Ww9ckOvE.:18012:0:99999:7::: user3:$6$.kPyYY7u$4I1Z9L.pK7JwUyceGeUsc3S/iechK8/grS3tk7VbCslvoYitG33/.3yf00BG0kKmtelOYg9cmhIZZn506c2cd0:18012:0:99999:7::: user4:$6$3GsOV1NG$7sJRXhmcGV2fMginz1mIawW8/g1LU0Lv7riLRuaM77jZIhKxXirwZCQI9RZrHUxGGm6hz.M6l1ZcDqBlYScAA/:18012:0:99999:7::: user5:$6$0Qed820A$cQPxR/0Eel0Sk1Kuq/DCatdGOfQkfgGnoQVxEdjgJElra8dAi/UqDhf9QG0SgX0bZESjacigwb/LOPDBdmXCD.:18012:0:99999:7::: user6:$6$7K52M3R4$sDGhJHCM.qs0ASWv4F9zdOIRcH3ju1c6aJKIKG8aAm99l/Zn8PlFURurKTJxCGUy3C.tQmMOjbAe121sYQ5CV.:18012:0:99999:7::: user7:$6$kbgzn9F7$NgyXkzu/mU2f7SZuf/N17o30lBE0OAdyQbvCtPYlVXdjP.iwHtWzRXqFMTzXTl0VX5UMC3RLmJoo3KW.E9JnC1:18012:0:99999:7::: user8:$6$5oEyWVAd$14tH.xSv/njtRbQQRzlef5H6hrmUCYT9YQYgC3jntAlBkavYhmSDxwJx4WJoWFyIOGU5uwwax7RUplCXHbBbo1:18012:0:99999:7::: user9:$6$7smw/DCA$Y4cHOXFx4k5tT1yNC9ldwaPZhZhO4TOTPzGN/X6q3.ZeoBI1eszMpGrEFi9X8x7uqIbfCTfTSe/TvuTmT8ftn1:18012:0:99999:7::: user10:$6$EQOEW5ir$INCc4FovR7DD7ozn/iNCA/GE9aYW8J1BRfsUFOk0ta5/LTJB0nOp5BA.3ZE0ICqjLLl63CjurDAyON1SAyP.30:18012:0:99999:7::: user11:$6$FnwlyVq2$Zw4o3CRM8HBopYjwS6bPuv1qh4711Qf1FZMK9n6h19.cOWFEfqQ9ooBciLIpffm0W40RSg/B9aB0Od0do3div/:18012:0:99999:7::: user12:$6$vdNcdCrz$1F7REyBiXVMJX2u7XeIAmEignw0GvSYRGoVsLhJ9ufEz93.oUmBiQigZr2jRq8ngBG3mNMwTl3v3p.U5VTD5p0:18012:0:99999:7::: user13:$6$9T7DmvNV$3ya3PKhXuvOvtVurLiT13Kv8unGwUFljVzuR5oNNGvpJOPS2VH.xmD.lhAb3J/QVZQy6u8yOdOpIEyYSnHetP1:18012:0:99999:7::: user14:$6$qhnOz7W.$Wbiqvj9Wkw7YNIwQ2xpsNASbAZ5Ai90d1rx3WcdTRi8tvuiGaulttxlgG96KSyT8yBpXw/pyZci7uA92y0WnP0:17935:0:99999:7::: user15:$6$QDVVXOnP$zM04k/zPCXK6tE72R80h0keNdPUoFPuL0yNLbsBfXtjWeftRbqhnAZRgYv6vnVk7uzyXWWb.EO/2DiLHrSdQO/:18012:0:99999:7::: user16:$6$jSai7i1D$3TLTNUDntkwBxSUaE/4UAcONJYSSlrB/RjXsZCPXjYrTakDiuvfw0O8JXwwm/ypRrwQYdVk2dTLhkh2VE8zD40:18012:0:99999:7::: user17:$6$FyW18HlF$VO1Ejg7nwQ8grc8jjEEtNmxDxGoNOKPya8ITWDZTLFPfyuBZ/V8eeneGPgIHCSJLsEh60Bx52xS1cQQzQ15YV.:18012:0:99999:7::: user18:$6$.z2/Dohm$8HmdCleOB6zUTXgFVtB8BDoaaJ0TXO0yfkXLa/CJHYT.P9DzFXwKosunrp3h69dg0fvqOr7.jDrzbpY3KzWql/:18012:0:99999:7::: user19:$6$A9a0tJNT$gMbp7ZqjdTqgOZ90Fe/qSw11cK0k993S2I15xYpzwBIHav/XLMJ7Ka7pakwkv3RmNW.D/6dWhi8w0.CnPxQl2.:18012:0:99999:7::: user20:$6$52.ELIOk$FobPACG6D2IUKDup9aXpGxEUvEG/PxdHt1XvWkJs/tNpgHWKVkNUQHqpfGN.BxyDbQYnUbp33dgKf.bL5Wk3h/:18012:0:99999:7::: tss:!!:17936:::::: cracker:$6$H775bLE6$tM5fjJtbAymFJT/adFBKV6PsVnPqrMfwtHBcBd.wbB7QPMbtbGkXVX6VpMKQEs6majhDDvgK/JLRMDUe.B5Pm/:17939:0:99999:7::: mysql:!!:17939:::::: ntp:!!:17949:::::: zhang:!!:18012:0:99999:7::: zhao:!!:18012:0:99999:7::: xixixi:!!:18012:0:99999:7::: op:!!:18012:0:99999:7::: wang:!!:18013:0:轉(zhuǎn)載于:https://www.cnblogs.com/wang618/p/11041162.html
總結
以上是生活随笔為你收集整理的shell脚本编程测试类型下的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shiro安全框架-简介
- 下一篇: 网络编程之网络架构及其演变过程、互联网与