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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Shell基础之条件判断 分支判断

發布時間:2025/3/8 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Shell基础之条件判断 分支判断 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

條件判斷式語句

文件類型判斷

測試選項作用
-d 文件是否是目錄
-e 文件是否存在
-f 文件是否是文件

整數之間的比較

測試選項作用
整數1 -eq 整數2相等
整數1 -ne 整數2不相等
整數1 -gt 整數2大于
整數1 -lt 整數2小于
整數1 -ge 整數2大于等于
整數1 -le 整數2小于等于

文件權限

測試選項作用
-r 文件是否可讀
-w 文件是否可寫
-x 文件是否可執行

字符串判斷

測試選項作用
-z 字符串是否為空,為空返回真
-n 字符串是否為空,非空返回真
字符串1 == 字符串2==
字符串1 != 字符串2!=

多重條件判斷

測試選項作用
判斷1 -a 判斷2
判斷1 -o 判斷2
!判斷
# 兩種判斷格式 # test -e /root/install.log # [-e /root/install.log] //推薦[-d /root] && echo "yes" || echo "no" 復制代碼

單分支if語句

#!/bin/bash # 判斷登陸的用戶是否是root test=$(env | grep "USER" | cut -d "=" -f 2)if [ "$test" == root ]thenecho "Current user is root" fi 復制代碼#!/bin/bash # 判斷分區使用率 rate=$(df -h | grep "/dev/vda1" | awk '{print $5}' | cut -d "%" -f 1)if [ $rate -ge 1 ]thenecho "Warming /dev/vda1 is full !!" fi復制代碼

雙分支if語句

#!/bin/bash read -t 30 -p "Please input a dir: " dirif [ -d "$dir" ]thenecho "yes"elseecho "no" fi 復制代碼#!/bin/bash #檢測apache的運行狀態(腳本名字堅決不能包含httpd,否則腳本會失效) test=$(ps aux | grep httpd | grep -v grep)if [ -n "$test" ]thenecho "$(date) httpd is ok!" >> /tmp/autostart-acc.logelse/etc/rc.d/init.d/httpd start &> /dev/nullecho "$(date) httpd restart httpd!!" >> /tmp/autostart-acc.log fi 復制代碼

多分支if語句

#!/bin/bash read -p "Please input a filename : " fileif [ -z "$file" ]thenecho "Error, please input a filename "exit 1 elif [ ! -e "$file" ]thenecho "Your input is not a file! "exit 2 elif [ -f "$file" ]thenecho "$file is a regulare file!" elif [ -d "$file" ]then echo "$file is a directory!" elseecho "$file is an other file!" fi 復制代碼

多分支case語句

#!/bin/bash read -t 30 -p "Please input yes/no : " chocase "$cho" in"yes")echo "yes";;"no")echo "no";;"*")echo "Please input right content";; esac 復制代碼

for循環

#!/bin/bash # 批量解壓文件 cd /root/test/ ls *.tar.gz > ls.log ls *.tgz >> ls.logfor i in $( cat ls.log )dotar -zxf $i & > /dev/nulldonerm -rf ls.log 復制代碼#!/bin/bash s=0 for (( i=1;i<=100;i=i+1 ))dos=$(( $s+$i ))done echo $s 復制代碼#!/bin/bash # 批量添加指定數量的用戶read -p "Please input user name : " -t 30 name read -p "Please input number of users : " -t 30 num read -p "Please input user pass : " -t 30 passif [ ! -z "$name" -a ! -z "$num" -a ! -z "$pass" ]theny=$(echo $num | sed 's/[0-9]//g')if [ -z "$y" ]thenfor (( i=1;i<=$num;i=i+1 ))do/usr/sbin/useradd $name$i &> /dev/nullecho $pass | /usr/bin/passwd --stdin $name$i &> /dev/nulldonefi fi 復制代碼#!/bin/bash #批量刪除for i in $( cat /etc/passwd | grep /bin/bash | grep -v root | cut -d ":" -f 1 )douserdel -r $idone 復制代碼

while循環和until循環

#!/bin/bash i=1 s=0while [ $i -le 100 ]dos=$(( $s+$i ))i=$(( $i+1))doneecho "This sum is : $s" 復制代碼#!/bin/bash i=1 s=0util [ $i -ge 100 ]dos=$(( $s+$i ))i=$(( $i+1))doneecho "This sum is : $s" 復制代碼

總結

以上是生活随笔為你收集整理的Shell基础之条件判断 分支判断的全部內容,希望文章能夠幫你解決所遇到的問題。

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