Linux | 写过的一些shell脚本

前言:

这些年写过的一些没啥用的shell脚本汇集

正文:

1.石头剪刀布 | if、case、数组

#v1_case版
#!/bin/bash
x=$[RANDOM%3]
#石头=0
#剪刀=1
#布=2
read -p "输入石头|剪刀|布:" y

cecho(){
    echo -e "\033[$1m$2\033[0m"
}

if [ $x -eq 0 ];then
    cecho 32 "石头"
elif [ $x -eq 1 ];then
    cecho 33 "剪刀"
elif [ $x -eq 2 ];then
    cecho 34 "布"
fi

case $y in
    "石头")
        if [ $x -eq 0 ];then
            cecho 41 "平了"
        elif [ $x -eq 1 ];then
            cecho 41 "赢了"
        elif [ $x -eq 2 ];then
            cecho 41 "输了"
        fi;;
    "剪刀")
        if [ $x -eq 0 ];then
            cecho 41 "输了"
        elif [ ${x} -eq 1 ];then
            cecho 41 "平了"
        elif [ ${x} -eq 2 ];then
            cecho 41 "赢了"
        fi;;
    "布")
        if [ $x -eq 0 ];then
            cecho 41 "赢了"
        elif [ $x -eq 1 ];then
            cecho 41 "输了"
        elif [ $x -eq 2 ];then
            cecho 41 "平了"
        fi
esac
####################
#v2_if+数组版_现在看好像还挺牛逼的
#!/bin/bash
x=$[RANDOM%3]
game=(石头 剪刀 布)
echo "0 = 石头"
echo "1 = 剪刀"
echo "2 = 布"
read -p "输入相应数值猜拳:" n
echo "MAN : ${game[$n]}"
echo "PC : ${game[$x]}"
if [ $x == $n ];then
    echo "NO WIN~"
elif [ $[$x-$n] -eq -1 ];then
    echo "PC WIN !"
elif [ $[$x-$n] -eq 1 ];then
    echo "MAN WIN !"
elif [ $[$x-$n] -eq -2 ];then
    echo "MAN WIN !"
elif [ $[$x-$n] -eq 2 ];then
    echo "PC WIN !"
else
    echo "Wrong Value !!"
fi

2.数字排序 | if

#!/bin/bash
echo "输入3个数进行排序:"
read -p "输入第1个整数:" num1
read -p "输入第2个整数:" num2
read -p "输入第3个整数:" num3
tmp=0
if [ $num1 -gt $num2 ];then
     tmp=$num1
     num1=$num2
     num2=$tmp
fi
if [ $num2 -gt $num3 ];then
    tmp=$num2
    num2=$num3
    num3=$tmp
fi
if [ $num1 -gt $num2 ];then
     tmp=$num1
     num1=$num2
     num2=$tmp
fi
echo $num1 $num2 $num3

3.局域网ip扫描 | for、if

#!/bin/bash
for ip in {1..254}
do
    ping -c2 -i0.3 -W1 192.168.4.$ip &>/dev/null
    if [ $? -eq 0 ];then
        echo "192.168.4.$ip is up"
    else
        echo "192.168.4.$ip is down"
    fi
done

4.打印乘法口诀表 | for

#!/bin/bash
#for i in {41..47}
#do
#    echo -e "\033[${i}m" $i "\033[0m"
#done
#上面测试打印颜色
for i in `seq 9`
do
    for j in `seq $i`
    do
        echo -ne "\033[0m${i}*$j=$[i*j]\033[0m "
    done
    echo
done

本文链接:

https://blog.zhigu34.cn/archives/32.html
1 + 5 =
快来做第一个评论的人吧~