Use case statements in Shell programming, and use case statements in shell programming

Source: Internet
Author: User

Use case statements in Shell programming, and use case statements in shell programming

Hello everyone, today we bring you the use of Shell programming case statements!

In fact, the case statement is quite understandable. In shell programming, the if statement has its syntax, and the function also has its syntax, the case statement in shell programming also has its syntax as follows:

Case variable name in value 1) command 1; value 2) command 2; Value 3) command 3; esac

In the above Code, we found that starting with case and ending with esac is like our if statement, starting with if and ending with fi.

Example 2:

Case "finding a husband's condition" in the House) marry you ...;; family background) marry you ...;; we can talk about men and women first ..;; esac

Now let's practice it. In fact, the syntax is some of the above basic syntax. So how can I use case in practice?

In fact, I think the case statement is more suitable for some menu options scripts, so we should first use the if statement to write a menu script as follows:

#! /Bin/bash #! /Bin/bash # Date: # Mail: ywyankerp@163.com # Founder:
 
  
# Describe: This is a one-button installation service script function CDAN () {cat <yankerp 1. rice 2. noodle 3. steamed Stuffed Bun yankerp} CDANread-p please enter what you want to eat today: NUMexpr $ NUM + 1 &>/dev/nullif ["$? "-Ne 0] then echo" Please input {1 | 2 | 3} "exit 1 fiif [" $ NUM "-eq 1] then echo", come to a bowl of rice "elif [$ NUM-eq 2] then echo", come to a bowl of noodles "elif [" $ NUM "-eq 3] then echo", "fi"
 

First, we can see whether the above if statement is a little complicated. Then we will use the case statement to implement the following:

#! /Bin/bash #! /Bin/bash # Date: # Mail: ywyankerp@163.com # Founder:
 
  
# Describe: This is a one-button installation service script function CDAN () {cat <yankerp 1. rice 2. noodle 3. steamed Stuffed Bun yankerp} CDANread-p please enter what you want to eat today: NUMexpr $ NUM + 1 &>/dev/nullif ["$? "-Ne 0] then echo" Please enter {1 | 2 | 3} "exit 1 ficase $ NUM in 1) echo", come to a bowl of rice "; 2) echo "xiao'er, come to a bowl of noodles"; 3) echo "xiao'er, come to a pot of steamed buns"; esac
 

Profiling time:

#! /Bin/bash #! /Bin/bash # Date: # Mail: ywyankerp@163.com # Founder:
 
  
# Describe: This is a one-button installation service script function CDAN () {# define a CDAN function cat <yankerp 1. rice 2. noodle 3. steamed Stuffed Bun yankerp} CDAN # Call the CDAN function read-p. Enter what you want to eat today: NUM # enter a prompt, then, assign the user-input string to the variable NUMexpr $ NUM + 1 &>/dev/null # Use the value calculation command expr to determine whether the user-input value is a value if ["$? "-Ne 0] # If the user does not enter the value then #, then echo" Please enter {1 | 2 | 3} "# print out 1 2 3 exit 1 # Finally exit the script fi # end if statement case $ NUM in # Here is our case statement, in the NUM variable, 1) # If the user inputs 1 echo ", "# output a line to a bowl of rice; 2) # If the user inputs 2 echo "," # In the NUM variable, continue to print the content; 3) # echo ", "; esac # End with esac
 

Do you think it is not eye-catching? It does not matter, such:

We found that it is more convenient and appropriate to use case statements in these scripts with multiple options.

Now that we have mastered the basic syntax and some of its functions, we can combine the function + case statement to complete a menu-style installation script as follows:

#! /Bin/bash # Date: # Mail: ywyankerp@163.com # Founder:
 
  
# Describe: This is a one-button installation service script function CDAN () {cat <yankerp + ------------------------------------------------ + | _ o0o _ 1. install Nginx. | 08880 2. install Apache | 88 ". "88 3. install MySQL | (|-_-|) 4. install PHP | 0 \ =/0 5. deploy the LNMP environment | _/\__ 6. install zabbix monitoring | '\ //' 7. exit this hypervisor |/Linux one-click \ 8. disable the computer | Server | |==============================||//// one-click Installation service | I | by Yan Kerp | ___ ==============================|| ___'. /--. --\. '___ | + ------------------------------------------------ + yankerp} CDAN. /etc/init. d/functionsBASE_DIR =/usr/local/srcread-p "Enter 1-8 characters:" NUMexpr $ NUM + 1 &>/dev/nullif ["$? "-Ne 0] then echo" Please enter {1 | 2 | 3 | 4 | 5 | 6 | 7 | 8} "exit 1 using unction Nginx_server () {echo "start to install Nginx. Please wait .... "yum install-y gcc-c ++ pcre-devel zlib-devel openssl-devel &>/dev/null cd $ BASE_DIR & wget http://nginx.org/download/nginx-1.12.2.tar.gz &>/dev/null & & useradd-M-s/sbin/nologin nginx & \ tar zxf nginx-1.12.2.tar.gz & cd $ BASE_DIR/nginx-1.12.2 /&&\. /configure -- prefix =/usr/local/nginx -- wit H-http_dav_module -- with-http_stub_status_module -- with-http_addition_module -- with-http_sub_module -- with-http_flv_module -- with-http_mp4_module -- with-pcre -- with-http_ssl_module -- with-http_gzip_static_module -- user = nginx -- group = nginx &>/dev/nullif ["$? "-Eq 0] thenmake & make install &>/dev/nullfiif [-f/usr/local/nginx/sbin/nginx]; then ln-s/usr/local/nginx/sbin/nginx/usr/local/sbin & nginx & echo "Nginx installation successful! "Fi} function Apache_server () {echo" start to install Apache. Please wait "yum install httpd-y &>/dev/nullif [" $? "-Eq 0] then echo" Apache installation successful! "Else echo" Apache installation failed! "Fi} case $ NUM in1) Nginx_server; 2) Apache_server; esac
 

The running result is as follows:

You can refer to the above script for reference. It is easy to implement. Next, let's take a case.

Practice 2: programming Nginx startup scripts

The compiled and installed Nginx does not have a STARTUP script. At this time, we can choose to write one by ourselves. Here I will use my ideas to explain to you that the entire process is not very standard, hope to help you as follows:

#! /Bin/bashBASE_DIR =/usr/local/nginx/sbin/PID =/usr/local/nginx/logs/nginx. pid # no1. determine whether to log on as a root user. if ["$ UID"-ne 0] then echo ", Please Log On As A root user !!! "Exit 1fi. /etc/init. d/functions # determine the number of input parameters if ["$ #"-ne 1] then echo "/bin/bash $0 {start | stop | restart}" exit 1fi # compile the function start () for starting the Nginx function () {if ['netstat-anput | grep nginx | wc-l'-eq 0] then $ BASE_DIR/nginxfi if ["$? "-Eq 0] then action" Nginx started successfully! "/Bin/true else action" Nginx startup failed! "/Bin/false fi} # compile and stop the Nginx function stop () {if ['netstat-anput | grep nginx | wc-l'-ne 0] then $ BASE_DIR/nginx-s stopfi if ["$? "-Eq 0] then action" Nginx stopped successfully! "/Bin/true else action" Nginx stop failed! "/Bin/false fi} # Write a case statement using the special position variable $1 to determine the user's input content case $1 instart) start; stop) stop; restart) stop start; esac

Run the following command:

A clearer picture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.