Linux--shell Programming (vi) using scripting options and combination condition testing

Source: Internet
Author: User
Tags stdin

Write a script:

1. Add 10 users user1 to User10, password with user name, but only if the user does not exist to add;

#!/bin/bash# forIinch{1..Ten}; Do    if!IDUser$i &>/dev/NULL; ThenUseradd user$iEcho "user$i"|passwd--stdin user$iElse        Echo "User$i exists"        fi Done

Delete the above 10 users

#!/bin/bash# forIinch{1..Ten}; Do    if IDUser$i &>/dev/NULL; ThenUserdel-r user$i &>/dev/nullEcho "user$i deleted"    Else        Echo "user$i NOT EXISTS"    fi Done

Extension: Adds or deletes a specified number of users based on input parameters,

#!/bin/Bash2 #  3 if[$#-lt1]; Then  4     Echo "Usage:no ARGs"  5 fi  6 if[ $1=="--add"]; Then  7  forIinch`Echo$2|sed 's/,//g'`; Do  8     if ID$I &>/dev/NULL; Then  9        Echo "$I exists" Ten     Else  OneUseradd $I A         Echo$I |passwd--stdin $I &>/dev/NULL  -         Echo "Add $I finished"  -     fi  the  Done  - elif[ $1=="--del"]; Then  -  forIinch`Echo$2|sed 's/,//g'`; Do  -     if!ID$I &>/dev/NULL; Then  +         Echo "$I NOT EXISTS"  -     Else  +Userdel-R $I A         Echo "Delete $I finished"  at     fi

Test:

Integer test

-le-lt-gt-ge-eq-ne

Character test

= = = > <-n-z

File test

-e-f-d-r-w-x

Combination test conditions

-A: With relation

-O: or relationship

! : Non-relational

If [$#-gt 1-a $#-le 3]

If [$#-gt 1] && [$#-le 3]

Linux--shell Programming (vi) using scripting options and combination condition testing

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.