脚本一:检查对象是否存在

    判断目录是否存在,如果有就再判断是否有指定文件,不存在就创建这个文件,并把当前系统时间写入。

#!/bin/bash
if [ -e $HOME ]
thenecho "ok. $HOME exist."if [ -e $HOME/testing ]
thenecho "Appending date to existing file"date >> $HOME/testing
elseecho "Creating new file"date >> $HOME/tesingfi
elseecho "Sorry.you do not have a HOME directory"
fi
[root@www ~]# sh t2.sh 
ok. /root exist.
Creating new file


脚本二:判断是否是文件

    判断指定目录文件是否存在,如果存在,继续判断是否是一个文件

#!/bin/bash
#check if a file
if [ -e $HOME ];thenecho "The object exist. is it a file?"if [ -f $HOME ];thenecho "Yes. it is a file."elseecho "No. it is not a file!"if [ -f $HOME/.bash_history ];thenecho "But this is a file!"fi
fi
elseecho "Sorry.the object does bot exist."
fi
[root@www ~]# sh t3.sh 
The object exist. is it a file?
No. it is not a file!
But this is a file!

脚本三:检查文件是否可读

#!/bin/bash
#check if you can read a file
pwfile=/etc/shadow
if [ -f $pwfile ];thenif [ -r $file ];thentail -n1 $pwfileelseecho "Sorry. I am unable to read the $pwfile file"fi
elseecho "Sorry. the file $file does not exist"
fi
[root@www ~]# sh t4.sh 
admin:$6$sSKhOTAb$OaDGmJufvzbsZj4zFkdVFtvVYrVECdaGRvOB7Zrt9RSgmuiNxhlAIV5mtEkFSuX9gRxZ5LwxkXo06Ro0SN2Tm.:16818:0:99999:7:::


脚本四:检查空文件-s

#!/bin/bash
#testing if a file is empty
file=file55
touch $file
if [ -s $file ];thenecho "The $file file exist and has data in it"
elseecho "The $file exist and is empty."
fi
date > $file
if [ -s $file ];thenecho "The $file file has data in it"
elseecho "The $file is still empty."
fi
[root@www ~]# sh t5.sh 
The file55 exist and is empty.
The file55 file has data in it
[root@www ~]# sh t5.sh 
The file55 file exist and has data in it
The file55 file has data in it



脚本五:检查是否可写

#!/bin/bash
#checking if a file is writeable
logfile=$HOME/t6file
touch $logfile
chmod u-w $logfile
now=`date +%Y-%m-%d-%H:%M`
if [ -w $logfile ];thenecho "The program ran at:$now" > $logfileecho "The first attempt succeeded"
elseecho "The first attempt failed"
fi
chmod u+w $logfile
if [ -w $logfile ];thenecho "The program ran at:$now" > $logfileecho "The second attempt succeeded"
elseecho "The second attempt failed"
fi
[root@www ~]# sh t6.sh 
The first attempt succeeded
The second attempt succeeded


脚本六:检查是否可执行

#!/bin/bash
#checking file execution
if [ -x tesing ];thenecho "You can run the script:"./tesing
elseecho "Sorry. you are unable to execute the script"
fi
[root@www ~]# sh t7.sh 
Sorry. you are unable to execute the script
[root@www ~]# chmod a+x tesing 
[root@www ~]# sh t7.sh 
You can run the script:


脚本七:查看系统当前存在的普通用户数

#!/bin/bash
sum=`awk -F':' '$3 >= 500' /etc/passwd | wc -l`
uname=`awk -F: '$3 >= 500 {print $1," Uid=",$3}' /etc/passwd`
if [ $sum -eq 0 ];thenecho "The system exist $sum common users."
elseecho "The system exist $sum common users:"echo "$uname"
fi[root@master1 ~]# sh user1.sh 
The system exist 2 users:
HM  Uid= 500
UU  Uid= 501


脚本八:按照日期格式(xxxx-xx-xx)每日生成一个文件,例如今天生成的文件为2013-09-23.log, 并且把磁盘的使用情况写到到这个文件中。

#!/bin/bash
d=`date "+%F"`
dir=/home/disk_usage
f=/home/disk_usage/$d.log
[ -d $dir ] || mkdir /home/disk_usageif [ $? -eq 0 ];thenecho "-------------------------------------" >> $fdf -Th >> $f
fi


脚本九:用脚本生成shell脚本文件头

1、方法一

#!/bin/bash
if ! sed -n '/^#!/p' $1 &>/dev/null
then
cat >> $1 << EOF
#!/bin/bash                                                                                                      
#author by HM
#Date & Time:`date +"%F %T"` 
EOF
fi
vim +3 $1

2、方法二

#!/bin/bash
file=$1
dt=`date +"%F %T"`
touch $file
echo "#!/bin/bash" >> $file
echo "#author by HM" >> $file
echo "#Date & Time: $dt" >> $file
vim +3 $file


脚本十:判断当前的操作系统的类型,使用if elif语句

#!/bin/bash
PLATFORM=`uname -s`
FREEBSD=
SUNOS=
MAC=
AIX=
MINIX=
LINUX=echo
echo "Identifying the platfrom on which this installer is running on .."
echoif [ "$PLATFORM" = "FreeBSD" ];thenecho "This is FreeBSD system."FREEBSD=1
elif [ "$PLATFORM" = "SunOS" ];thenecho "This is Solaris system."SUNOS=1
elif [ "PLATFORM" = "Darwin" ];thenecho "This is Mac OSX system."MAC=1
elif [ "$PLATFORM" = "AIX" ];thenecho "This is AIX system."AIX=1
elif [ "$PLATFORM" = "MINIX" ];thenecho "This is MINIX system."MINIX=1
elif [ "$PLATFORM" = "Linux" ];thenecho "This is Linux system."LINUX=1
elseecho "Failed to identify this Openrating System,Abort!"exit 1
fiecho "Starting to install the software..."
echoexit 0

执行结果

[root@salve1 ~]# sh sys.sh Identifying the platfrom on which this installer is running on ..This is Linux system.
Starting to install the software...


脚本十一:打印九九乘法口诀

#!/bin/bash
for i in {1..9}
do
for j in `seq 1 $i`dom=`echo "$i" "*" "$j" | bc`echo -n "$j "x" $i "=" $m" " "doneecho " "
done
# sh 9x9.sh 
1 x 1 = 1   
1 x 2 = 2  2 x 2 = 4   
1 x 3 = 3  2 x 3 = 6  3 x 3 = 9   
1 x 4 = 4  2 x 4 = 8  3 x 4 = 12  4 x 4 = 16   
1 x 5 = 5  2 x 5 = 10  3 x 5 = 15  4 x 5 = 20  5 x 5 = 25   
1 x 6 = 6  2 x 6 = 12  3 x 6 = 18  4 x 6 = 24  5 x 6 = 30  6 x 6 = 36   
1 x 7 = 7  2 x 7 = 14  3 x 7 = 21  4 x 7 = 28  5 x 7 = 35  6 x 7 = 42  7 x 7 = 49   
1 x 8 = 8  2 x 8 = 16  3 x 8 = 24  4 x 8 = 32  5 x 8 = 40  6 x 8 = 48  7 x 8 = 56  8 x 8 = 64   
1 x 9 = 9  2 x 9 = 18  3 x 9 = 27  4 x 9 = 36  5 x 9 = 45  6 x 9 = 54  7 x 9 = 63  8 x 9 = 72  9 x 9 = 81


脚本十二:case的使用方法

#!/bin/bash#clear清除屏幕
clearecho "          File Operation List"
echo "          ---- --------- ----"echo "Choose one of the following operations:"
echoecho "[O]pen File"
echo "[D]elete File"
echo "[R]ename File"
echo "[M]ove File"
echo "[C]opy File"
echo "[P]aste File"
echoecho -n "Please enter your operation:"
read operationcase "$operation" in"O"|"o")echoecho "Opening File ..."echo "Successed!";;"D"|"d")echoecho "Deleleting File..."echo "Successed!";;"R"|"r")echoecho "Rename File to File2"echo "Successed!";;"M"|"m")echoecho "Moving File1 to File2..."echo "Successed!";;"C"|"c")echoecho "Copying File1 to File2..."echo "Successed!";;"P"|"p")echoecho "Paste File"echo "Successed!";;*)echoecho "Error,Unknown operation."echo "Program exit!"exit 1;;
esacechoexit 0


# sh case1.sh File Operation List---- --------- ----
Choose one of the following operations:[O]pen File
[D]elete File
[R]ename File
[M]ove File
[C]opy File
[P]aste FilePlease enter your operation:oOpening File ...
Successed!


脚本十三:检测局域网在线IP

#!/bin/bash
NETWORK=192.168.1
IP=1while [ $IP -lt "30" ]
doecho -en "Pinging ${NETWORK}.${IP}..."ping -c1 -w1 ${NETWORK}.${IP} >/dev/null 2>&1if [ $? -eq 0 ];thenecho "OK"elseecho "Failed"filet IP=$IP+1
done
exit 0
# sh net.sh 
Pinging 192.168.1.1...Failed
Pinging 192.168.1.2...OK
Pinging 192.168.1.3...Failed
Pinging 192.168.1.4...Failed
Pinging 192.168.1.5...Failed


脚本十四:将指定目录下大于100k的文件移动到/tmp目录

#!/bin/bash
dir=/root/
for file in `ls $dir`
doif [ -f $file ];thendisk=`du -sb $file |awk '{print $1}'`if [ $disk -gt "102400" ];thenmv $file /tmp/fifi
done