Linux command: Case selection Structure statement

Source: Internet
Author: User

Introduction to the case command:


This command is used for conditional judgment, select structure.

1 . Command format:

Case switch in-----switch variable value

value1)

Statement-----Statement Statement

...

;;

value2)

Statement

...

;;

*)

Statement

....

;;

Esac



2 . Command function:

By selecting the structure, the results of the final output are compared for all possible conditional statements.


3 . Command parameters:

/

4, the command instance:

1, through the keyboard input characters, if the capital letter is a capital letter, lowercase is a lowercase letter.

#!/bin/bash

#

Case $ in

[0-9])

echo "It is digit"

;;

[A-z])

echo "It is lower"

;;

[A-z])

echo "It is Upper"

;;

*)

echo "It is Unknown"

;;

Esac

2, only accept the parameter start, Stop,restart,shutdown.

#!/bin/bash

#

#program

Case $ in

' Start ')

echo "Start server ...";;

' Restart ')

echo "Restart server ...";;

' Stop ')

echo "Stop server ...";;

' Status ')

echo "Running ...";;

*)

echo "' basename $ ' {start|stop|restart|status}";;

Esac

3, write a script, you can accept the option parameters, and then get each option, and the parameters of the options,

And can be based on options and parameters to make a specific operation.

#!/bin/bash

#

Declare-i shownum=0

Declare-i showusers=0

For I in ' seq 1 $# '; Do

If [$#-gt 0]; Then

Case $ in

-H|--HELP)

echo "Usage: ' basename$0 '-h|--help-c|--count-v|--verbose"

Exit 0;;

-v|--verbose)

Let Showusers=1

shift;

-c|--count)

Let Shownum=1

shift;

*)

echo "Usage: ' basename$0 '-h|--help-c|--count-v|--verbose"

Exit 8;;

Esac

Fi

Done

If [$SHOWNUM-eq 1]; Then

echo "Logged users: ' Who |wc-l '."

If [$SHOWUSERS-eq 1]; Then

echo "They is:"

W.H.O.

Fi

Fi


This article is from the "Learn Linux history" blog, please be sure to keep this source http://woyaoxuelinux.blog.51cto.com/5663865/1866118

Linux command: Case selection Structure statement

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.