Shell (3) application

Source: Internet
Author: User

Shell Script (iii)

A For Loop statement: (Unknown number of cycles)

For Variable name Value list
Do
Command sequence
Done

(1) Bulk add users by Name list
Vim/root/user.txt
?
Zhangsan
Wangwu
Xiaobai
vim/root/uadd.sh
#!/bin/bash
ulist=$ (Cat/root/user.txt)
For uname in $ULIST
Do
Useradd $uname
echo "123456" | passwd--stdin $uname &>/dev/null
Done
sh/root/uadd.sh
cat/etc/passwd

① Develop Employee List files

② scripting (For Loop statement format)

③ Execute Script

(2) Check host status based on IP address
Vim/root/ipadds.txt
192.168.1.1
192.168.1.2
192.168.10.1
Vim chkhosts.sh
#!/bin/bash
hlist=$ (Cat/root/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 was up"
else echo "host $IP is down"
Fi
Done
chmod +x chkhosts.sh
SH chkhosts.sh

① Developing IP address lists

② scripting (For Loop statement format)

③ Execute Script

Two-While loop statement: (Number of cycles known)

While condition test
Do
Command sequence
Done

(1) Batch to add a regular number of users:
① use I to control the number of user names
② in the loop by Let + + (equivalent to i= ' expr $i + 1 ') to increase I, and so on.

(2) Guess the price of the game:
vim/pricegame.sh
#!/bin/bash
price=$ (expr $RANDOM% 1000)
Times=0
echo "Price between 0 and 999 pelease input It"
While True
Do
Read-p "Input price:" int
Let times++
If [$price-eq $int]; Then
echo "Yes,you is right."
echo "You have anwser $times time"
Exit 0
elif [$int-gt $price]; Then
echo "Price was too high"
Else
echo "Price was too low"
Fi
Done
chmod +x/pricegame.sh
./pricegame.sh

① a random integer of less than 2 16 is obtained by using the environment variable random, and the remainder of the 1000 is computed to obtain a random price of 0~999.
② repeated test operations can be implemented by a while loop that ture as a test condition
③ when the user guesses the thing to abort the operation
④ using variables to manipulate the number of probabilities

Three Case Branch statements:

Case Variable value in
Mode 1)
Command sequence 1
;;
Mode 2)
Command Sequence 2
;;
......
*)
Default command sequence
Esac

(1) Keystroke type recognition (Case Branch statement format):

(2) Write the System service script (case Branch statement format):

Vim/myprogs
Case "$" in
Start
Echo-n "Zhengzai qi Dong $sleep fuwu"
If Sleep 7200 &
Then
echo "OK"
Fi
;;
Stop
Echo-n "Zheng Guan bi dong sleep fuwu"
Pkill "Sleep" &/dev/null
echo "OK"
;;
Status
If Pgrep "Sleep" &>dev/null
Then
echo "Fuwuyikaiqi"
Else
echo "Fuwuyitongzhi"
Fi
;;
Restart
$ stop
$ start
;;
*)
echo "TONGAF: $ {Start|stop|restart|status}"
Esac
chmod +x/myprogs
./myprogs Start
./myprogs Status
./myprogs Stop

Shell (3) application

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.