判断目录是否存在,如果有就再判断是否有指定文件,不存在就创建这个文件,并把当前系统时间写入。
#!/bin/bash if [ -e $HOME ] then echo "ok. $HOME exist." if [ -e $HOME/testing ] then echo "Appending date to existing file" date >> $HOME/testing else echo "Creating new file" date >> $HOME/tesing fi else echo "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 ];then echo "The object exist. is it a file?" if [ -f $HOME ];then echo "Yes. it is a file." else echo "No. it is not a file!" if [ -f $HOME/.bash_history ];then echo "But this is a file!" fi fi else echo "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 ];then if [ -r $file ];then tail -n1 $pwfile else echo "Sorry. I am unable to read the $pwfile file" fi else echo "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 ];then echo "The $file file exist and has data in it" else echo "The $file exist and is empty." fi date > $file if [ -s $file ];then echo "The $file file has data in it" else echo "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 ];then echo "The program ran at:$now" > $logfile echo "The first attempt succeeded" else echo "The first attempt failed" fi chmod u+w $logfile if [ -w $logfile ];then echo "The program ran at:$now" > $logfile echo "The second attempt succeeded" else echo "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 ];then echo "You can run the script:" ./tesing else echo "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 ];then echo "The system exist $sum common users." else echo "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_usage if [ $? -eq 0 ];then echo "-------------------------------------" >> $f df -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 .." echo if [ "$PLATFORM" = "FreeBSD" ];then echo "This is FreeBSD system." FREEBSD=1 elif [ "$PLATFORM" = "SunOS" ];then echo "This is Solaris system." SUNOS=1 elif [ "PLATFORM" = "Darwin" ];then echo "This is Mac OSX system." MAC=1 elif [ "$PLATFORM" = "AIX" ];then echo "This is AIX system." AIX=1 elif [ "$PLATFORM" = "MINIX" ];then echo "This is MINIX system." MINIX=1 elif [ "$PLATFORM" = "Linux" ];then echo "This is Linux system." LINUX=1 else echo "Failed to identify this Openrating System,Abort!" exit 1 fi echo "Starting to install the software..." echo exit 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` do m=`echo "$i" "*" "$j" | bc` echo -n "$j "x" $i "=" $m" " " done echo " " 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清除屏幕 clear echo " File Operation List" echo " ---- --------- ----" echo "Choose one of the following operations:" echo echo "[O]pen File" echo "[D]elete File" echo "[R]ename File" echo "[M]ove File" echo "[C]opy File" echo "[P]aste File" echo echo -n "Please enter your operation:" read operation case "$operation" in "O"|"o") echo echo "Opening File ..." echo "Successed!" ;; "D"|"d") echo echo "Deleleting File..." echo "Successed!" ;; "R"|"r") echo echo "Rename File to File2" echo "Successed!" ;; "M"|"m") echo echo "Moving File1 to File2..." echo "Successed!" ;; "C"|"c") echo echo "Copying File1 to File2..." echo "Successed!" ;; "P"|"p") echo echo "Paste File" echo "Successed!" ;; *) echo echo "Error,Unknown operation." echo "Program exit!" exit 1 ;; esac echo exit 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 File Please enter your operation:o Opening File ... Successed!
脚本十三:检测局域网在线IP
#!/bin/bash NETWORK=192.168.1 IP=1 while [ $IP -lt "30" ] do echo -en "Pinging ${NETWORK}.${IP}..." ping -c1 -w1 ${NETWORK}.${IP} >/dev/null 2>&1 if [ $? -eq 0 ];then echo "OK" else echo "Failed" fi let 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` do if [ -f $file ];then disk=`du -sb $file |awk '{print $1}'` if [ $disk -gt "102400" ];then mv $file /tmp/ fi fi done