Shell Branch Statement
Case inch mode 1) Command1 command2 command3 ; Mode 2) Command1 command2 command3 ;; *) Command1 command2 command3 ;; Esac
Description: After the case is taken, the value is the keyword in, followed by the various patterns of the match, each pattern must end with a closing parenthesis.
The value can be a variable or constant.; Like a C-language break, do not cancel!
Example:
#!/bin/SH#auther: linuxdaxue.com#Date: .- to- - Case$1 inchStart|begin)Echo "I am started!" ;; Stop|end)Echo "I am stopped!" ;;*) Echo "Other command!" ;;Esac
Description: This script exercises the pattern matching, pattern matching support ' | ' The command will be executed if a condition is met.
Example 2:
Projectname="Zynq"; #call funprintpwd () {#Echo pwd Echo "Current directory is ' pwd '";} Case " $" inch#启动服务create)if[-D $PROJECTNAME]; Then Echo "folder $projectname already exists, and re-created after deletion"; #删除目录, successful return 0RM-RF $PROJECTNAME; fi#创建目录, successful return 0mkdir$PROJECTNAME; if[ $? !=0]; Then Echo "failed to create folder, please try again!"; Exit-1; fi ;; Delete)if[!-d $PROJECTNAME]; Then Echo "folder $projectname does not exist, please try again"; Exit1; fi#删除目录, successful return 0RM-RF $PROJECTNAME; if[ $? !=0]; Then Echo "Delete folder failed, please try again!"; Exit-1; fi ;;*) #处理其他情况, show usageEcho "The parameter you entered is wrong, please enter the following parameters: {create|delete}"; Exit-1; ;;EsacCD $PROJECTNAME;p rintpwd;Echo " last line"; exit0;
Here, a case statement is used to create a function similar to makefile.
When the parameter is create, the detection folder exists and is deleted if it exists. Re-create a new folder
When the parameter is delete, detects whether the folder exists and forces the exit if it does not exist. Delete the folder.
When the parameter is other, the correct parameter hint is output.
Linux Shell Learning four