Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of vim text Editor in Linux system

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The original question of the Red Hat examination: use the read command in the script to read the password value entered by the user, then assign it to the PASSWD variable, and display a prompt message to the user through the-p parameter, telling the user that what he is typing is about to be used as the account password. After executing the script, it automatically gets all the user names from the list file users.txt, then uses the "id user name" command to view the user's information one by one, and uses $? Determine whether the command was executed successfully, that is, to determine whether the user already exists.

To add that / dev/null is a file called a Linux black hole, redirecting output to this file is tantamount to deleting data (similar to a trash can without recycling), keeping the user's screen window simple.

[root@linuxprobe ~] # vim example.shroud: "PASSWDfor UNAME in `cat users.txt`doid $UNAME & > / dev/nullif [$?-eq 0] thenecho" Already exists "elseuseradd $UNAME & > / dev/nullecho" $PASSWD "| passwd-- stdin $UNAME & > / dev/nullif [$?-eq 0] thenecho" $UNAME, Create success "elseecho" $UNAME, Create failure "fifidone"

Execute the Shell script Example.sh that creates the user in bulk, and after entering the password set for the account, the script will automatically check and create these accounts. Since the extra information has been transferred to the / dev/null black hole file through the output redirector, under normal circumstances the screen window will have nothing but the "user account created successfully" (Create success) prompt.

In the Linux system, / etc/passwd is a file used to save user account information. If you want to confirm that the script successfully created a user account, you can open the file to see if it contains the newly created user information.

[root@linuxprobe ~] # bash Example.shEnter The Users Password: linuxprobeandy, Create successbarry, Create successcarl, Create successduke, Create successeric, Create successgeorge Create success [root@linuxprobe ~] # tail-6 / etc/passwdandy:x:1001:1001::/home/andy:/bin/bashbarry:x:1002:1002::/home/barry:/bin/bashcarl:x:1003:1003::/home/carl:/bin/bashduke:x:1004:1004::/home/duke:/bin/basheric:x:1005:1005::/home/eric:/bin/bashgeorge:x:1006:1006::/home/george:/bin/bash

Let the script automatically read the IP address (used to represent the host) from the host list file ipadds.txt and assign it to the HLIST variable, thus testing whether the host is online one by one by judging the return value after the execution of the ping command. The $(command) that appears in the script is a Shell operator that is exactly like the reverse quotation mark command in the escape characters in Chapter 3, with the same effect of executing a command in a string enclosed in parentheses or double quotes. When writing scripts, you can learn several similar new methods to show your skills in your work:

[root@linuxprobe ~] # vim CheckHosts.shallows cards binds to Bash HLIST $(cat ~ / ipadds.txt) for IP in $HLISTdoping-c 3-I 0.2-W 3$ IP & > / dev/nullif [$?-eq 0]; thenecho "Host $IP is On-line." elseecho "Host $IP is Off-line." fidone [root@linuxprobe] #. / CheckHosts.shHost 192.168.10.10 is On-line.Host 192.168.10.11 is Off-line.Host 192.168.10.12 is Off-line.

Second, learning experience

Three pictures uploaded

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report