利用case语法安装httpd服务
[root@localhost shell]# cat httpd_start1.sh #!/bin/bash # This script manages the Apache HTTP server. # by author rivers on 2024-10-12 # 检查是否提供了参数 if [ -z "$1" ]; then echo "Usage: $0 {start|stop|restart|status}" exit 1 fi # 定义变量 HTTPD_SERVICE="httpd" HTTPD_CONFIG="/etc/httpd/conf/httpd.conf" # 检查 httpd 是否已安装 if ! command -v $HTTPD_SERVICE &> /dev/null; then echo "Error: $HTTPD_SERVICE is not installed." exit 1 fi # 检查配置文件是否存在 if [ ! -f "$HTTPD_CONFIG" ]; then echo "Error: Configuration file $HTTPD_CONFIG does not exist." exit 1 fi # 使用 case 语句处理不同的命令 case "$1" in start) echo "Starting $HTTPD_SERVICE..." if sudo systemctl start $HTTPD_SERVICE; then echo "$HTTPD_SERVICE started successfully." else echo "Failed to start $HTTPD_SERVICE." exit 1 fi ;; stop) echo "Stopping $HTTPD_SERVICE..." if sudo systemctl stop $HTTPD_SERVICE; then echo "$HTTPD_SERVICE stopped successfully." else echo "Failed to stop $HTTPD_SERVICE." exit 1 fi ;; restart) echo "Restarting $HTTPD_SERVICE..." if sudo systemctl restart $HTTPD_SERVICE; then echo "$HTTPD_SERVICE restarted successfully." else echo "Failed to restart $HTTPD_SERVICE." exit 1 fi ;; status) echo "Checking $HTTPD_SERVICE status..." sudo systemctl status $HTTPD_SERVICE ;; *) echo "Invalid option: $1" echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac exit 0 |
执行脚本
标签:脚本,HTTPD,shell,http,SERVICE,httpd,echo,exit,fi From: https://blog.csdn.net/2301_82330629/article/details/142876467