May 23, 2021 That's what Linux should learn
The for-loop statement allows the script to read multiple messages at once and then manipulate the information one by one, which is perfect when the data to be processed has scope. The syntax format of the for loop statement is shown in Figure 4-20.
Figure 4-20 for the syntax format of the loop statement
The following uses the for loop statement to read multiple user names from the list file, and then create user accounts and set passwords for each of them. S tart by creating a list file of user .txt, with each user name on a separate line. Readers can decide for themselves the specific user name and number:
[root@linuxprobe ~]# vim users.txt andy barry carl duke eric george
Next, write the Shell script Example.sh. U se the read command in the script to read the password value entered by the user, assign it to the PASSWD variable, and display a prompt message to the user with the -p parameter that the user is entering is about to be used as an account password. After executing the script, all user names are automatically obtained from the list file user.txt, and then the user's information is viewed one by one using the "id user name" command, and $?
Need to say more, /dev/null is a file called a Linux black hole, and redirecting the output information to this file is equivalent to deleting data (similar to a trash can without recycling) to keep the user's screen window simple.
[root@linuxprobe ~]# vim Example.sh
read -p "Enter The Users Password : " PASSWD
for UNAME in `cat users.txt`
do
id $UNAME &> /dev/null
if [ $? -eq 0 ]
then
echo "Already exists"
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
The Shell script that performs bulk creation Example.sh user is automatically checked and created by the script after entering the password set for the account. Since excess information has been transferred through the output redirector to the /dev/null black hole file, the screen window normally has nothing but the "Create success" prompt.
In Linux systems, /etc/passwd is a file used to hold user account information. If you want to confirm that the script successfully created the user account, you can open the file to see if there is any of these newly created user information.
[root@linuxprobe ~]# bash Example.sh Enter The Users Password : linuxprobe andy , Create success barry , Create success carl , Create success duke , Create success eric , Create success george , Create success [root@linuxprobe ~]# tail -6 /etc/passwd andy:x:1001:1001::/home/andy:/bin/bash barry:x:1002:1002::/home/barry:/bin/bash carl:x:1003:1003::/home/carl:/bin/bash duke :x:1004:1004::/home/duke:/bin/bash eric:x:1005:1005::/home/eric:/bin/bash george:x:1006:1006::/home/george:/bin/bash
Do you remember the script that the test host used to learn the dual branch if conditional statement was online? Now that we've mastered for-loop statements, we might as well do something cooler, such as try getting scripts to automatically read the list of hosts from text and then automatically testing whether those hosts are online one by one.
Start by creating a host list file ipadds .txt:
[root@linuxprobe ~]# vim ipadds.txt 192.168.10.10 192.168.10.11 192.168.10.12
The preceding dual-branch if conditional statement is then combined with the for loop statement, allowing the script to automatically read the IP address (used to represent the host) from the host list file ipadds.txt and assign it to the HLIST variable to test whether the host is online one by one by determining the return value after the ping command is executed. T
he $(command) that appears in the script is a Shell
命令
operator that is exactly similar to the back quote command in chapter 3 escape characters, and the effect is also to execute commands in strings enclosed in parentheses or double quotes.
As you write your scripts, learn a few more similar new ways to make a big show at your work:
[root@linuxprobe ~]# vim CheckHosts.sh
HLIST=$(cat ~/ipadds.txt)
for IP in $HLIST
do
ping -c 3 -i 0.2 -W 3 $IP &> /dev/null
if [ $? -eq 0 ] ; then
echo "Host $IP is On-line."
else
echo "Host $IP is Off-line."
fi
done
[root@linuxprobe ~]# ./CheckHosts.sh
Host 192.168.10.10 is On-line.
Host 192.168.10.11 is Off-line.
Host 192.168.10.12 is Off-line.