Linux Command exercise: Case selection Structure Statement exercise

Source: Internet
Author: User

Practice One:

1, receive the parameter, prompt input parameter is what type:

#!/bin/bash

#program:

#接收参数并输出参数类型

#history Donggen 2016-10-27-17:20

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bansh

Export PATH

Case $ in

[[:d Igit:]])

echo "It is Digit"

;;

[[: Lower:]])

echo "It is Lower"

;;

[[: Upper:]])

echo "It is Upper"

;;

*)

echo "It is Unknown"

;;

Esac


Exercise two:

2, write a script, you can accept the options and 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. Like Administrators.sh-add Tom,jerry--del Tom,blair

-v|--verbose-h|--help

#!/bin/bash

#program:

#练习接收参数, and get the parameters

#history Donggen 2016-10-27-22:40

Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bash

Export PATH

Debug=0

Add=0

Del=0


For I in ' seq 0 $# '; Do

If [$#-gt 0]; Then

Case $ in

-v|--verbose)

Debug=1

shift;

-H|--HELP)

echo "Usage: ' basename $--add user_list--del user_list-v|--verbose-h|--help"

Exit 0

;;

--add)

Add=1

Addusers=$2

Shift 2

;;

--del)

Del=1

Delusers=$2

Shift 2

;;

*)

echo "Usage: ' basename $--add user_list--del user_list-v|--verbose-h|--help"

Exit 7

;;

Esac

Fi

Done

If [$ADD-eq 1]; Then

For the USER in ' echo $ADDUSERS |sed ' [email protected],@ @g '; Do

If ID $USER &>/dev/null; Then

[$DEBUG-eq 1] &&echo "$USER exists."

Else

Useradd $USER

[$DEBUG-eq 1] &&echo "ADD user $USER finished."

Fi

Done

Fi

If [$DEL-eq 1]; Then

For the USER in ' echo $DELUSERS |sed ' [email protected],@ @g '; Do

If ID $USER &>/dev/null; Then

Userdel-r $USER

[$DEBUG-eq 1] &&echo "Delete $USER finished."

Else

[$DEBUG-eq 1] &&echo "$USER not exist."

Fi

Done

Fi






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

Linux Command exercise: Case selection Structure Statement exercise

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.