11、石头、剪刀、布游戏 #!/bin/bash
game=(石头 剪刀 布) num= [ R A N D O M c o m p u t e r = [RANDOM%3] computer= [RANDOMcomputer={game[$num]}
echo “请根据下列提示选择您的出拳手势” echo “1.石头” echo “2.剪刀” echo “3.布”
read -p “请选择 1‐3:” person case $person in 1) if [ $num -eq 0 ] then echo “平局” elif [ $num -eq 1 ] then echo “你赢” else echo “计算机赢” fi;; 2) if [ $num -eq 0 ] then echo “计算机赢” elif [ $num -eq 1 ] then echo “平局” else echo “你赢” fi;; 3) if [ $num -eq 0 ] then echo “你赢” elif [ $num -eq 1 ] then echo “计算机赢” else echo “平局” fi;; *) echo “必须输入 1‐3 的数字” esac 12、编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(for 版本)
#!/bin/bash
for i in {1…254} do # 每隔0.3秒ping一次,一共ping2次,并以1毫秒为单位设置ping的超时时间 ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4.$i &>/dev/null if [ ? − e q 0 ] ; t h e n e c h o " 192.168.4. ? -eq 0 ];then echo "192.168.4. ?−eq0];thenecho"192.168.4.i is up" else echo “192.168.4.$i is down” fi done 13、编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(while 版本) #!/bin/bash
i=1 while [ i − l e 254 ] d o p i n g ‐ c 2 ‐ i 0.3 ‐ W 1192.168.4. i -le 254 ] do ping ‐c 2 ‐i 0.3 ‐W 1 192.168.4. i−le254]doping‐c2‐i0.3‐W1192.168.4.i &>/dev/null if [ ? − e q 0 ] ; t h e n e c h o " 192.168.4. ? -eq 0 ];then echo "192.168.4. ?−eq0];thenecho"192.168.4.i is up" else echo “192.168.4.$i is down” fi let i++ done 14、编写脚本测试 192.168.4.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态(多进程版) #!/bin/bash
#定义一个函数,ping 某一台主机,并检测主机的存活状态 myping(){ ping ‐c 2 ‐i 0.3 ‐W 1 $1 &>/dev/null if [ $? -eq 0 ];then echo “$1 is up” else echo "KaTeX parse error: Expected 'EOF', got '}' at position 15: 1 is down" fi }̲ for i in {1..2…i & done
15、编写脚本,显示进度条 #!/bin/bash
jindu(){ while : do echo -n ‘#’ sleep 0.2 done } jindu & cp -a $1 $2 killall $0 echo “拷贝完成”
