16.03.18 the use of "practice" case Write a script

Source: Internet
Author: User

Write a script that can combine the options and parameters, then get each option, and the parameters of the options, and can be based on options and parameters to make a specific operation, such as :adminusers.sh--add Tom,blair- -del Tom,blair-v|--verbose-h|--help


#!/bin/bash

#

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

Adduser=$2

Shift 2;;

--del)

Del=1

Deluser=$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 USER in ' echo $ADDUSER | Sed ' s#,# #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 USER in ' echo $DELUSER | Sed ' s#,# #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 EXISTS"

Fi

Done

Fi


16.03.18 the use of "practice" case Write a script

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.