Linux Bash Shell Learning (11): Process Control-case

Source: Internet
Author: User
Tags builtin

This article is part 3 of the fifth chapter of "Learning the bash Shell" 3rd Edition, Flow Control, but we will not limit this article. Flow control is a very common part of any programming language, including case. Here, we will continue to learn about them.

Case checks whether the string style matches and the number is equal to each other for different processing. Modify the if-then-else structure to a better reading mode. The format is as follows:

CaseExpression
In

Pattern1)

Statements
;;

Pattern2)

Statements
;;

...

Esac


Looking at the end esac and the fi IN if/else are both written in reverse, which may be a characteristic of bash. Note that statement is followed by two semicolons. Any style can contain multiple styles, which are separated by "|". If one of them is matched, statement is executed. Case will be checked in sequence until matching. The following is an example of processing different image files with different suffixes. We do not want to take specific measures, but mainly look at the use of case.

For filename in "$ @"; do

Case
$ FilenameIn

*. Jpg)
Exit 0;;

*. Tga)
<Do statement>;;

*. Xpm)
<Do statement>

;;

*. Pcx)
<Do statement>

;;

*. Tif)
<Do statement>

;;

*. Gif)
<Do statement>

;;

*)
Echo "procfile: $ filename is an unknown graphics file ."

Exit 1;;

Esac

Done

In other advanced languages, such as C language and common switch methods, they all have a default option, indicating the processing method when none of the above matches, in bash, the same processing can be performed at the end. Here is another example.

Cd ()

{

# Check the number of write but parameters, and process 0 or 1 parameters, 2 parameters, and others respectively.

Case
"$ #"In

# If there are one or zero parameters, use the original cd processing method. Note | the use of this parameter indicates that either of these two conditions can be matched.

0 | 1)
Builtin cd $1 ;;

# For the two parameters, replace all strings matching parameter 1 in the current path with style 2 and jump to the new path.

2)
Newdir =$ {PWD // $1/$2}

# Here is an example of case nesting. If the change path is the same as the original one, that is, there is no change in the processing, and other (that is, there is a change) processing.


Case "$ newdir" in

$ PWD) echo "bash: cd: bad substitution"> & 2;

Return 1 ;;

*) Builtin cd "$ newdir ";;

Esac ;;



# Process two or more parameters.

*) Echo "bash: cd: wrong arg count" 1> & 2; return 1 ;;

Esac

}

Related links:
My articles on Linux operations

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.