Linux Shell Learning Four

Source: Internet
Author: User
Tags case statement

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

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.