Shell编程入门(第二版)(中)
變量測試語句-test
作用:用來測試變量是否相等,是否為空,文件類型等。
格式:
test?測試條件?或?[] #范圍:整數,字符串,文件 ?
?
1)整數測試:?
test?int1?-eq?int2? 測試整數是否相等?
test?int1?-ge?int2? 測試int1是否>=int2?
test?int1?-gt?int2? 測試int1是否>int2?
test?int1?-le? int2?測試int1是否<=int2?
test?int1?-lt?int2? 測試int1是否<int2?
test?int1?-ne?int2? 測試整數是否不相等
?
2)字符串測試:?
test?str1=str2? 測試字符串是否相等?
test?str1!=str2? 測試字符串是否不相等?
test?str1? 測試字符串是否不為空?
test?-n?str1? 測試字符串是否不為空?
test?-z?str1? 測試字符串是否為空
?
3)文件測試:?
test?-d?file? 指定文件是否目錄?
test?-f?file? 指定文件是否常規文件?
test?-x?file? 指定文件是否可執行?
test?-r?file? 指定文件是否可讀?
test?-w?file? 指定文件是否可寫?
test?-a?file?指定文件是否存在?
test?-s?file?文件的大小是否非0
?
注:test測試語句一般不單獨使用,一般作為if語句的測試條件,如;
if test -d file then.... fi?
test的變量的簡寫形式”[]”
?
示例-apachtest.sh
#!/bin/bash # A test shell script for test Apache is running or notweb=$(/usr/bin/pgrep httpd)echo "Now let's test the Apache..." echo#if [ "$web" != "" ] if [ -n "$web" ] thenecho "Apache is running..." elseecho "Apache is NOT running..."/etc/rc.d/init.d/httpd start fi
流程控制語句
流控制語句:用于控制shell程序的流程?
exit語句:退出程序執行,并返回一個返回碼,返回碼為0表示正常退出,非0表示非正常退出。?
例如:exit?0?
?
一、if
if/then格式
if test -d $1 then ... fi示例-if_then.sh
#!/bin/bash # A test shell script for if/thenif [ -x /etc/rc.d/init.d/httpd ] thenecho "Script: /etc/rc.d/init.d/httdp have x power!"/etc/rc.d/init.d/httpd restart fiif/else格式
if 條件1 then 命令1 elif 條件2then 命令2 else 命令3 fi多個條件的聯合:?
-a: 邏輯與,僅當兩個條件都成立時,結果為真。?
-o: 邏輯或,兩個條件只要有一個成立,結果為真。
?
示例-if_else.sh
#!/bin/bash # A test shell script for if/elif/elseecho -n "Please input a filename: " read filenameif [ -d $filename ] thenecho "$filename is a directory" elif [ -f $filename ] thenecho "$filename is a commen file" elif [ -c $filename -o -b $filename ] thenecho "$filename is a device file" elseecho "$filename is a unkown file" fi?
示例-if_elif_exit.sh
#!/bin/bash # A test shell script for if/elifif [ $# -ne 2 ] then echo "Not enough parameters" exit 1 fiif [ $1 -gt $2 ] thenecho "$1 is great then $2" elif [ $1 -lt $2 ] thenecho "$1 is little then $2" elseecho "$1 is equal as $2" fi二、for/in
for 變量 in 名字表 do 命令列表 done示例-for.sh
#!/bin/bash # A test shell script for "for"for DAY in Sunday Monday Tuesday Wednesday Thursday Friday Saturday doecho "The day is $DAY" doneawk命令[分段提取]
awk?-F域分隔符?‘命令’[單引號] #如果不用-F指定分割符,默認為空格
?
1、檢測系統中UID為0的用戶?
awk?-F:?'$3==0?{print?$1}'?/etc/passwd
#awk?-F:?'{print?$1}'?/etc/passwd
-F: 指定分割附為:
$3 表示以:為分割附的第三位
?
2、檢測系統中密碼為空的用戶?
awk?-F:?'length($2)==0?{print?$1}'?/etc/shadow?
#ps?aux?|?grep?-v?root?|?awk?'{print?$2}'
示例-awk.sh
#!/bin/bash # A test script for desplay users infomation/bin/echo -n "Please input a username: " read username/bin/grep $username /etc/passwd > /dev/null 2> /dev/nullif [ $? -eq 0 ] then/bin/echo "username is: $username" else/bin/echo "user: $username is not exits."exit 1 fi /bin/echo# list /etc/passwd info userinfo=`/bin/grep ^$username:x /etc/passwd` uid=`echo $userinfo | awk -F: '{print $3}'` gid=`echo $userinfo | awk -F: '{print $4'}` dir=`echo $userinfo | awk -F: '{print $6}'` shell=`echo $userinfo | awk -F: '{print $7}'`# get /etc/group info groupinfo=`/bin/grep x:$gid /etc/group` gname=`/bin/echo $groupinfo | awk -F: '{print $1}'`/bin/echo "user id is: $uid" /bin/echo "default group is: $gname" /bin/echo "home directory is: $dir" /bin/echo "shell is: $shell" /bin/echo "group member info:"# get group members groups=`/usr/bin/groups $username` /bin/echo $groups /bin/echo# get online info online=`/usr/bin/who | grep $username` if [ -z "$online" ] thenecho "$username is not online" elseecho "$username is online..." fi?
實例-killuser.sh
#思路:將一個用戶所有的進程包括shell都關閉,則相當于將該用戶踢出了系統 #!/bin/bash # A shell sript to kill a user in Linuxusername=$1killpid=`/bin/ps aux | grep $username | awk '{print $2}'`for PID in $killpid do/bin/kill -9 $PID 2> /dev/null done轉載于:https://www.cnblogs.com/itrena/p/5927018.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Shell编程入门(第二版)(中)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MySQL】Java对SQL时间类型的
- 下一篇: UT(XCAP) 参数说明