Shell Script 100 Example "II"

Source: Internet
Author: User

11. Scripting, man-machine < stone, scissors, cloth > Games
#!/bin/bash
#Author: The Adventures of Tintin (Jacob)
Game= (Stone scissors cloth)
NUM=$[RANDOM%3]
computer=${game[$num]}
#通过随机数获取计算机的出拳
#出拳的可能性保存在一个数组中, game[0],game[1],game[2] are different possibilities in 3, respectively.

echo "Please select your punch gesture according to the following tips"
echo "1. Stone"
echo "2. Scissors"
echo "3. Cloth"

Read‐p "Please select 1‐3:" Person
Case $person in
1)
If [$num ‐eq 0];then

echo "Draw"
elif [$num ‐eq 1];then

echo "You Win"
Else

echo "Computer wins"
fi;;
2)
If [$num ‐eq 0];then

echo "Computer wins"
elif [$num ‐eq 1];then

echo "Draw"
Else

echo "You Win"
fi;;
3)
If [$num ‐eq 0];then

echo "You Win"
elif [$num ‐eq 1];then

echo "Computer wins"
Else

echo "Draw"
fi;;
*)
echo "must enter number of 1‐3"
Esac

12. Write a script to test which hosts are turned on and which are powered off in the entire 192.168.4.0/24 network segment
Status (for version)
#!/bin/bash
For i in {1..254}
Do
Ping‐c2‐i0.3‐w1 192.168.4. $i &>/dev/null
If [$?–eq 0];then
echo "192.168.4. $i is Up"
Else
echo "192.168.4. $i is Down"
Fi
Done

13. Write a script to test which hosts are turned on and which are powered off in the entire 192.168.4.0/24 network segment
Status (while version)
#!/bin/bash
I=1
While [$i ‐le 254]
Do
Ping‐c2‐i0.3‐w1 192.168.4. $i &>/dev/null
If [$?–eq 0];then
echo "192.168.4. $i is Up"
Else
echo "192.168.4. $i is Down"
Fi
Let i++
Done

14. Write a script to test which hosts are turned on and which are powered off in the entire 192.168.4.0/24 network segment
Status (multi-process version)
#!/bin/bash
#Author: The Adventures of Tintin (Jacob)
#定义一个函数, ping a host, and detect the host's survival status
Myping () {
PING‐C2‐I0.3‐W1 $ &>/dev/null
If [$?‐eq 0];then
echo "is up"
Else
echo "is down"
Fi
}
For i in {1..254}
Do
Myping 192.168.4. $i &
Done
#使用 & symbols, put the executed function into the background execution
#这样做的好处是不需要等待 Ping the response of the first host, you can continue to ping the second host concurrently, and so on.

15. Write a script to show the progress bar
#!/bin/bash
Jindu () {
While:
Do
Echo‐n ' # '
Sleep 0.2
Done
}
Jindu &
Cp‐a $
Killall $!
echo "Copy Complete"

16. Progress bar, Dynamic hour version
#!/bin/bash
#定义一个显示进度的函数, quick Screen Display | / ‐ \
Rotate_line () {
interval=0.1 #设置间隔时间
count= "0" #设置 the number of 4 shapes, the default number is 0 (does not represent any image)
While:
Do
Count= ' expr $COUNT + 1 ' #执行循环, COUNT each loop plus 1, (represents 4 different shapes respectively)
Case $COUNT The value of the in #判断 COUNT, the shape is not the same as the value shown
"1") #值为 1 Display ‐
Echo‐e '-' \b\c '
Sleep $INTERVAL
;;
"2") #值为 2 shows \ \, first \ is escaped
Echo‐e ' \ \ ' "\b\c"
Sleep $INTERVAL
;;
"3") #值为 3 Display |
Echo‐e "|\b\c"
Sleep $INTERVAL
;;
"4") #值为 4 Display/
Echo‐e "/\b\c"
Sleep $INTERVAL
;;
*) #值为其他时, resets the COUNT to 0
count= "0";;
Esac
Done
}
Rotate_line

17.9*9 Multiplication Table (write shell script, print 9*9 multiplication table)
#!/bin/bash

For i in ' seq 9 '
Do
For j in ' Seq $i '
Do
Echo‐n "$i * $j =$[i*j]"
Done
Echo
Done

18. Use the dead loop to display the packet traffic sent by the ETH0 network card in real time
#!/bin/bash
#Author: The Adventures of Tintin (Jacob)
While:
Do
Echo ' Local NIC eth0 traffic information is as follows: '
Ifconfig eth0 | grep "RX Pack" | awk ' {print $} '
Ifconfig eth0 | grep "TX Pack" | awk ' {print $} '
Sleep 1
Done

19. Use the list of people in the User.txt file to automatically create the corresponding account and configure the initial password on the computer
#!/bin/bash
#本脚本执行, you need to prepare a user.txt file in advance that contains several user name information
For i in ' Cat User.txt '
Do
Useradd $i
echo "123456" | Passwd‐‐stdin $i
Done

20. Write bulk modify extension scripts, such as batch to modify TXT file to doc file
#!/bin/bash
#执行脚本时, you need to add positional parameters to the script
#脚本名 txt doc (you can modify the txt extension to doc)
#脚本名 doc jpg (can modify Doc's extension to jpg)

For i in "LS *.$1"
Do
MV $i ${i%.*}.$2
Done


Shell Script 100 Example "II"

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.