判斷/var/目錄下所有文件的類型
[root@www sh.log]# cat typefile.sh #!/bin/bash #author:DYW #顯示目錄下文件類型 if [ $# -lt 1 ];then echo "Please enter a directory" exit fi #================================= if [ ! -d $1 ];then echo "$1 is not a directory or No such directory" exit fi #================================= dirname=`echo $1 | sed -r 's@(.*)/$@\1@'` for w in `ls -A $1` ;do filetype=`ls -dl $dirname/$w |cut -c1` case $filetype in d) echo "$dirname/$w is a directory" ;; -) echo "$dirname/$w is a common file" ;; l) echo "$dirname/$w is a link file" ;; p) echo "$dirname/$w is a pipe file" ;; b) echo "$dirname/$w is a block file" ;; c) echo "$dirname/$w is a character file" ;; s) echo "$dirname/$w is a socket file" ;; *) echo "$dirname/$w is a unknown file" ;; esac done [root@www sh.log]# bash typefile.sh /etc/ /etc/adjtime is a common file /etc/aliases is a common file /etc/aliases.db is a common file /etc/alternatives is a directory /etc/anacrontab is a common file /etc/asound.conf is a common file ....
添加10個用戶user1-user10,密碼同用戶名
[root@www sh.log]# cat adduser1-10.sh #!/bin/bash #author:DYW #添加uesr1-user10,用戶密碼相同 if [ $# -lt 1 ];then echo -e "please add a option\n\t-a\tadd\tuser1-user10\n\t-d\tdel\tuser1-user10" exit fi for w in `seq 10`;do case $1 in -a) if id user$w &> /dev/null;then echo "user$w"|passwd --stdin "user$w" &> /dev/null echo "user$w already existed!" else useradd user$w &> /dev/null echo "user$w"|passwd --stdin "user$w" &> /dev/null echo "user$w add complete" fi ;; -d) if id user$w &> /dev/null;then userdel -r user$w echo "user$w del complete" else echo "No such user$w" fi ;; *) echo -e "Unknow option,please enter'-a'or'-d'" exit esac done [root@www sh.log]# bash adduser1-10.sh -a user1 add complete user2 add complete user3 add complete user4 add complete user5 add complete user6 add complete user7 add complete user8 add complete user9 add complete user10 add complete [root@www sh.log]# bash adduser1-10.sh -d user1 del complete user2 del complete user3 del complete user4 del complete user5 del complete user6 del complete user7 del complete user8 del complete user9 del complete user10 del complete [root@www sh.log]# bash adduser1-10.sh -q Unknow option,please enter'-a'or'-d'
/etc/rc.d/rc3.d目錄下分別有多個以K開頭和以S開頭的文件;分別讀取每個文件,以K開頭的文件輸出為文件加stop,以S開頭的文件輸出為文件名加start;
“ K34filename stop”
“S66filename start”
[root@www sh.log]# cat KSfile.sh #!/bin/bash #author:DYW #/etc/rc.d/rc3.d目錄下分別有多個以K開頭和以S開頭的我文件,分別讀取每個文件,以K開頭的文件輸出為文件加stop,以S開頭的文件輸出為文件名加start。 for w in `ls /etc/rc.d/rc3.d/`;do filename=`echo "$w"|cut -c1` case $filename in K) echo -e "$w\tstop" ;; S) echo -e "$w\tstart" ;; *) echo "Unknow file" ;; esac done [root@www sh.log]# bash KSfile.sh K50netconsole stop S10network start
寫一個腳本,提示輸入正整數n的值,計算1+2+3+…n的總和
[root@www sh.log]# cat sumfora-b.sh #!/bin/bash read -p "input s number please:" number num=`echo "$number"|grep "^[[:digit:]]\+$"` if echo $number | grep -q "^[[:digit:]]\+$" ;then if [ $num -eq 0 ];then echo "Please enter a number more than 0" exit fi else echo "This is a negative number" exit fi string=0 for N in `seq $number`;do sum=$[$sum+$N] string=$string+$N done echo "$string=$sum" [root@www sh.log]# bash sumfora-b.sh input s number please:10 0+1+2+3+4+5+6+7+8+9+10=55 [root@www sh.log]# bash sumfora-b.sh input s number please:0 Please enter a number more than 0 [root@www sh.log]# bash sumfora-b.sh input s number please:-10 This is a negative number
寫一個腳本,提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態
[root@www sh.log]# cat Online.sh #!/bin/bash #author:DYW #寫一個腳本,提示請輸入網絡地址,如192.168.0.0,判斷輸入的網段中主機在線狀態 read -p "please input a ip:" ip segment=`echo "$ip"|cut -d. -f1-3`. if echo "$ip" |egrep '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>' &>/dev/null;then for w in `seq 10`;do if ping -c1 -W1 $segment$w &> /dev/null;then echo -e "$segment$w\tonline" else echo -e "$segment$w\tnot online" fi done else echo "$ip is invaild" fi [root@www sh.log]# bash Online.sh please input a ip:a.a.a.a a.a.a.a is invaild [root@www sh.log]# bash Online.sh please input a ip:192.168.1.0 192.168.1.1 online 192.168.1.2 not online 192.168.1.3 not online 192.168.1.4 not online 192.168.1.5 not online 192.168.1.6 not online 192.168.1.7 not online 192.168.1.8 not online 192.168.1.9 not online 192.168.1.10 not online
打印九九乘法表
[root@www sh.log]# cat 99.sh #!/bin/bash #author:DYW #打印九九乘法表 for h in `seq 9`;do for s in `seq $h`;do echo -ne "$h*$s=$[$h*$s]\t" done echo done [root@www sh.log]# bash 99.sh 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
求100以內所有正整數之和
[root@www sh.log]# cat while100.sh #!/bin/bash #author:DYW #求100以內數字的合 laowang=1 sum=0 while [ $laowang -le 100 ];do sum=$[$laowang + $sum] let laowang++ done echo "$sum" [root@www sh.log]# bash while100.sh 5050
通過ping命令探測172.16.250.1-254范圍內的所有主機的在線狀態,統計在線主機和離線主機各多少。
[root@localhost sh.log]# cat whileonline.sh #!/bin/bash #author:DYW #通過ping命令探測網段范圍內的所有主機的在線狀態,統計在線主機和離線主機各多少 read -p "please input a ip:" ip segment=`echo "$ip"|cut -d. -f1-3`. a=0 b=0 c=0 while [ $a -le 255 ];do ping -c1 -W1 $segment$a &> /dev/null if [ $? -eq 0 ];then echo "$segment$a is active" let b++ else echo "$segment$a is inactive" let c++ fi let a++ done [root@localhost sh.log]# bash whileonline.sh please input a ip:192.168.1.1 192.168.1.0 is inactive 192.168.1.1 is active 192.168.1.2 is inactive 192.168.1.3 is inactive 192.168.1.4 is inactive ...
打印九九乘法表
[root@www sh.log]# cat while99.sh #!/bin/bash #author:DYW #打印九九乘法表 h=1 while [ $h -le 9 ];do s=1 while [ $s -le $h ];do echo -ne "$h*$s=$[$h*$s]\t" let s++ done echo let h++ done [root@www sh.log]# bash while99.sh 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
利用變量RANDOM生成10個隨機數字,輸出這個10數字,并顯示其中的最大者和最小者
[root@localhost sh.log]# cat whileRANDOM.sh #!/bin/bash #author:DYW #利用變量RANDOM生成10個隨機數字,輸出這個10數字,并顯示其中的最大者和最小者 a=1 s=$RANDOM max=$s min=$s while [ $a -le 10 ] do [ $max -lt $s ] && max=$s [ $min -gt $s ] && min=$s echo "$s" s=$RANDOM let a++ done echo "最大值$max" echo "最小值$min" [root@localhost sh.log]# bash whileRANDOM.sh 11809 31992 7297 30427 16659 1443 14497 3661 28410 6132 最大值31992 最小值1443
打印國際象棋棋盤
[root@localhost sh.log]# cat whilexiangqi.sh #!/bon/bash #author:DYW #打印國際象棋棋盤 a=1 while [ $a -le 8 ];do b=1 while [ $b -le 8 ];do sum=`expr $a + $b` c=`expr $sum % 2` [ $c -eq 0 ] && echo -ne "\033[41;1m \033[0m"||echo -ne "\033[43;1m \033[0m" let b++ done echo let a++ done [root@localhost sh.log]# bash whilexiangqi.sh
每隔3秒鐘到系統上獲取已經登錄的用戶的信息;如果發現用戶hacker登錄,則將登錄時間和主機記錄于日志/var/log/login.log中,并提示該用戶退出系統。
[root@localhost sh.log]# cat untiluser.sh #!/bin/bash #author:DYW #每隔3秒鐘到系統上獲取已經登錄的用戶的信息,如果發現用戶hacker登錄,則將登錄時間和主機記錄在日志/var/log/login.log中,并提示該用戶退出系統 until who|grep -q "^hacker\>" ;do sleep 3 done who|grep "^hacker"|tr -s " "|cut -d" " -f3,5 >> /var/log/login.log echo "you should logout system" | mail hacker echo "reminded and login record in /var/log/login.log"
隨機生成10以內的數字,實現猜字游戲,提示比較大或小,相等則退出。
[root@localhost sh.log]# cat untilcai.sh #!/bin/bash #author:DYW #隨機生成10以內的數字,實現猜字游戲,提示比較大或者小,相等則退出 read -p "please input a number:" num random=$[$RANDOM%10+1] until [ $random -eq $num ];do if [ $random -lt $num ];then echo "大了,重猜!" read -p "please input a number:" num elif [ $random -gt $num ];then echo "小了,重猜!" read -p "plesae input a number:" num fi done echo "你猜中了!" [root@localhost sh.log]# bash untilcai.sh please input a number:1 小了,重猜! plesae input a number:2 小了,重猜! plesae input a number:3 小了,重猜! plesae input a number:4 小了,重猜! plesae input a number:5 小了,重猜! plesae input a number:6 小了,重猜! plesae input a number:7 小了,重猜! plesae input a number:8 小了,重猜! plesae input a number:9 小了,重猜! plesae input a number:10 你猜中了!
原創文章,作者:DYW,如若轉載,請注明出處:http://www.www58058.com/37289
作業完成的很好,每一天都很經典,也是筆試中常見的考點,希望寫完之后能通過文字記錄下自己的思路,這樣對自己來說是一筆可貴的經驗哦。