一个简单的记时脚本

可以实现的功能:
输入一个提前的时间(10:10, 10点10分)
到时间自动终止

#!/bin/bash
 
read -p "Please input the time (like: `date +%H:%M`): " time
echo $time
 
while :
do
    if [ `date +%H:%M` = "$time" ]
    then
        echo "$time is up!"
        break
    else
        for ((i=0;i<5;i++))
        do
            echo -n "* "
            sleep 1
        done
        echo -ne "`date +%H:%M:%S` \n"
    fi
 
done

运行结果:

[root@localhost shell]# sh 5.sh
Please input the time (like: 00:28): 00:29
00:29
* * * * * 00:28:44
* * * * * 00:28:49
* * * * * 00:28:55
* * * * * 00:29:00
00:29 is up!

继续阅读

Bash Shell实现批量ping的脚本

思路是:通过循环,去ping主机地址
用$?来判断上次命令是否成功得出主机是否存在

代码如下:

#!/bin/bash
 
for i in `seq 10`
do
    ip=192.168.113.$i
    ping -c 2 $ip > /dev/null 2>&1
    [ $? -eq 0 ] && echo "$ip is alive"  || echo "$ip is not alive"
done

结果:

[root@localhost shell]# sh 3.sh
192.168.113.1 is not alive
192.168.113.2 is alive
192.168.113.3 is not alive
192.168.113.4 is not alive
192.168.113.5 is not alive
192.168.113.6 is not alive
192.168.113.7 is not alive
192.168.113.8 is not alive
192.168.113.9 is not alive
192.168.113.10 is not alive

之前有写过,有一个叫fping工具的。
也是批量的功能,详细见ping的加强版fping

继续阅读

一个找出输入目录下的死链接脚本(Bash Shell)

下面这个脚本,只要输入目录的路径,
就可以帮你找到目录下的死链接,支持子目录查找
如输入:/etc

源码如下:

#!/bin/bash
 
#read -p "input a directory:" dir
#echo
 
#for i in $dir/*
#do
#      [ -h $i -a ! -e $i ] && echo "$i是死链接"
#done
 
read -p "input a directory:" dir
echo
 
find $dir -type l |while read i
do
      [ ! -e $i ] && echo "$i是死链接"
done

继续阅读

Bash的for的几种写法

#!/bin/bash
 
 
 
for i in 1 2 3 4 5
do
	echo -en "$i\n"
#	sleep 1
done 
echo
 
for i in `seq 5`
do	
	echo -en "$i\t"
done
echo 
 
for i in $(seq 5)
do	
	echo -en "$i\t"
done
echo 
 
for ((i=2;i<6;i++))
do
	echo -en "$i\t"
done
echo
 
for i in {5..10}
do
	echo -n "$i "
done
echo
 
for i in {a..f}
do
	echo -n "$i "
done
echo
for i in {A..Z}
do
	echo -n "$i "
done
echo
 
for i in /etc/*
do
	echo $i
done
echo
 
for i in `find /etc/ -name "*.conf"`
do
	echo $i
done
echo
 
find /etc/ -name "*.conf" | while read i
do
	echo $i
done
echo
 
`find /etc/ -name "*.conf"` | xargs echo > /tmp/find.txt
while read i 
do
        echo $i
done < /tmp/find.txt

继续阅读