判断/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.178linux.com/37289
评论列表(1条)
作业完成的很好,每一天都很经典,也是笔试中常见的考点,希望写完之后能通过文字记录下自己的思路,这样对自己来说是一笔可贵的经验哦。