1. Hello World
vim hello_world.sh
#!/bin/bash
echo "Hello World!"
1.1 输出
chmod +x hello_world.sh
[root@localhost shell]# ./hello_world.sh
Hello World!
2. 检查存活主机
for循环
[root@localhost shell]# cat check_hosts.sh
#!/bin/bash #检查主机状态 for i in {1..254} do ping -c2 -i0.3 -w1 192.168.1.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.1.$i is up." else echo "192.168.1.$i is down." fi done
while循环
#!/bin/bash i=1 while [ $i -le 254 ] do ping -c2 -i0.3 -W1 192.168.1.$i &>/dev/null if [ $? -eq 0 ];then echo "192.168.1.$i is up." else echo "192.168.1.$i is down." fi let i++ done
3. 猜数字
[root@localhost shell]# cat guess_num.sh
#!/bin/bash num=$[ RANDOM % 100 +1 ] while : do read -p "Enter a num: " cat if [ $cat -eq $num ];then echo "right!" break elif [ $cat -le $num ];then echo "small!" else echo "big!" fi done
评论 (0)