網站首頁 編程語言 正文
1.單分支if條件語句
then后面跟符合條件之后執行的程序,可以放在[]之后,用;分隔。也可以換行寫入,
就不需要“;”了。
比如:
if ? [ ?條件判斷式 ?]?
? ? then
? ? ? ? 程序?
fi?
1.1舉例:判斷目錄是否存在,不存在則創建
#!/bin/bash
#date:2022-4-13
#描述:if單分支語句示例,判斷目錄是否存在
read -p "請輸入需要判斷的目錄:" name
if [ ! -d $name ]
then
echo "$name目錄不存在,正在創建..."
mkdir -p $name
echo "$name目錄創建完成."
fi
echo "$name目錄已存在,正在退出..."
2.雙分支if條件語句
if ? [ ?條件判斷式 ?]?
? ? then
? ? ? ? 條件成立時,執行的程序。?
? ? else
? ? ? ? 條件不成立時,執行的另一個程序。?
fi
2.1舉例:監聽并自動重啟apache服務腳本
在日常工作中,服務器上的服務經常會宕機。如果我們對服務器監控不好,就會造成服務器中服務宕機了,而管理員卻不 知道的情況。這是我們可以寫一個腳本來監聽本機的服務。如果服務停止或宕機了,可以自動重啟這些服務。用apache舉例:
首先介紹端口掃描命令
nmap端口掃描命令,
格式:nmap -sT 域名或IP
子選項:
? ? ? ? -s ? ? ? ? 掃描
? ? ? ? -T ? ? ? ?掃描所有開啟的TCP端口?
nmap掃描后顯示的端口一定是存活的。
腳本要使用nmap命令,首先用yum -y install nmap安裝。
apache服務也是yum安裝。
[root@xiaopeng ~]# cat autostart.sh
#!/bin/bash
port=$(nmap -sT 192.168.22.222 | grep tcp | grep http | awk '{print $2}')
if [ "$port" == "open" ]
then
echo "$(date) httpd is ok!" >> /tmp/autostart-acc.log
else
/etc/rc.d/init.d/httpd start &> /dev/nullecho "$(date) restart httpd!!" >> /tmp/autostart-err.log
fi
(首先用nmap命令查看是否開啟apache并賦值給port。
然后進行條件判斷。如果服務開啟,輸出當前時間+httpd is ok 到/tmp/autostart-
acc.log。
如果變量port的值不是open,那么執行else下操作。首先啟動apache服務,將啟動后 信息輸出至位桶,然后在/tmp/autostart-err.log中記錄。在本次腳本中nmap命令使用的是
IP查找端口,但并未指DNS,所以會報DNS不存在的錯,但不影響結果。)
3.多分支if條件語句
if ? [ ?條件判斷式1 ?]?
? ? then
? ? ? ? 當條件判斷式1成立時,執行程序1。?
elif ?[ ?條件判斷式2 ?]?
? ? then
? ? ? ? 當條件判斷式2成立時,執行程序2。?
? ? ? ? ......(可加入更多條件)?
? ? else
? ? ? ? 當所有條件不成立時,最后執行此程序。?
fi
3.1舉例:判斷用戶輸入的是文件還是目錄
#!/bin/bash
#date:2022-4-13
#描述:判斷文件類型
read -p "請輸入一個文件:" file
if [ -z $file ]
then
echo "錯誤!輸入的文件為空."
elif [ ! -e $file ]
then
echo "錯誤!輸入的文件不存在."
elif [ -f $file ]
then
echo "$file是一個普通文件"
elif [ -d $file ]
then
echo "$file是一個目錄"
else
echo "$file是其他類型文件"
fi
4.case條件語句
多分支case條件語句
case ?$變量名 ?in?
? ? “值1”)?
? ? ? ? 如果$變量等于值1,則執行程序1?
? ? ;;?
? ? “值2”)?
? ? ? ? 如果$變量等于值2,則執行程序2?
? ? ;;?
? ? ? ? ....省略...?
? ? *)?
? ? ? ? 如果$變量的值不是以上值,則執行此程序?
? ? ;;?
esac?
4.1舉例:創建啟動腳本,讓service命令管理apache
[root@xiaopeng htdocs]# vim /etc/init.d/apached
#!/bin/bash
# chkconfig: 2345 64 36
# description: A very fast and reliable SQL database engine
httpd=/usr/local/apache2/bin/apachectl
case $1 in
start)
$httpd start
;;
stop)
$httpd stop
;;
restart)
$0 stop
sleep 0.05
$0 start
;;
configtest)
$httpd -t
;;
*)
echo "usage:$0 start|stop|restart|configtest."
;;
esac
4.2舉例:創建啟動腳本,讓service命令管理nginx
[root@xiaopeng conf]# vim /etc/init.d/nginx
#!/bin/bash
#Author:liu
#chkconfig: 2345 99 33
#description: nginx server control tools
ngxc="/usr/local/nginx/sbin/nginx"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
case "$1" in
start)
$ngxc -t &> /dev/null
if [ $? -eq 0 ];then
$ngxc
$ngxc_fpm
echo "nginx service start success!"
else
$ngxc -t
fi
;;
stop)
$ngxc -s stop
killall php-fpm
echo "nginx service stop success!"
;;
restart)
$0 stop
$0 start
;;
reload)
$ngxc -t &> /dev/null
if [ $? -eq 0 ];then
$ngxc -s reload
pkill -HUP php-fpm
echo "reload nginx config success!"
else
$ngxc -t
fi
;;
*)
echo "please input stop|start|restart|reload."
exit 1
esac
總結
原文鏈接:https://blog.csdn.net/weixin_46659843/article/details/124139867
相關推薦
- 2022-10-02 Pandas中的unique()和nunique()區別詳解_python
- 2022-12-19 nginx?rewrite參數解析_nginx
- 2022-10-24 C#?Winform實現自定義漂亮的通知效果_C#教程
- 2022-01-16 jQuery實現動畫效果和導航欄動態顯示
- 2022-04-16 Python3如何將源目錄中的圖片用MD5命名并可以設定目標目錄_python
- 2022-11-10 Android拍攝照片后返回縮略圖的方法_Android
- 2022-04-28 C#委托用法詳解_C#教程
- 2023-02-07 Redis?中ZSET數據類型命令使用及對應場景總結(案例詳解)_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支