码迷,mamicode.com
首页 > 系统相关 > 详细

shell编程

时间:2018-03-20 15:54:20      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:shell

1:利用if语句,根据你输入的成绩来定制输出。

#!/bin/bash
read -p "Please input your score: " score
if [[ ! "$score" =~ ^[0-9]+$ ]];then
        echo "Your score is not interger"
        exit 1
fi
if [ $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/bash
i=0
k=0
for 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
        fi
done
if [ $i -eq 0 ];then
echo "defeat"
fi
~                 

3:利用case写一个能判断yes/no的脚本,(大小写均能识别,yes九种可能,no四种可能)、

#!/bin/bash
read -p "请输入yes|no: " q
case $q in
        [Yy][Ee][Ss]|[Yy])
                echo "yes";;
        [Nn][Oo])
                echo "no";;
        *)
                echo "请输入正确的格式"
esac

4:批量创建user1至user10,要求设置随机16位密码,包含数字、大小写字母、符号。并要求用户使用密码首次登录后,强制设置新密码。

#!/bin/bash
> /root/user.log
for 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/null
done

5:使用?,利用for,显示出一个等腰三角形。
法一:

#!/bin/bash
read -p "请输入三角形的高度: " num
for 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 "请输入行数: " h
for (( 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=0
lc=0
cc=0
bc=0
oc=0
zc=0

ls -l /var |grep -v total >/tmp/var_ftype.txt
while read lines
do
   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
   esac

done </tmp/var_ftype.txt
echo ‘-----------------------------‘ 
echo -e "var目录下普通文件数量: $zc\nvar目录下子目录数量:$dc\nvar目录下链接文件数量:$lc\nvar目录下字符类型文件数量: $cc\nvar
目录下块设备文件数量:$bc\n其他类型文件数量:$oc"
rm -f /tmp/var_ftype.txt

7、/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start

#!/bin/bash
for i in $(ls /etc/rc.d/rc3.d/);do
        if [[ $i =~ ^K.* ]];then
                echo "$i stop"
        elif [[ $i =~ ^S.* ]];then
                echo "$i start"
        fi
done

8、计算100以内所有能被3整除的整数之和
方法一:

echo {3..100..3} |tr ‘ ‘ + |bc

方法二:

#!/bin/bash
sum=0
for n in $(seq 1 100);do
        i=$[$n/3]
        y=$[3*$i]
        if [ $y -eq $n ];then
                sum=$[$n+$sum]
        fi
done  
        echo $sum

方法三:

sum=0
for i in {1..100};do
    if [ $[$i%3] -eq 0 ];then
    let sum+=$i
    fi  
done
echo $sum

9、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html

if ! [ -d /testdir ];then
  mkdir /testdir &> /dev/null
fi
for i in {1..10};do
  touch /testdir/$i`cat /dev/urandom |tr -dc [:alpha:] |head -c 8`.html
done

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
                }&
        done
done

11.打印国标象棋棋盘

#!/bin/bash
for 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/bash
while 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 5
done

13:使用一条命令,依次创建指定的用户。如执行createuser.sh tom bob alice harry
法一:

#!/bin/bash
while [ $# -gt 0 ]
do

        id $1 &>/dev/null
        if [ $? -eq 0 ];then
                echo $1 is already exsit.
                shift
                continue
        fi
        useradd $1
        echo "$1 用户创建成功"
        shift

done

法二:


#!/bin/bash
while [ $# -gt 0 ];do
        id $1 &> /dev/null && { echo $1 is alread exist && shift && continue; }
        useradd $1 && echo  $1 is created
        shift
done

14、每隔3秒钟到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/log/login.log中,并退出脚本

#!/bin/bash
while true;do
if $(ps -au |grep "^hacker.*" &> /dev/null);then
        echo $(ps -au |grep "^hacker.*" |tr -s " " |cut -d" " -f1,7,9) >> /var/log/login.log
        break
else
        sleep 3s
fi
done 

15、用文件名做为参数,统计所有参数文件的总行数

#!/bin/bash
sumhang=0
while [ $# -gt 0 ];do
        hang=$(cat $1 |wc -l)
        let sumhang+=$hang
        shift
done
echo $sumhang

16、用二个以上的数字为参数,显示其中的最大值和最小值
方法一:

#!/bin/bash
min=$1
max=$1

while [ $# -gt 0 ];do
        value=$1
        if [ $value -gt $max ];then
                max=$value
        elif [ $value -lt $min ];then
                min=$value
        fi
        shift
done
echo "min $min"
echo "max $max" 

方法二:

#!/bin/bash
if [ $# -lt 2 ];then
        echo "type more than two numbers"
        exit 1
fi

small=$(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/bash
while 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
        fi
done < /etc/passwd

18:找出分区利用率大于10%的分区,并发出警告/dev/sdaX will be full: 15%
要求使用while read来实现

法一

#!/bin/bash
df |grep /dev/sd > /app/fenqu
while 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%"
        fi
done < /app/fenqu

法二:

#!/bin/bash
> df_h
df -h | grep ^/dev/[sh]d* >> df_h
while 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/bash
df |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%"
        fi
done

19:扫描/etc/passwd文件每一行,如发现GECOS字段为空,则填充用户名和单位电话为62985600,并提示该用户的GECOS信息修改成功。

#!/bin/bash
cat /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!
        fi
done   

20:计算从1加到100的和。
法一:

#!/bin/bash
sum=0
for ((i=1;i<=100;i++));do
        let sum+=i
done
echo sum=$sum
unset sum i

sum=0
i=1

while [ $i -le 100 ];do
        let sum+=i
        let i++
done
echo sum=$sum

21:用select语句制作一个菜单。


#!/bin/bash
PS3="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;;
        esac
done

shell编程

标签:shell

原文地址:http://blog.51cto.com/13560258/2088976

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!