Linux中shell脚本案例(三)
下面是案例:
1、查找当前网段内IP用户,重定向到ip.txt文件中
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#/bin/bash a=1 while : do a=$(($a+1)) if test $a -gt 255 then break else echo $(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') ip=$(ping -c 1 192.168.0.$a | grep "ttl" | awk '{print $4}'| sed 's/://g') echo $ip >> ip.txt fi done |
结果:
文档。
2、case语句练习
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/bin/bash clear echo "enter a number from 1 to 5:" read num case $num in 1) echo "you enter 1" ;; 2) echo "you enter 2" ;; 3) echo "you enter 3" ;; 4) echo "you enter 4" ;; 5) echo "you enter 5" ;; *) echo "error" ;; esac |
结果:
1 2 |
2 echo "you enter 2" |
3、内置命令的使用
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#/bin/bash clear echo "Hello, $USER" echo echo "Today 's date id `date`" echo echo "the user is :" who echo echo "this is `uname -s`" echo echo "that's all folks! " |
结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
Hello, root2 Today 's date id Fri Jun 10 23:16:57 CST 2016 the user is : root1 pts/0 2016-06-10 21:53 () root2 pts/1 2016-06-10 23:13 () root3 pts/2 2016-06-10 21:16 () this is Linux that's all folks! |
4、打印无密码用户
代码:
1 2 3 |
#/bin/bash echo "No Password User are :" echo $(cat /etc/shadow | grep "!!" | awk 'BEGIN { FS=":" }{print $1}') |
结果:
1 2 |
No Password User are : dsuscc xxxx |
5、检查端口号是否已启动
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/bin/bash n=1 echo "检查xxx服务..." while true do if test $n -gt 20 then echo "xxx服务启动失败" break fi sleep 5 n=$(($n+1)) port=`netstat -antp | grep "0.0.0.0:8080"` if [ ${#port} -gt 3 ]; then echo "xxx服务已经启动" break; fi done |
结果:
1 2 |
检查xxx服务... xxx服务已经启动 |
- Linux中shell脚本案例(二)
- PHP中利用cookie实现自动登录(案例)