Linux operating system process control statement - introduction to the use of case statement
This blog will introduce how to use the flow control statement-case statement in the Linux operating system. We will detail the execution flow and usage of the case statement through multiple cases. These cases include menu options, rsync service scripts, nginx service scripts, implementing system toolboxes, and implementing simple jumpserver springboards. By learning these contents, you will be able to better grasp the process control in shell scripting and realize code execution in different situations.
Case 1: Menu Options
1.1 if statement execution
First, let's look at a case of using an if statement to implement menu options.
echo "Please select an option:"
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
read option
if [ $option -eq 1 ]
then
echo "You selected Option 1."
elif [ $option -eq 2 ]
then
echo "You selected Option 2."
elif [ $option -eq 3 ]
then
echo "You selected Option 3."
else
echo "Invalid option."
fi
1.2 case statement execution
Next, we use the case statement to achieve the same menu option functionality.
echo "Please select an option:"
echo "1. Option 1"
echo "2. Option 2"
echo "3. Option 3"
read option
case $option in
1)
echo "You selected Option 1."
;;
2)
echo "You selected Option 2."
;;
3)
echo "You selected Option 3."
;;
*)
echo "Invalid option."
;;
esac
Case 2: rsync service script
2.1 if statement execution
The following is a case of using an if statement to implement an rsync service script.
#!/bin/bash
command=$1
if [ "$command" = "start" ]
then
echo "Starting rsync service..."
# 启动rsync服务的命令
elif [ "$command" = "stop" ]
then
echo "Stopping rsync service..."
# 停止rsync服务的命令
elif [ "$command" = "restart" ]
then
echo "Restarting rsync service..."
# 重启rsync服务的命令
else
echo "Invalid command. Please specify 'start', 'stop', or 'restart'."
fi
2.2 case statement execution
Now, let us use case statement to achieve the same rsync service script functionality.
#!/bin/bash
command=$1
case $command in
"start")
echo "Starting rsync service..."
# 启动rsync服务的命令
;;
"stop")
echo "Stopping rsync service..."
# 停止rsync服务的命令
;;
"restart")
echo "Restarting rsync service..."
#
重启rsync服务的命令
;;
*)
echo "Invalid command. Please specify 'start', 'stop', or 'restart'."
;;
esac
Case 3: nginx service script
For more complex service scripts, using case statements can make the code clearer and easier to maintain. The following is an example of an nginx service script.
#!/bin/bash
command=$1
case $command in
"start")
echo "Starting nginx service..."
# 启动nginx服务的命令
;;
"stop")
echo "Stopping nginx service..."
# 停止nginx服务的命令
;;
"restart")
echo "Restarting nginx service..."
# 重启nginx服务的命令
;;
"status")
echo "Checking nginx service status..."
# 检查nginx服务状态的命令
;;
*)
echo "Invalid command. Please specify 'start', 'stop', 'restart', or 'status'."
;;
esac
Case 4: Implementing the System Toolbox
The case statement can also be used to create a simple system toolbox script that executes different system commands based on the options entered by the user.
Here is an example:
#!/bin/bash
echo "Please select a tool:"
echo "1. Disk Usage"
echo "2. Network Statistics"
echo "3. Process List"
read tool
case $tool in
1)
df -h
;;
2)
ifconfig
;;
3)
ps aux
;;
*)
echo "Invalid tool."
;;
esac
Case 5: Realize a simple jumpserver springboard machine
The simple jumpserver function can be realized by using the case statement, and the jump login can be performed according to the server selected by the user.
Here is an example:
#!/bin/bash
echo "Please select a server:"
echo "1. Server 1"
echo "2. Server 2"
echo "3. Server 3"
read server
case $server in
1)
ssh [email protected]
;;
2)
ssh [email protected]
;;
3)
ssh [email protected]
;;
*)
echo "Invalid server."
;;
esac
in conclusion
This blog introduces the use of flow control statements-case statements in the Linux operating system, and illustrates the execution of case statements in different scenarios through multiple cases. These cases include menu options, rsync service scripts, nginx service scripts, implementing system toolboxes, and implementing simple jumpserver springboards. By learning and applying these knowledge, you will be able to better grasp the flow control in shell scripting, and execute corresponding code blocks according to different conditions. Hope this blog was helpful to you!