需求·:1、给定一个ip.txt文件,用脚本ping通 2、且执行文件方式为 ./脚本名 file 3、无参数或者文件名不对皆报错
代码:
#!/usr/bin/bash if [ ! -f $1 ] #进行条件测试,看$1(位置变量1,即第一个参数)是否 是文件 then echo "error file" exit fi if [ $# -eq 0 ] # $#是查看位置变量参数个数 then echo " `basename $0` lack parameter" #反引号将里面内容先 exit 抛给shell,返回结果代替 原来整个单引号。 fi for ip in `cat ip.txt` #这里为什么能用cat? do ping -c1 $ip &>/dev/null #去ping读到的所有ip并将结 if [ $? -eq 0 ] 果重定向到垃圾箱中 then echo "up" else echo "down" fi done
原文:https://www.cnblogs.com/yanyan-python/p/12431761.html