日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shell脚本逻辑判断,文件目录属性判断,if,case用法

發布時間:2025/5/22 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shell脚本逻辑判断,文件目录属性判断,if,case用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

shell腳本中的邏輯判斷

1.if then fi

[root@weixing01 shell]# cat if1.sh #!/bin/bash a=5 if [ $a -gt 3 ] thenecho ok fi

2.if then else fi:

root@weixing01 shell]# sh -x if2.sh + a=1 + '[' 1 -gt 3 ']' + echo nook nook [root@weixing01 shell]# cat if2.sh #!/bin/bash a=1 if [ $a -gt 3 ] thenecho ok elseecho nook fi

3.if then elif then else fi:

[root@weixing01 shell]# cat if3.sh #!/bin/bash a=4 if [ $a -gt 4 ] thenecho ">1" elif [ $a -lt 4 ] thenecho "<4" elseecho "=4" fi [root@weixing01 shell]# sh -x if3.sh + a=3 + '[' 3 -gt 4 ']' + '[' 3 -lt 4 ']' + echo '<4' <4 [root@weixing01 shell]# vi if3.sh [root@weixing01 shell]# sh -x if3.sh + a=4 + '[' 4 -gt 4 ']' + '[' 4 -lt 4 ']' + echo =4 =4 [root@weixing01 shell]# cat if3.sh #!/bin/bash a=4

4.注意【】兩側都需要有空格,-gt 大于 -lt 小于 -eq等于 兩側都需要空格 -ge大于等于 -le小于等于 noeq 不等于

5.if邏輯判斷支持||和&&

文件目錄屬性判斷

1.-f file 判斷是否是普通文件,且存在

[root@weixing01 shell]# sh -x file1.sh + f=/tmp/aminglinux + '[' -f /tmp/aminglinux ']' + touch /tmp/aminglinux [root@weixing01 shell]# cat file1.sh #!/bin/bash f="/tmp/aminglinux" if [ -f $f ] then echo $f exist elsetouch $f fi [root@weixing01 shell]# sh -x file1.sh + f=/tmp/aminglinux + '[' -f /tmp/aminglinux ']' + echo /tmp/aminglinux exist /tmp/aminglinux exist

2.-d file 判斷是否是目錄且存在:

[root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux + '[' -d /tmp/aminglinux ']' + touch /tmp/aminglinux [root@weixing01 shell]# cat file2.sh #!/bin/bash f="/tmp/aminglinux" if [ -d $f ] then echo $d exist elsetouch $f fi

3.-e判斷文件或者目錄是否存在:

[root@weixing01 shell]# vi file2.sh [root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux + '[' -e /tmp/aminglinux ']' + echo exist exist

4.-r判斷文件是否可讀

[root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux + '[' -r /tmp/aminglinux ']' + echo /tmp/aminglinux readable /tmp/aminglinux readable [root@weixing01 shell]# cat file2.sh #!/bin/bash f="/tmp/aminglinux" if [ -r $f ] then echo $f readable fi

5.-w判斷文件是否可寫

[root@weixing01 shell]# cat file2.sh #!/bin/bash f="/tmp/aminglinux" if [ -w $f ] then echo $f writeable fi [root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux + '[' -w /tmp/aminglinux ']' + echo /tmp/aminglinux writeable /tmp/aminglinux writeable

6.-x判斷是否可執行:不可執行,沒有輸出

[root@weixing01 shell]# ls -l /tmp/aminglinux -rw-r--r-- 1 root root 0 4月 18 21:36 /tmp/aminglinux [root@weixing01 shell]# vi file2.sh [root@weixing01 shell]# cat file2.sh #!/bin/bash f="/tmp/aminglinux" if [ -x $f ] then echo $f exeable fi [root@weixing01 shell]# sh -x file2.sh + f=/tmp/aminglinux + '[' -x /tmp/aminglinux ']'

if特殊用法

1.判斷變量是否為空:

++ wc -l /tmp/lalal wc: /tmp/lalal: 沒有那個文件或目錄 + n= + '[' -gt 100 ']' if4.sh: 第 3 行:[: -gt: 期待一元表達式 [root@weixing01 shell]# vi if4.sh [root@weixing01 shell]# sh -x if4.sh ++ wc -l /tmp/lalal wc: /tmp/lalal: 沒有那個文件或目錄 + n= + '[' -z '' ']' + echo error error [root@weixing01 shell]# cat if4.sh #!/bin/bash n=`wc -l /tmp/lalal` if [ -z "$n" ] thenecho error elif [ $n -gt 100 ] then echo aldkjglka fi

2.-n判斷是否不為空:

[root@weixing01 shell]# ls 01.sh file1.sh file2.sh if1.sh if2.sh if3.sh if4.sh [root@weixing01 shell]# if [ -n 01.sh ]; then echo ok; fi ok

3.-q 文件中含有字符時會怎樣:

[root@weixing01 shell]# if grep -wq 'weixing01' /etc/passwd; then echo "sdjfk"; fi sdjfk

case判斷

1.編寫腳本:

[root@weixing01 shell]# cat case1.sh #!/bin/bash read -p "Please input a number: " n if [ -z "$n" ] thenecho "Please input a number."exit 1 fin1=`echo $n|sed 's/[0-9]//g'` if [ -n "$n1" ] thenecho "Please input a number."exit 1 fiif [ $n -lt 60 ] && [ $n -ge 0 ] thentag=1 elif [ $n -ge 60 ] && [ $n -lt 80 ] thentag=2 elif [ $n -ge 80 ] && [ $n -lt 90 ] thentag=3 elif [ $n -ge 90 ] && [ $n -le 100 ] thentag=4 else tag=0 fi case $tag in1)echo "not ok";;2)echo "ok";;3)echo "ook";;4)echo "oook";;*)echo "The number range is 0-100.";; esac [root@weixing01 shell]# sh -x case1.sh + read -p 'Please input a number: ' n Please input a number: 101 + '[' -z 101 ']' ++ echo 101 ++ sed 's/[0-9]//g' + n1= + '[' -n '' ']' + '[' 101 -lt 60 ']' + '[' 101 -ge 60 ']' + '[' 101 -lt 80 ']' + '[' 101 -ge 80 ']' + '[' 101 -lt 90 ']' + '[' 101 -ge 90 ']' + '[' 101 -le 100 ']' + tag=0 + case $tag in + echo 'The number range is 0-100.' The number range is 0-100. [root@weixing01 shell]# sh -x case1.sh + read -p 'Please input a number: ' n Please input a number: 78 + '[' -z 78 ']' ++ echo 78 ++ sed 's/[0-9]//g' + n1= + '[' -n '' ']' + '[' 78 -lt 60 ']' + '[' 78 -ge 60 ']' + '[' 78 -lt 80 ']' + tag=2 + case $tag in + echo ok ok

轉載于:https://blog.51cto.com/13517254/2105139

總結

以上是生活随笔為你收集整理的shell脚本逻辑判断,文件目录属性判断,if,case用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。