shell100例 不会的语句 man bash查询(2)

5、批量创建10个账号,并设置随机密码
#!/bash/bin
#filename=create_user_passwd_for.sh
USER=`echo testuser{11..15}`
getent group test &> /dev/null || groupadd test &> /dev/null

for i in $USER;do
    PASSWD=`openssl rand -base64 9`
#echo user{1..10} | xargs -n1 useradd
    useradd "$i" -s /bin/bash -g test -c "$i" &> /dev/null
    #getent passwd "$i" &> /dev/null


    if [ $? -eq 0 ];then
        echo "$PASSWD" | passwd --stdin "$i" &> /dev/null
        echo "`date +%F-%T` $i:$PASSWD" >> /tmp/useradd.log;
    else
    echo "userad $i flase" >> /tmp/usererror.log
    fi

done
getent passwd |grep test
#删除新建账号
CJZ="`echo testuser{11..15}`";for i in $CJZ;do userdel -r $i;done
rm -rf /tmp/user*
groupdel test
cat /tmp/useradd.log
cat /tmp/usererror.log

[root@centos8-3 ~ ]$ man openssl
rand           Generate pseudo-random bytes.
[root@centos8-3 ~ ]$ openssl rand --help
Usage: rand [flags] num
Valid options are:
 -help               Display this summary
 -out outfile        Output file
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file
 -base64             Base64 encode output
 -hex                Hex encode output
 -engine val         Use engine, possibly a hardware device
6、创建以每天日期为文件名,并通过shell吧同一个月的放到一个文件夹
#创建以每天日期为文件名
#$(com)==`com`
mkdir -pv /date/test/
for i in `seq 365` ; do {$(touch /date`date -d "-$i day" +%F`) &> /tmp/modity_file.log}& done
#!/bin/bash
#filename=modify_file_dir.sh
FILENAME="`ls /date/test/`"
DIR=/date/test/
#echo $FILENAME
cd $DIR

for i in $FILENAME ;do
    DIRNAME="$(echo $i|cut -c1-7)"
    if [ -d /date/test/$DIRNAME ];then
        mv $i $DIRNAME
    else
        mkdir $DIRNAME
        mv $i $DIRNAME
    fi
done
7、创建一个早餐点餐shell
#!/bin/bash
cat << EOF
1) 小米粥   5元
2) 紫米粥   5元
3) 馄饨     10元
4) 肉包     4元
5) 菜包     3元
6) 油条     2元
7) 点餐完成
EOF
#filename=breakfast_menu.sh
#初始变量放到循环的前面
TOTAL=0
echo -e "\n"

for i in `seq 11`;do
read -p "请输入序号点餐: " NUM
    if [ $NUM != 7 ];then

        case $NUM in
        1)
            MONEY=5
            echo "小米粥$MONEY 元"
            ;;
        2)
            MONEY=5
            echo "紫米粥$MONEY 元"
            ;;
        3)
            MONEY=10
            echo "馄饨$MONEY 元"
            ;;
        4)
            MONEY=4
            echo "肉包$MONEY 元"
            ;;
        5)
            MONEY=3
            echo "菜包$MONEY 元"
            ;;
        6)
            MONEY=2
            echo "油条$MONEY 元"
            ;;
        *)
            echo "请输入菜单内的序号"
            ;;
        esac
        X=$MONEY
    else
        echo "总共需要$TOTAL 元"
        exit
    fi
   
    TOTAL=$[ $X+$TOTAL ]
done
8、创建一个99乘法表
#!/bin/bash

for i in $(seq 9); do
#echo -n 不回车换行
#filename=99.sh
#使用seq $i 当i等于几,第二个for循环就循环几次
    for j in `seq $i`;do
        echo -ne "${i}*${j}=$[$i*$j]\t"

    done

    echo ""
done
[root@centos8-3 /date/shell2 ]$ vi 99.sh 

#!/bin/bash

for i in $(seq 9); do

#使用seq $[10-$i] 当seq $[10-$i]等于几,第二个for循环就循环几次
    for j in `seq $[10-$i]`;do
        echo -ne "${i}*${j}=$[$i*$j]\t"

    done

    echo ""
done
9、ping10.0.0.*网段存活ip 注意变量的使用,别使用没有的变量
[root@centos8-3 /date/shell2 ]$ time `ping -w 1  10.0.0.155`   
bash: PING: command not found...
Similar command is: 'ping'

real    0m1.037s
user    0m0.024s
sys     0m0.013s
[root@centos8-3 /date/shell2 ]$ time `ping -c 1  10.0.0.155` 
bash: PING: command not found...
Similar command is: 'ping'

real    0m0.010s
user    0m0.006s
sys     0m0.004s
[root@centos8-3 /date/shell2 ]$ time `ping -c 1 -w 1 10.0.0.155`
bash: PING: command not found...
Similar command is: 'ping'

real    0m0.009s
user    0m0.005s
sys     0m0.004s
#!/bin/bash
host=10.0.0
for i in {0..254};do
#filename=check_host.sh
#    { ping -c1 -W1 $host.$i &> /dev/null && echo $host.$i is up || echo $host.$i is down; }&
    {
    if ping -c1 -W1 $host.$i &> /dev/null;then
        echo $host.$i is up
    else
        echo $host.$i is down
    fi
    }&
done
wait
#!/bin/bash
host=10.0.0
for i in {0..254};do
{ ping -c1 -W1 $host.$i &> /dev/null && echo $host.$i is up || echo $host.$i is down; }&
done
wait
#!/bin/bash

HOSTIP=10.0.0
for i in `seq 254`;do
#{  }中命令必须前后有空格  { command }
    {
        ping -c 1 -w 1 "$HOSTIP.$i" &> /dev/null;

        if [ $? -eq 0 ];then
            echo  "${HOSTIP}.${i} IP IS UP"
        else
            echo  "${HOSTIP}.${i} IP IS DOWN"
        fi
    }&

done
wait
 wait() This function waits for the first child to die. 
10、制作一个石头剪刀布的小游戏
[root@centos8-3 /date/shell2 ]$ cat Rock_Paper_Scissors.sh   
#!/bin/bash
#filename=Rock_Paper_Scissors.sh
cat << EOF
0) 石头
1) 剪刀
2) 布
EOF


read -p "输入你的选择:" NUM
#if [ $NUM -le 2 ];then
#如果使用le,当输入为字符时,会报错,使用正则则不会报错。
if [[ $NUM =~ 0|1|2 ]];then
#$NUM
case $NUM in
0)
    result="石头"
    ;;
1)
    result="剪刀"
    ;;
2)
    result="布"
    ;;
*)
    echo "请输入: 0、1、2"
    ;;
esac

REBOT=$[$RANDOM%3]
#$rebot
case $REBOT in
0)
    rebot="石头"
    ;;
1)
    rebot="剪刀"
    ;;
2)
    rebot="布"
    ;;
esac
    echo -e "\E[1;33m------------------\E[0m"
    if [ $NUM -eq 0 -a $REBOT -eq 1 -o  $NUM -eq 1 -a $REBOT -eq 2 -o $NUM -eq 2 -a $REBOT -eq 0 ];then

        echo "you choice is $result"
        echo "rebot choice is $rebot"
        echo ""
        echo -e "\E[1;32myou are win\E[0m"
    elif [ $NUM -eq $REBOT ];then
    
        echo "you choice is $result"
        echo "rebot choice is $rebot"
        echo "game draw"
    else
    
        echo "you choice is $result"
        echo "rebot choice is $rebot"
        echo ""
        echo -e "\E[1;31myou are loser\E[0m"
    fi

else
echo "请输入: 0、1、2"
exit
fi
11、统计accesslog某天访问量最大的10个ip和访问次数
#!/bin/bash

#"$ACCESSTXT" 不等于 $ACCESSTXT
#两个同级别变量,不能放到两个for循环
#filename=access_total.sh
FILE=$1
ACCESSTXT=`cat "$1" |grep -E "24\/May\/2018"|cut -d " " -f1 |sort |uniq -c |sort -rn`
ACCESSIP=`echo "$ACCESSTXT" |grep -Eo "([0-9]+\.){3}([0-9]+)"`
j=0
for i in $ACCESSIP;do
    if [ $j -le 9 ];then
        ACCESSNUM=`echo "$ACCESSTXT" |grep "$i" |tr -s " " |cut -d " " -f2`
        echo "IP IS $i ACCESS TOTAL $ACCESSNUM NUM"
    else
        exit
    fi
j=$[$j+1]
done
[root@centos8-3 /date/shell2 ]$ cat access_log |head -3
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "POST /webnoauth/model.cgi HTTP/1.1" 404 293 "http://172.18.0.1/webnoauth/model.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET /router/get_rand_key.cgi HTTP/1.1" 404 297 "http://172.18.0.1/router/get_rand_key.cgi" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
[root@centos8-3 /date/shell2 ]$ bash Statistical_traffic2.sh access_log
IP IS 172.20.116.208 ACCESS TOAL 3429 NUM
IP IS 172.20.116.228 ACCESS TOAL 2535 NUM
IP IS 172.20.0.227 ACCESS TOAL 2267 NUM
IP IS 172.20.116.179 ACCESS TOAL 2262 NUM
IP IS 172.20.65.65 ACCESS TOAL 2259 NUM
IP IS 172.20.0.200 ACCESS TOAL 1482 NUM
IP IS 172.20.116.182 ACCESS TOAL 1047 NUM
IP IS 172.20.116.191 ACCESS TOAL 800 NUM
IP IS 172.20.116.215 ACCESS TOAL 795 NUM
IP IS 172.20.0.76 ACCESS TOAL 784 NUM
上一篇:记:linux的ssh开启失败(Active: inactive (dead))


下一篇:2021-03-26