Practical application of if case in common shell scripts

if statement

single branch if statement

if conditional test action

then

command sequence

fi

Prompt the user to enter the content, and use the if statement to determine whether the input content is an integer

Use the if statement to determine whether the host is alive ping -c 5

double branch if statement

if conditional test action

then

command sequence 1

else

command sequence 2

fi

 Prompt the user to enter the content, and use the if statement to determine whether the input content is an integer

According to the previous question, judge whether the input content is odd or even.

multi-branch if statement

if conditional test action 1

then

command sequence 1

elif conditional test action 2

then

command sequence 2

[else]

[command sequence 3]

fi

Judging the relationship between two numbers entered by the user.

case statement

When using the case branch statement, there are several noteworthy features as follows:

A case line must end with the word "in", and each pattern must end with a closing bracket ")".

A double semicolon ";;" indicates the end of a command sequence.

In the pattern string, square brackets can be used to represent a continuous range, such as "[0-9]"; vertical bar symbols "|" can also be used to represent or, such as "A|B".

The last "*)" indicates the default mode, where * is equivalent to a wildcard.

case variable value in

Mode 1)

command sequence 1

;;

Mode 2)

command sequence 2

;;

……

* )

default command sequence

esac

Use the case statement to control firewalld start|stoplrestart status to manage firewalld service requirements. If the command option is incorrect, the prompt input is incorrect: sO startlstoplstatuslrestart]".

Use the case statement to decompress the compressed package named .tar.gz or .tar.bz2 according to the suffix to the /opt directory

  if-case statement

Prompt the user to enter the number of seconds for the 100-meter race. It is required to judge that the number of seconds is greater than 0 and less than or equal to 10 seconds to enter the trial, and those greater than 10 seconds will be eliminated. If you enter other characters, you will be prompted to re-enter: members who enter the trial will further determine the gender of men and women , Boys enter the boys group, girls enter the girls group, if the input is wrong, please prompt the error. ( Note: need to judge the decimal situation )


 

Guess you like

Origin blog.csdn.net/weixin_42054864/article/details/131684473