Linux Bash programming case statements and scripting (eight)

Source: Internet
Author: User
Tags case statement

Linux Bash programming case statements and scripting (eight)

1.case statement in the following format:

Case variable in

Value 1)

Statement 1

...

;; Each branch statement ends with a semicolon

Value 2)

Statement 2

...

;;

*) Other, equivalent if statement else

Statement 3

...

;;

ESAC the entire case statement ends

2 . Write a script that adds and deletes the user arbitrarily, with the following requirements:

2.1: If the script option is-A or--add:, the user will be added; If the option is-D or--del, the user is deleted,

            If it is-h or--help display Help information, if it is-V or--verbose displays the execution process,

2.2: Parameters following the script option can be arbitrarily specified by multiple users and separated by commas , password and user name;

2.3: The user exists on the prompt cannot be added, the user does not exist can not delete the user;

Vim admuser.sh Create a script file:

#!/bin/bash

Add=0 Add User variable switch

Del=0 Delete User variable switch

Debug=0 as a display execution process variable switch

#以下判断脚本后面的参数是否正确

For I in ' seq 1 $# '; Do

If [$#-gt 0];then

Case $ in

-v|--verbose)

Debug=1

Shift

;;

-H|--HELP)

echo "Usage: ' BaseName $-v|--verbose | -h|--help | -a|--add user1,user2 ...-d|--del user1,user2 ... "

Exit 0

;;

-a|--add)

Add=1

Addusers=$2

Shift 2

                     ;;

-d|--del)

Del=1

Delusers=$2

Shift 2

;;

*)

echo "Usage: ' BaseName $-v|--verbose | -h|--help | -a|--add user1,user2 ...-d|--del user1,user2 ... "

Exit 8

;;

Esac

Fi

Done


#以下判断增加用户

If [$ADD-eq 1]; Then

For USER in ' Echo ' $ADDUSERS | Sed ' s#,# #g '; Do remove to add user list

if! ID $USER &>/dev/null; Then determine if the user exists and does not exist add

Useradd $USER

echo $USER | passwd--stdin $USER &>/dev/null

[$DEBUG-eq 1] && echo "$USER add successfully"

Else

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

Fi

Done

Fi



#以下判断删除用户

If [$DEL-eq 1]; Then

For USER in ' Echo ' $DELUSERS | Sed ' s/,//g '; Do remove the list of users to delete

If ID $USER &>/dev/null; Then determine if the user exists, and then delete

Userdel-r $USER

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

Else

[$DEBUG-eq 1] && echo "$USER no Exist"


Fi

Done

Fi




This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1764856

Linux Bash programming case statements and scripting (eight)

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.