Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell Loop statement: for

Shulou Source: shulou.com Published: 2022-06-02 12:34:35 10月01日 Update

[root@master1-192,168117-18 ~] # vim users.txt

[root@master1-192,168117-18 ~] # vim Example.sh

#! / bin/bash

Read-p "Enter The Users Password:" PASSWD

For UNAME in cat users.txt

Do

Id $UNAME & > / dev/null

If [$?-eq 0]

Then echo "Already exits"

Else useradd $UNAME & > / dev/null

Echo "$PASSWD" | passwd-- stdin $UNAME & > / dev/null

If [$?-eq 0]

Then echo "$UNAME,create success!"

Else echo "$UNAME,Create failure!"

Fi

Fi

Done

[root@master1-192,168117-18 ~] # bash Example.sh

Enter The Users Password:1234

Zhangsan,create success!

Lisi,create success!

Wangmazi,create success!

Hanmeimei,create success!

Lilei,create success!

Bobo,create success!

Note: / dev/null is a file called Linux black hole. Redirecting output to this file is tantamount to deleting data (similar to trash bins without recycling), which keeps the user's screen window simple.

[root@master1-192,168117-18 ~] # vim ChechHosts.sh

#! / bin/bash

HLIST=$ (cat ~ / ipadds.txt) # $(executable command) = executable command

For IP in $HLIST

Do

Ping-c 3-I 0.2-W $IP & > / dev/null

If [$?-eq 0]; then

Echo "Host $IP is on-line."

Else

Echo "Host $IP is off-line."

Fi

Done

Tags: Commands files simplicity information functions garbage dustbins comments screens data users black holes output statements loops Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB Linux Huawei vpn