AI智能
改变未来

SHELL脚本编程小程序

文章目录

  • 1.鸡兔同笼
  • 2. 死亡转盘游戏
  • 3.随机颜色(一giao我嘞giao~)
  • 5.短路语句,用户是否存在以及创建
  • 6.实现运维工作菜单
  • 7.BMI身体健康
  • 8.九九乘法表(需要理清逻辑)
  • 9.半自动交互式网络配置脚本
  • 10.初始化脚本(持续完善中)
  • 11.模拟生成百分数进度
  • 11.猜数小游戏
  • 12.国际象棋

1.鸡兔同笼

#!/bin/bashHEAD=$1FOOT=$2RABBIT=(((FOOT-HEAD-HEAD)/2))CHOOK=(HEAD-RABBIT)echo \"The number of RABBIT is:${RABBIT}\"echo \"The number of CHOOK is :${CHOOK}\"

2. 死亡转盘游戏

[ [[[RANDOM%6] = 0 ] && echo “boom” || echo “you are luck dog!”

3.随机颜色(一giao我嘞giao~)

echo -e \"\\033[1;$[$RANDOM%7+31]m一giao我嘞giao~\\033[0m\"

4.检测用户panda用户是否存在,如果存在显示其信息,如果未存在则创建panda。

read -p \"Please input the username :\" NAMEINFO=\"echo `id ${NAME}`\"ifid ${NAME} &> /dev/nullthenecho -e \"\\033[1;32mThe user ${NAME} is exist,and ${NAME}\'s information:\\n${INFO}\\033[0m\"elseecho -e \"The user ${NAME} dont exist \"echo -e \"\\033[1;31mI will crate the account ${NAME},please choice \"yes\" or \"no\" \\033[0m\"read -p \"Please choice :\" YNif[[ ${YN} =~ ^([Yy]|[Ee]|[Ss])$ ]];thenuseradd ${NAME};echo \"${NAME}:123456\" |chpasswdchage -d1 ${NAME}echo \"The default password is 123456 ,you must change your passwd next login (force)\"elseecho -e \"\\033[1;33mwill leave and exit!!!\\033[0m\"fifi

5.短路语句,用户是否存在以及创建

NAME=pandaid $NAME &> /dev/null &&  echo \"the account is exist\" || echo\"the account dont exist\";

6.实现运维工作菜单

echo -en \"\\033[1;$[$RANDOM%7+31]m\"cat<<EOFPlease choice:1 DB bakup2 Clean log3 Soft update4 Soft goback5 Damage foreverEOFecho -en \"\\033[0m\"read -p \"Please select the corresponding number above \" MENU[ ${MENU} -eq 1 ] &&[ ${MENU} -eq 2 ] &&[ ${MENU} -eq 3 ] &&[ ${MENU} -eq 4 ] &&[ ${MENU} -eq 5 ] && echo \"just a joke!\"

知识点:条件测试与短路

7.BMI身体健康

read -p \"Please input your HIGHT (the unit:meter range:0<HIGHT&<3:eg:1.81):\" HIGHT#键入数值正确性检测:if[[ ! ${HIGHT} =~ ^[0-2](\\.[0-9]{1,2})$  ]]thenecho \"please input right number\"firead -p \"Please input your HIGHT (the unit:KG range:0<WEIGHT&<250:eg:70):\" WEIGHTif[[ ! ${WEIGHT} =~ ^[0-9][0-9]|1[0-9][0-9]|2[0-5][0-9] ]]#       [[ ! ${WEIGHT} =~ ^[0-9]{1,3}$  ]]thenecho \"please input right number\"fiBMI=`echo ${WEIGHT}/${HIGHT}^2 |bc`if[[ ${BMI} -le 18 ]]#-le 小于等于thenecho \"you are so light\"elif[[ ${BMI} -lt 24 ]]thenecho \" you are health\"elseecho \"you are so fat!\"fi

8.九九乘法表(需要理清逻辑)

观察结构:

1x1=11x2=2	2x2=41x3=3	2x3=6	3x3=91x4=4	2x4=8	3x4=12	4x4=161x5=5	2x5=10	3x5=15	4x5=20	5x5=251x6=6	2x6=12	3x6=18	4x6=24	5x6=30	6x6=361x7=7	2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=491x8=8	2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=641x9=9	2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81

解析:
每个乘法第二个数为行数(即i行)
每个乘法第一个数是从1到行数(即1…i)(seq 可以解决,二次循环方法也可解决)
语法一:利用echo `seq i` 打印每个乘法第一个数

for i in {1..9}dofor j in `seq $i`doecho -e \"${j}x${i}=$[j*i]\\t\\c\"doneechodone

语法二:利用二次循环循环出每个乘法第一个数

for((i=1;i<10;i++))doforfor((j=1;j<=i))doecho -e \"${j}x${i}=$((j*i))\\t\\c\"doneechodone

9.半自动交互式网络配置脚本

echo -e \"\\033[1;31m will change the inet name as eth0\\033[0m\"sed -i.bak \'/^.*quiet\"$/s/quiet\"/quiet net.ifnames=0\"/\' /etc/default/grubgrub2-mkconfig -o /boot/grub2/grub.cfgmv /etc/sysconfig/network-scripts/ifcfg-ens* /etc/sysconfig/network-scripts/ifcfg-eth0 || touch /etc/sysconfig/network-scripts/ifcfg-eth0echo \'\' >/etc/sysconfig/network-scripts/ifcfg-eth0read -p \"Do you set the static or dhcp inet?(please input static or dhcp)\" SDSDd=`echo ${SD} |tr \'A-Z\' \'a-z\'`if[[ ${SDd} =~ [static]|[stati]|[stat]|[sta]|[st]|[s] ]];thenecho -e \"\\033[1;31myou will set the static inet \\033[0m\"read -p \"Please input the Ethernrt name:\" NAMEread -p \"Please input the DEVICE name:\" DEVICEread -p \"Please input the IPADDE :\" IPADDRread -p \"Please input the GATEWAY:\" GATEWAYread -p \"Please input the NETMASK:\" NETMASKread -p \"Please input the DNS1:\" DNSONEread -p \"Please input the DNS2:\" DNSTWOecho \"NAME=${NAME}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"DEVICE=${DEVICE}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"IPADDR=${IPADDR}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"GATEWAY=${GATEWAY}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"NETMASK=${NETMASK}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"DNS1=${DNSONE}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"DNS2=${DNSTWO}\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"BOOTPROTO=static\" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo \"ONBOOT=yes\" >> /etc/sysconfig/network-scripts/ifcfg-eth0elseecho -e \"\\033[1;31mDo you want to set dhcp inet?\\033[0m\"read -p \"Please select yes or no?\"YNYNy=`echo ${YN} |tr \'A-Z\' \'a-z\'`if[ ${YNy} == [yes] ];thenecho \"Will set the default dhcp set\"echo \'TYPE=\"Ethernet\"PROXY_METHOD=\"none\"BROWSER_ONLY=\"no\"BOOTPROTO=\"dhcp\"DEFROUTE=\"yes\"NAME=\"eth0\"DEVICE=\"eth0\"ONBOOT=\"yes\"BOOTPROTO=\"yes\"\' > /etc/sysconfig/network-scripts/ifcfg-eth0elseecho \"dont set dhcp inet,will leave\"fifiecho -e \"\\033[1;31mnow, restart the network service!\\033[0m\"systemctl restart network || nmcli c reload ;nmcli c up eth0;nmcli d reapply eth0;nmcli d connect eth0echo -e \"\\033[1;32mDONE!\\033[0m\"

10.初始化脚本(持续完善中)

11.模拟生成百分数进度

for((i=1;i<=100;++i))doprintf \"\\e[4D%3d%%\" $isleep 0.1sdone

11.猜数小游戏

echo \"welcome to the guess number games\"NUM=$[$RANDOM%10]while  read -p \"please input the number you guess\" INPUT  ;doif [ ${INPUT} == ${NUM} ] ;thenecho \"you are right!\"breakelif[ ${INPUT} -lt ${NUM} ] ;thenecho \"the number is too less \"elseecho \"the number is too great\"fidone

12.国际象棋

for((i=1;i<=8;i++));dofor((j=1;j<=8;j++));dolet sum=(\"$i\"+\"$j\")%2if [ \"${sum}\" -eq 1 ];thenecho -ne \"\\033[1;43m  \\033[0m\"elseecho -ne \"\\033[1;47m  \\033[0m\"fidoneechodone
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » SHELL脚本编程小程序