Linux commands: Bash scripting-Script

Source: Internet
Author: User

Exercise: Write a script adminuser33.shthat uses the following format:

Adminuser33.sh--add-del-h|--help-v|--verbose

among them, - H option can only be used alone to display help information; --add option, a new user is added; - v option, the new user appears after the new user is displayed, and the--del option deletes the user.


#!/bin/bash
#
Debug=0
Add=0
Del=0

For I in ' seq 1 $# '; Do

If [$#-gt 0]; Then

    Case in                   have a match to end the entire case
   -v|--verbose)
      debug=1
      shift    ;;
   -H|--HELP)
      echo "Usage: ' basename $--add user_list--del user_list-v|--verbose-h|--help"
  &nbs P   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 Auser in `echo $ADDUSERS | Sed ' s@,@ @G`; Do
ifID $AUSER &>/dev/null; Then
[$DEBUG-eq 1]&& echo "$AUSER exists."
Else
Useradd $AUSER
[$DEBUG-eq 1] && echo "ADD user $AUSER finished."
fi
Done
Fi

If [$DEL-eq 1]; Then
For Duser in ' echo $DELUSERS | Sed ' [email protected],@ @g '; Do
If ID $DUSER &>/dev/null; Then
Userdel-r $DUSER
[$DEBUG-eq 1] && echo "Delete $DUSER finished."
Else
[$DEBUG-eq 1] && echo "$DUSER not exists."
Fi
Done
Fi
echo "$DEBUG add: $ADD del: $DEL"



[Email protected] ~]#/adminuser33.sh-v--add tom2,tom3--del tom2
ADD user Tom2 finished.
ADD user Tom3 finished.
Delete Tom2 finished.
1 add:1 del:1



Exercise: Write a script showlogged.sh , which is in the following format:

Showlogged.sh-v-c-h|--help

among them, - H option can only be used alone to display help information; - C option, displays the number of all users logged on on the current system; - v option displays both the number of users logged in simultaneously and information about the logged-on user, such as


1 #!/bin/bash
2 #
3
4
5 Declare shownum=0
6 Declare showusers=0
7 for I in 'seq 1 $#` ; Do
8 If [$#-GT 0]; Then
9 Case $ in
10-C)
Shownum=1
12Shift
13;;
14-v|--verbose)
Showusers=1
Shownum=1
Shift
18;;
19-H|--HELP)
echo "Showlogged.sh-v-c-h|--help."
Exit 0
22;;
23 *)
echo "Showlogged.sh-v-c-h|--help."
Exit 7
26;;
Esac
-Fi
Done
If [$SHOWNUM-eq 1]; Then
echo "logged Usrs: ' Who | Wc-l ' "
Who
If [$SHOWUSERS-eq 1]; Then
echo "All users: ' cat/etc/passwd | cut-d:-f1 |wc-l ' "
+ fi
+ fi


[Email protected] ~]#./showloged.sh-v
Logged Usrs:3
Root pts/0 2016-10-24 07:55 (*.*.135.13)
Root PTS/1 2016-10-24 13:34 (*.*.135.13)
Root PTS/2 2016-10-24 15:30 (*.*.135.13)
All users:62
[Email protected] ~]#./showloged.sh-c
Logged Usrs:3
Root pts/0 2016-10-24 07:55 (*.*.135.13)
Root PTS/1 2016-10-24 13:34 (*.*.135.13)
Root PTS/2 2016-10-24 15:30 (*.*.135.13)


---end---

Linux commands: Bash scripting-Script

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.