文章目录

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

1.鸡兔同笼

#!/bin/bash
HEAD=$1
FOOT=$2
RABBIT=(((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 :" NAME
INFO="echo `id ${NAME}`"
ifid ${NAME} &> /dev/null
thenecho -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"fi
fi

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

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

6.实现运维工作菜单

#!/bin/bash
VERSION=`rpm -q centos-release |grep -o '[[:digit:]]' |head -c1 `
dir1=/etc/sysconfig/network-scripts/
dir2=/etc/yum.repos.d/
function ETHERNET (){[[ ! -e ${dir1}ifcfg-eth0 ]] && touch ${dir1}ifcfg-eth0 &>/dev/nullarm -rf ${dir1}ifcfg-enss* &>/dev/nullread -p "please input the ip:(xxx.xxx.xxx.xxx) " IP
cat>${dir1}ifcfg-eth0 <<EOF
NAME=eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=${IP}
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=180.76.76.76
DNS2=223.6.6.6
EOFif [ ${VERSION} == 6 ];then
service network restartelif [ ${VERSION} == 7 ];thensed -i.bak '/^.*quiet"$/s/quiet"/quiet net.ifnames=0"/' /etc/default/grubgrub2-mkconfig -o /boot/grub2/grub.cfgsystemctl  restart networkelif [ ${VERSION} == 8 ];thensed -i.bak '/^.*quiet"$/s/quiet"/quiet net.ifnames=0"/' /etc/default/grubgrub2-mkconfig -o /boot/grub2/grub.cfgsystemctl restart network || nmcli c reload ;nmcli c up eth0;nmcli d reapply eth0;nmcli d connect eth0elseecho unknow version!!!
fi
}function YUM () {yum -y install autofs service restart autofs || systemctl restart autofschkconfig autofs on || systemctl enable --now autofsmv ${dir2}CentOS-Base.repo ${dir2}CentOS-Base.repo.baktouch ${dir2}CentOS-Base.repoif [ ${VERSION} == 6 ];thencat>>${dir2}CentOS-Base.repo<<EOF
[base]
name=CentOS-\$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=file:///misc/cdhttp://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/http://mirrors.aliyuncs.com/centos/\$releasever/os/\$basearch/http://mirrors.cloud.aliyuncs.com/centos/\$releasever/os/\$basearch/
gpgcheck=0
enabled=1[extras]
name=CentOS-\$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/http://mirrors.aliyuncs.com/centos/\$releasever/extras/\$basearch/http://mirrors.cloud.aliyuncs.com/centos/\$releasever/extras/\$basearch/
gpgcheck=0
enabled=1[epel]
name=Extra Packages for Enterprise Linux 6 - \$basearch
baseurl=http://mirrors.aliyun.com/epel/6/\$basearch
enabled=1
gpgcheck=0EOFyum clean allyum makecache        elif [ ${VERSION} == 7 ];then
cat>>${dir2}CentOS-Base.repo<<EOF
[base]
name=CentOS-\$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=file:///misc/cdhttp://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/http://mirrors.aliyuncs.com/centos/\$releasever/os/\$basearch/http://mirrors.cloud.aliyuncs.com/centos/\$releasever/os/\$basearch/
gpgcheck=0[extras]
name=CentOS-\$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/http://mirrors.aliyuncs.com/centos/\$releasever/extras/\$basearch/http://mirrors.cloud.aliyuncs.com/centos/\$releasever/extras/\$basearch/
gpgcheck=0
enabled=1[epel]
name=Extra Packages for Enterprise Linux 7 - \$basearch
baseurl=http://mirrors.aliyun.com/epel/7/\$basearch
failovermethod=priority
enabled=1
gpgcheck=0EOFyum clean allyum makecacheelif [ ${VERSION} == 8 ];then
mkdir ${dir2}repo.bak
mv ${dir2}* ${dir2}repo.bak
[ ! -e /etc/yum.repos.d/epel.repo  ]  || touch /etc/yum.repos.d/epel.repo
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*cat>>${dir2}CentOS-Base.repo<<EOF
[base]
name=CentOS-\$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=file:///misc/cdhttps://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/http://mirrors.aliyuncs.com/centos/\$releasever/BaseOS/\$basearch/os/http://mirrors.cloud.aliyuncs.com/centos/\$releasever/BaseOS/\$basearch/os/
gpgcheck=0
enabled=1[extras]
name=CentOS-\$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/http://mirrors.aliyuncs.com/centos/\$releasever/extras/\$basearch/os/http://mirrors.cloud.aliyuncs.com/centos/\$releasever/extras/\$basearch/os/
gpgcheck=0
enbaled=1EOFyum clean allyum makecacheelseecho unknow version!!!fi}function FIREWALLD () {if [ ${VERSION} == 6 ];thensed -i.bak  -rn '/^S.*g$/s/enforcing/disabled/p' /etc/selinux/config chkconfig iptables offelif [ ${VERSION} == 7 ];thensed -i.bak  -rn '/^S.*g$/s/enforcing/disabled/p' /etc/selinux/config systemctl disable --now firewalld || chkconfig iptables offelif [ ${VERSION} == 8 ];thensed -i.bak  -rn '/^S.*g$/s/enforcing/disabled/p' /etc/selinux/config systemctl disable --now firewalldelseecho unknow version!!!fi
}function SOFTWARE () {yum -y install vim tree net-tools  &> /dev/nullyum -y install pstree  &> /dev/null
}function ALIAS () {alias rm="DIR=/data/backup/bak.`date +%F_%T`;mkdir -p $DIR;mv -t $DIR"alias cdif="cd /etc/sysconfig/network-scripts/"}function ENV () {echo 'PS1="\[\e[1;36m\][\[\e[0m\]\t \[\e[1;32m\]\u\[\e[31m\]@\h\[\e[1;37m\] \W\[\e[1;35m\]]\[\e[0m\]\\$"
HISTTIMEFORMAT="%F %T " ' >> /etc/profile.d/env.shecho HISTTIMEFORMAT="%F %T " >>/etc/profile.d/env.shbash /etc/profile.d/env.sh
}function VIM () {cat>>~/.vimrc<<EOF
autocmd BufNewFile *.sh exec ":call SetTitle()"
func SetTitle()if expand("%:e") == 'sh'call setline(1,"#!/bin/bash")call setline(2,"#****************************************************")call setline(3,"#Author:        阿俊(panda)")call setline(4,"#QQ:            949786521")call setline(5,"#Date:          ")call setline(6,"#Version:")call setline(7,"#Discription:")call setline(8,'#Color--help 基本格式 开始:\033[1;32m 结束:\033[0m')call setline(9,"#30黑,31红,32绿,33黄,34蓝,35紫红,36青蓝,37白")call setline(10,"#****************************************************")endif
endfunc
EOF
}function MAIL () {yum -y install mailx
cat>>/etc/mail.rc<<EOF
set form=949786521@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=949876521@qq.com
set smtp-auth-password=hicoeykrtvhrbehg
set smtp-auth=login
EOF
}ETHERNET
YUM
FIREWALLD
SOFTWARE
ALIAS
ENV
VIM
MAILwait

知识点:条件测试与短路

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=1
1x2=2  2x2=4
1x3=3  2x3=6  3x3=9
1x4=4  2x4=8  3x4=12 4x4=16
1x5=5  2x5=10 3x5=15 4x5=20 5x5=25
1x6=6  2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7  2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8  2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=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`do      echo -e "${j}x${i}=$[j*i]\t\c"done    echo
done

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

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

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/grub
grub2-mkconfig -o /boot/grub2/grub.cfg
mv /etc/sysconfig/network-scripts/ifcfg-ens* /etc/sysconfig/network-scripts/ifcfg-eth0 || touch /etc/sysconfig/network-scripts/ifcfg-
eth0
echo '' >/etc/sysconfig/network-scripts/ifcfg-eth0
read -p "Do you set the static or dhcp inet?(please input static or dhcp)" SD
SDd=`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-eth0 echo "DNS2=${DNSTWO}" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
elseecho -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"fi
fi
echo -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 eth0
echo -e "\033[1;32mDONE!\033[0m"

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

11.模拟生成百分数进度

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

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"fi
done

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"fidoneecho
done

SHELL脚本编程小程序相关推荐

  1. linux shell编程小程序6,shell脚本应用小程序

    1...100的求和 非递归: #!/bin/bash read num count=1 sum=0 val="" ret="" while [ $count  ...

  2. linux脚本程序是什么意思,什么是shell脚本编程?

    什么是Shell脚本 示例: #!/bin/sh cd~ mkdirshell_tut cdshell_tut for((i=0;i<10;i++));do touchtest_$i.txt d ...

  3. Linux shell脚本编程(一)

    -------------------------------------------------------------------- 注:如果你对python感兴趣,我这有个学习Python基地, ...

  4. 《Linux命令行与shell脚本编程大全 第3版》

    第一部分 Linux 命令行 第1章  初识Linux she 1.1   什么是Linux 2 1.1.1 深入探究Linux 内核 3 1.1.2 GNU 工具 6 1.1.3 Linux 桌面环 ...

  5. 《Linux命令行与shell脚本编程大全》(第三版)读书笔记

    第一部分 Linux命令行 第三章.基本的bash shell命令 bash手册 man 命令 例子: man cat 空格翻页.回车下一行.左右键看右侧(左侧)内容.q退出 info info in ...

  6. 【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-汇总 ( Chapter1-Chapter16 )

    一.初识 Linux shell Linux 系统可划分为以下 4 部分. Linux 内核 GNU 工具 图形化桌面环境 应用软件 内核主要负责以下 4 种功能. 系统内存管理 内核通过硬盘上称为交 ...

  7. shell脚本编程知识汇总(一)

    原文: http://www.cnblogs.com/ebusi/archive/2011/02/09/1950422.html Shell脚本编程的常识 (这些往往是经常用到,但是各种网络上的材料都 ...

  8. linux 命令行与shell脚本编程大全

    linux 命令行与shell脚本编程大全 第一章 Linux LiveCD就是从cd读取的系统,由于没法将数据写入到cd,所以一旦重启,之前操作过后的一切数据都会丢失. 第二章 第三章 1.man手 ...

  9. linux之基础shell脚本编程1 基础变量赋值

    本章主要介绍函数的基础,变量赋值,逻辑运算,条件表达式,测试 linux之基础shell脚本编程1  基础变量赋值 linux之基础shell脚本编程2 if语句循环判断 linux之基础shell脚 ...

最新文章

  1. [自定义区间-Range]书里的例子 - 中文数字类
  2. 2高并发服务器:多线程服务器
  3. Windows Embedded从入门到精通4月预告
  4. C++学习之路 | PTA乙级—— 1047 编程团体赛 (20 分)(精简)
  5. Javascript——声明提升(函数、变量提升)
  6. mysql e 变量_mysql 变量简介
  7. Java中break、continue及标签等跳转语句的使用[下]
  8. Nacos 配置实时更新原理分析
  9. 【NLP】揭秘马尔可夫模型神秘面纱系列文章(四)
  10. mac pro M1(ARM)安装:VMWare Fusion及linux(centos7/ubuntu)(一)
  11. 104键键盘 按键码表
  12. oracle 等待,必看干货 | Oracle常见的等待事件说明(下)
  13. 使用XPlanner进行敏捷项目计划和进度跟踪管理
  14. 海洋cms 模板标签手册
  15. 【笨木头Cocos2dx 038】战争迷雾效果 第01章_要探索,不要地图全开!
  16. 娑罗双树,半枯半荣,娑罗花开,盛者必衰
  17. linux公社_如何在Linux中安装和使用dig和nslookup命令
  18. LOJ10064黑暗城堡
  19. H5页面rem兼容适配及华为样式调整
  20. 川土微电子CA-IS1300用于电流检测的隔离运放新品上市

热门文章

  1. Xilinx FPGA平台GTX简易使用教程(一)GTX基础知识
  2. CPU/GPU/GPGPU简介
  3. 计算机教学楼起名,学校教学楼起名(文雅的教学楼取名)
  4. Web---HTML标签总结
  5. RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR
  6. 知物由学 | 易盾SaaS系统资损防控体系建设
  7. 淘宝可以传照片搜索商品,verygood.雅客VC多味水果糖
  8. 0环权限高还是3环_环0到环3
  9. esp8266启动报错 ets Jan 8 2013,rst cause:2, boot mode:(7,7)
  10. 小程序生成统一支付prepay_id相关配置和类介绍