本文共 8649 字,大约阅读时间需要 28 分钟。
1:利用if语句,根据你输入的成绩来定制输出。
#!/bin/bashread -p "Please input your score: " scoreif [[ ! "$score" =~ ^[0-9]+$ ]];then echo "Your score is not interger" exit 1fiif [ $score -lt 60 ];then echo "You need study hard"elif [ $score -ge 60 -a $score -lt 80 ];then echo "You score is just soso"elif [ $score -ge 80 -a $score -le 100 ];then echo "You score is very good"else echo "You score is invalid"fi
2:写一个×××系统,判断5个数字。
1 5 3 8 9 输入 第一个值。输入 第二个值。输入 第三个值。输入 第四个值。输入 第五个值。每一步在输入完,都会判断,如果中了,则有输出结果:第一次中输出 : First blood第二次中输出 : Double kill第三次中输出 :Triple kill第四次中输出 :Quadra Kill第五次中输出 :Penta Kill 以及 ACE!一次也不中输出: Defeat!#!/bin/bashi=0k=0for n in {1..5}do ran=$[RANDOM%5] read -p "第$n次输入数字,好吗: " number if [ $number -eq $ran ];then i=$[$i+1] k=1 else echo "你错了" fi if [ $k -eq 1 ];then case $i in 1) echo "first blood";; 2) echo "double kill";; 3) echo "trible kill";; 4) echo "quadra kill";; 5) echo "penta kill ACE";; esac fidoneif [ $i -eq 0 ];thenecho "defeat"fi~
3:利用case写一个能判断yes/no的脚本,(大小写均能识别,yes九种可能,no四种可能)、
#!/bin/bashread -p "请输入yes|no: " qcase $q in [Yy][Ee][Ss]|[Yy]) echo "yes";; [Nn][Oo]) echo "no";; *) echo "请输入正确的格式"esac
4:批量创建user1至user10,要求设置随机16位密码,包含数字、大小写字母、符号。并要求用户使用密码首次登录后,强制设置新密码。
#!/bin/bash> /root/user.logfor i in {1..10};do useradd user$i && echo user$i is created password=$(cat /dev/urandom |tr -dc '0-9a-zA-Z!@_#?.,' |head -c 16) echo user$i:$password >> /root/user.log echo $password |passwd --stdin user$i &> /dev/null passwd -e user$i &> /dev/nulldone
5:使用♥,利用for,显示出一个等腰三角形。
法一:#!/bin/bashread -p "请输入三角形的高度: " numfor i in `seq $num`;do for x in `seq 1 $[$num-$i]`;do echo -e " \c" done for n in `seq 1 $[2*$i-1]`;do #yanse=`tr -dc '1-6' < /dev/urandom | head -c 1` #echo -e "\e[3"$yanse"m♥\e[0m\c" echo -e "\e[34m♥\e[0m\c" done echo done
法二:
#!/bin/bash read -p "请输入行数: " hfor (( i=1;i<=$h;i=$[$i+1] ))do for (( j=$[$h-$i];j>0;j-- )) do echo -ne ' ' done for (( k=1;k<=$[2*$i-1];k++ )) do echo -n "♥" done echo done~
6:判断/var/目录下所有文件的类型
dc=0lc=0cc=0bc=0oc=0zc=0ls -l /var |grep -v total >/tmp/var_ftype.txtwhile read linesdo ftype=`echo $lines |awk '{print $1}' |cut -c 1` case $ftype in d) dname=`echo $lines |awk '{print $9}'`; echo "$dname is a Directory" ; let dc+=1;; l) lname=`echo $lines |awk '{print $9}'`; echo "$lname is a Links of Soft " ;let lc+=1 ;; c) cname=`echo $lines |awk '{print $9}'`; echo "$cname is a Character of file" ;let cc+=1;; b) bname=`echo $lines |awk '{print $9}'`; echo "$bname is a Block file" ; let bc+=1;; -) zname=`echo $lines |awk '{print $9}'`; echo "$zname is a common file" ; let zc+=1;; *) echo "Others files"; let oc+=1 esacdone
7、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
#!/bin/bashfor i in $(ls /etc/rc.d/rc3.d/);do if [[ $i =~ ^K.* ]];then echo "$i stop" elif [[ $i =~ ^S.* ]];then echo "$i start" fidone
8、计算100以内所有能被3整除的整数之和
方法一:echo {3..100..3} |tr ' ' + |bc
方法二:
#!/bin/bashsum=0for n in $(seq 1 100);do i=$[$n/3] y=$[3*$i] if [ $y -eq $n ];then sum=$[$n+$sum] fidone echo $sum
方法三:
sum=0for i in {1..100};do if [ $[$i%3] -eq 0 ];then let sum+=$i fi doneecho $sum
9、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
if ! [ -d /testdir ];then mkdir /testdir &> /dev/nullfifor i in {1..10};do touch /testdir/$i`cat /dev/urandom |tr -dc [:alpha:] |head -c 8`.htmldone
10、探测局域网内的主机
172.18.250-254.X能ping通显示并保存至/root/ip.log要求并行探测提高效率。/root/bin/ip.log
for i in {250..255};do for n in {1..255};do { ping -c1 -w1 172.18."$i"."$n" &>/dev/null if [ $? -eq 0 ];then echo 172.18."$i"."$n" host is up |tee -a /root/bin/ip.log fi }& donedone
11.打印国标象棋棋盘
#!/bin/bashfor xline in $(seq 1 8);do for yline in $(seq 1 8);do if [ $[$[$xline+$yline]%2] -eq 0 ];then echo -e "\033[47m \033[0m\c" else echo -e "\033[41m \033[0m\c" fi done echo done
12:编写一个自动恢复httpd进程的脚本
#!/bin/bashwhile true;do if killall -0 httpd &> /dev/null;then : else systemctl restart httpd &> /dev/null echo `date "+%F %T"` restart httpd >> /app/httpd.log fi sleep 5done
13:使用一条命令,依次创建指定的用户。如执行createuser.sh tom bob alice harry
法一:#!/bin/bashwhile [ $# -gt 0 ]do id $1 &>/dev/null if [ $? -eq 0 ];then echo $1 is already exsit. shift continue fi useradd $1 echo "$1 用户创建成功" shiftdone
法二:
#!/bin/bashwhile [ $# -gt 0 ];do id $1 &> /dev/null && { echo $1 is alread exist && shift && continue; } useradd $1 && echo $1 is created shiftdone
14、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本
#!/bin/bashwhile true;doif $(ps -au |grep "^hacker.*" &> /dev/null);then echo $(ps -au |grep "^hacker.*" |tr -s " " |cut -d" " -f1,7,9) >> /var/log/login.log breakelse sleep 3sfidone
15、用文件名做为参数,统计所有参数文件的总行数
#!/bin/bashsumhang=0while [ $# -gt 0 ];do hang=$(cat $1 |wc -l) let sumhang+=$hang shiftdoneecho $sumhang
16、用二个以上的数字为参数,显示其中的最大值和最小值
方法一:#!/bin/bashmin=$1max=$1while [ $# -gt 0 ];do value=$1 if [ $value -gt $max ];then max=$value elif [ $value -lt $min ];then min=$value fi shiftdoneecho "min $min"echo "max $max"
方法二:
#!/bin/bashif [ $# -lt 2 ];then echo "type more than two numbers" exit 1fismall=$(echo $* |xargs -n1 |sort -n |head -n 1)big=$(echo $* |xargs -n1 |sort -n |tail -n 1)echo "Maximum number is $big"echo "Minimum number is $small"
17:显示UID小于1000的为sys user,大于等于1000的为comm user。
#!/bin/bashwhile read userline;do userid=$(echo $userline |cut -d: -f3) username=$(echo $userline |cut -d: -f1) if (( $userid < 1000 )) ;then echo $username is a sys user else echo $username is a comm user fidone < /etc/passwd
18:找出分区利用率大于10%的分区,并发出警告/dev/sdaX will be full: 15%
要求使用while read来实现法一
#!/bin/bashdf |grep /dev/sd > /app/fenquwhile read line;do used=$(echo $line |tr -s " " % |cut -d% -f5) name=$(echo $line |cut -d" " -f1) if (( $used > 10 ));then echo "$name will be full:$used%" fidone < /app/fenqu
法二:
#!/bin/bash> df_hdf -h | grep ^/dev/[sh]d* >> df_hwhile read disk;do a=`echo $disk |grep -o "\<[0-9]\{0,3\}%" | cut -d% -f1` b=`echo $disk |grep -o "^[^[:space:]]\+\>"` if [ $a -gt 10 ];then echo "$b will be full: $a%" fi done < df_h
法三:
#!/bin/bashdf |grep /dev/sd |while read disk;do diskused=$(echo $disk |sed -r 's/.* ([0-9]+)%.*/\1/') diskname=$(echo $disk |sed -r 's@(/dev/sd[[:lower:]][[:digit:]]).*@\1@') if (( $diskused > 10 ));then echo "$diskname will be full:$diskused%" fidone
19:扫描/etc/passwd文件每一行,如发现GECOS字段为空,则填充用户名和单位电话为62985600,并提示该用户的GECOS信息修改成功。
#!/bin/bashcat /etc/passwd |while read userline;do if [ -n "$(echo $userline |cut -d: -f5)" ];then continue else username=$(echo $userline|cut -d: -f1) usermod -c $username-62985600 $username echo $username\'s gecos already changed! fidone
20:计算从1加到100的和。
法一:#!/bin/bashsum=0for ((i=1;i<=100;i++));do let sum+=idoneecho sum=$sumunset sum isum=0i=1while [ $i -le 100 ];do let sum+=i let i++doneecho sum=$sum
21:用select语句制作一个菜单。
#!/bin/bashPS3="Please choose your menu: "select menu in huangmenji huimian hulatang roujiamo;do case $REPLY in 1) echo "The price is \$15";; 2) echo "The price is \$10";; 3) echo "The price is \$8";; 4) echo "The price is \$6";; *) echo "get out";break;; esacdone
转载于:https://blog.51cto.com/13560258/2088976