列表:
1, 新装Centos系统环境快速部署
2, 查看linux系统各项系统信息
3, 统计文件空行总数
4, 检测硬盘利用率,如果超过百分之17就报警,并发送wall
5, 添加一个用户
6, 文件类型判断
7, 判断某ip是否在线
8, 显示两个文件空行数的和
9, 选择yes or no
10,批量改某路径下的文件名后缀(更改后的文件名粉色显示)
10,CentOS7 系统中 /data/路径下有大量的 jpg 小图片文件, 文件名称都是以 4 位小写字符加 4 位数字构成的,请编写一个 shell 或者 python 脚本将文件前四个小写字符相同的文件移动到以这四个字母为名字的文件夹中
11,求1-100的和(for)= seq -s + 100 | bc =echo {1..100} | tr " " + |bc
12,批量创建用户and批量删除用户(可以合成一个脚本,$0 create|del)
13,并发扫描多网段IP是否在线
14,小星星之一闪一闪亮晶晶
15,九九乘法表(正反)
16,打印等腰三角形
17,猜数字(1-10)
18,创建用户(shift)
19,监控http服务运行状况(while)
20,根据访问lastb,禁止10次以上ip访问(while)
21,磁盘利用率报警(while read)
22,点菜(select menu in ...)-可用于多脚本合成
23,比较两个参数的大小与阶乘(函数调用脚本)
24,fork bomb(自调用)
25,10个随机数取最大值最小值(for)
26,10个随机数取最大值最小值(数组)
27,10个随机数冒泡排序(数组)
28,编写脚本id.sh,计算/etc/passwd⽂件中的第5个⽤户和第9个⽤户和第15⽤户的ID之和。(eval)
29,创建一位数字加两个随机字母.html 如: 1Ab.html
30,破解已知md5sum加密字符串对应的RANDOM值
31,CA自签名证书,申请证书和颁发证书
32,SSH批量基于Key验证传送公钥至多台主机
1,新装Centos系统环境快速部署(基于两块网卡,内卡和外卡,否则YUM源检测会卡住):注意事项,虚拟机额里两块网卡会出现故障,重启后会造成ssh无法连接,手动重启网络才可以。可能是虚拟的两块网卡,另一个块没有MAC地址?
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-03-20#FileName: My_Work_Environment.sh#URL: https://blog.51cto.com/5033330 ##Desciption: This script can quick set your work environment(Centos6,7 or other) ~/.vimrc## When you use this Shell Script,Please make sure your ~/.bashrc not set any alias # or other,this script will overwrite ~/.bashrc## login user is root. ## if your system ver is 7,this script will create /etc/profile.d/environment.sh , # set the PS1 style,the HISTSIZE=2000,LANG=EN,add /data/script36 to PATH ,rename ens33 # to eth0.and some alias# if your system ver is 6,just like 7 but excetp rename## Login user is common user# just set some alias and set PS1 style.## After run , source ./etc/profile.d/environment.sh or ~/.bashrc the PS1 style will change.# #Copyright(c) 2019 All rights reserved#---------------------------------------#set -e#set -urelease7=7release6=6Start_Color="e[1;31m"End_Color="e[0m"Sysver=`cat /etc/redhat-release | grep -o "[1-9]+"| head -1`IPADDR7='IPADDR=172.16.36.102'IPADDR6='IPADDR=172.16.36.103'NETMASK='NETMASK=255.255.255.0'GATEWAY='GATEWAY=172.16.36.1'PackageName="gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree screen lsof tcpdump wget ntpdate net-tools iotop bc zip unzip nfs-utils apr-devel apr-util-devel"DIR="/data"cat ~/.vimrc EOFset ts=4set pasteset cursorlineset softtabstop=4set expandtabset shiftwidth=4set autoindentautocmd BufNewFile *.sh exec ":call SetTitle()"func SetTitle() if expand("%:e") == 'sh' call setline(1,"#!/bin/bash") call setline(2,"#") call setline(3,"#---------------------------------------") call setline(4,"#Author: MartinHe") call setline(5,"#Date: ".strftime("%Y-%m-%d")) call setline(6,"#FileName: ".expand("%")) call setline(7,"#URL: https://blog.51cto.com/5033330 ") call setline(8,"#Desciption: The test Scirpt") call setline(9,"#Copyright(c) ".strftime("%Y")." All rights reserved") call setline(10,"#---------------------------------------") call setline(11,"") endifendfuncautocmd BufNewFile * normal GEOFif [ ! -e /$DIR/script36 ];then mkdir /$DIR/script36 fiif [ `id -u` = 0 ];then if [ "$Sysver" = "7" ];thencat/etc/profile.d/environment.shEOFalias cdnet="cd /etc/sysconfig/network-scripts"alias editnet7="vim /etc/sysconfig/network-scripts/ifcfg-ens33"alias diskscan2="echo - - - /sys/class/scsi_host/host2/scan"alias diskscan1="echo - - - /sys/class/scsi_host/host1/scan"alias diskscan0="echo - - - /sys/class/scsi_host/host0/scan"export PS1="[e[1;31m][d A [e[1;32m]u[e[0m][e[1;35m]@h [e[1;33m]w]\$ [e[0m]"export LANG=en_US.utf8export HISTSIZE=2000export HISTTIMEFORMAT="%F %T "export PATH=/data/scripts36:'$PATH'EOF grep "ifnames=0" /etc/default/grub & /dev/null || sed -r -i.bak '/linux16.*/s@.*quiet@& net.ifnames=0@' /boot/grub2/grub.cfg & /dev/null grep "ONBOOT=yes" /etc/sysconfig/network-scripts/ifcfg-ens33 & /dev/null || sed -r -i.bak '/BOOTPROTO/s@(BOOTPROTO=).*$@1static@;/DEVICE/s@(DEVICE=).*$@1eth0@;/ONBOOT/s@(ONBOOT=).*$@1yes@' /etc/sysconfig/network-scripts/ifcfg-ens33 grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-ens33 & /dev/null || echo -e "$IPADDR7$NETMASK$GATEWAY" /etc/sysconfig/network-scripts/ifcfg-ens33 grep "^SELINUX=disabled" /etc/selinux/config & /dev/null || sed -r -i.bak '/^SELINUX=/s@(SELINUX=).*@1disabled@' /etc/selinux/config systemctl stop firewalld.service &/dev/null systemctl disable firewalld.service &/dev/null [ -e /etc/yum.repos.d/backup ] && cp -au /etc/yum.repos.d/* /etc/yum.repos.d/backup &/dev/null || mkdir /etc/yum.repos.d/backup & /dev/null;mv /etc/yum.repos.d/* /etc/yum.repos.d/backup & /dev/null echo -e "$Start_Color Begain create YUM!!! $End_Color"cat/etc/yum.repos.d/base.repoEOF[base]name=Base $release7baseurl=file:///media/cdromgpgcheck=0cost=10[epel]name=aliyun epelbaseurl=https://mirrors.aliyun.com/epel/$release7/x86_64/gpgcheck=1gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7enabled = 0cost=100EOF echo -e "$Start_Color Create YUM Finish!!!$End_Color" [ ! -e /media/cdrom ] && mkdir /media/cdrom || true &/dev/null mount /dev/sr0 /media/cdrom & /dev/null echo "/dev/sr0 /media/cdrom iso9660 defaults 0 0" /etc/fstab echo -e "$Start_Color Beagin Install Package!!!$End_Color" echo -e "$Start_Color Wait for a moment!!!$End_Color" yum clean all &/dev/null yum repolist all &/dev/null yum install $PackageName -y echo -e "$Start_Color Finish Install Package,All Environment is set,Enjoy It!!!$End_Color" elif [ "$Sysver" = "6" ];thencat/etc/profile.d/environment.shEOFalias cdnet="cd /etc/sysconfig/network-scripts"alias editnet6="vim /etc/sysconfig/network-scripts/ifcfg-eth0"alias diskscan2="echo - - - /sys/class/scsi_host/host2/scan"alias diskscan1="echo - - - /sys/class/scsi_host/host1/scan"alias diskscan0="echo - - - /sys/class/scsi_host/host0/scan"export PS1="[e[1;31m][d A [e[1;33m]u[e[0m][e[1;36m]@h [e[1;35m]w]\$ [e[0m]" export LANG=en_US.utf8export HISTSIZE=2000export HISTTIMEFORMAT="%F %T "export PATH='/data/scripts36:$PATH'EOF grep "BOOTPROTO=dhcp" /etc/sysconfig/network-scripts/ifcfg-eth0 & /dev/null && sed -r -i.bak '/BOOTPROTO/s@(BOOTPROTO=).*$@1static@;/DEVICE/s@(DEVICE=).*$@1eth0@;/ONBOOT/s@(ONBOOT=).*$@1yes@' /etc/sysconfig/network-scripts/ifcfg-eth0 grep "IPADDR" /etc/sysconfig/network-scripts/ifcfg-eth0 & /dev/null || echo -e "$IPADDR6$NETMASK$GATEWAY" /etc/sysconfig/network-scripts/ifcfg-eth0 grep "^SELINUX=disabled" /etc/selinux/config & /dev/null || sed -r -i.bak '/^SELINUX=/s@(SELINUX=).*@1disabled@' /etc/selinux/config service network restart & /dev/null service iptables stop & /dev/null service iptables off & /dev/null [ -e /etc/yum.repos.d/backup ] && cp -au /etc/yum.repos.d/* /etc/yum.repos.d/backup &/dev/null || mkdir /etc/yum.repos.d/backup & /dev/null;mv /etc/yum.repos.d/* /etc/yum.repos.d/backup & /dev/nullecho -e "$Start_Color Begain create YUM!!! $End_Color"cat/etc/yum.repos.d/base.repoEOF[base]name=Base $release6baseurl=file:///media/cdromgpgcheck=0cost=10[httpd]name=httpd $release6baseurl=http://172.16.36.102/Centos/$release6/x86_64/gpgcheck=0cost=10[epel]name=aliyun epelbaseurl=https://mirrors.aliyun.com/epel/$release6/x86_64/gpgcheck=1gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7enabled = 0cost=100EOF echo -e "$Start_Color Create YUM Finish!!!$End_Color" [ ! -e /media/cdrom ] && mkdir /media/cdrom || true &/dev/null mount /dev/sr0 /media/cdrom & /dev/null echo "/dev/sr0 /media/cdrom iso9660 defaults 0 0" /etc/fstab /dev/null echo -e "$Start_Color Beagin Install Package!!!$End_Color" echo -e "$Start_Color Wait for a moment!!!$End_Color" yum clean all &/dev/null yum repolist all &/dev/null yum install $PackageName -y echo -e "$Start_Color Finish Install Package,All Environment is set,Enjoy It!!!$End_Color" elif [[ "$Sysver" -gt "7" ]] && [[ "$Sysver" -lt "6" ]];thencat/etc/profile.d/environment.shEOFalias cdnet="cd /etc/sysconfig/network-scripts"alias editnet7="vim /etc/sysconfig/network-scripts/ifcfg-ens33"alias editnet6="vim /etc/sysconfig/network-scripts/ifcfg-eth0"export PS1="[e[1;31m][d A [e[1;33m]u[e[0m][e[1;36m]@h [e[1;35m]w]\$ [e[0m]" export LANG=en_US.utf8export HISTSIZE=2000export HISTTIMEFORMAT="%F %T "export PATH=/data/scripts36:'$PATH'EOF fielse cp ~/.bashrc ~/.bashrc.bakcat ~/.bashrcEOF# .bashrc# User specific alias rm='rm -i'alias cp='cp -i'alias mv='mv -i'# Source global definitionsif [ -f /etc/bashrc ]; then. /etc/bashrcfialias scpp='scp -r wang@172.22.149.168:/data/script36 /root'alias cdnet="cd /etc/sysconfig/network-scripts"alias editnet7="vim /etc/sysconfig/network-scripts/ifcfg-ens33"alias editnet6="vim /etc/sysconfig/network-scripts/ifcfg-eth0"export LANG=en_US.utf8export HISTSIZE=2000export HISTTIMEFORMAT="%F %T "export PATH=/data/scripts36:'$PATH'EOF if [ "$Sysver" = "7" ];then echo 'export PS1="[e[1;31m][d A [e[1;32m]u[e[0m][e[1;35m]@h [e[1;33m]w]\$ [e[0m]"' ~/.bashrc else echo 'export PS1="[e[1;31m][d A [e[1;33m]u[e[0m][e[1;36m]@h [e[1;35m]w]\$ [e[0m]"' ~/.bashrc fifiunset Sysverunset releaseunset Start_Colorunset End_Colorunset IPADDR7unset IPADDR6unset NETMASKunset GATEWAYunset PackageName2,查看linux系统信息
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-03-16#FileName: systeminfo.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------export Start_Color="e[1;32m"export End_Color="e[0m"Ipaddr=`ifconfig | grep -Eo --color=auto "(([1-9]|[1-9][0-9]|[1-9][0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]|[1-9][0-9]|[1-9][0-9]{2}|2[0-4][0-9]|25[0-5])"| head -1`Version=`cut -d" " -f4 /etc/redhat-release`KerneVer=`uname -r`Cpu_Info=`lscpu | grep "Model name:"|tr -s " " |cut -d: -f2`Mem_Info=`free -mh|tr -s " "|cut -d" " -f2 |head -2| tail -1`HD_Info=`lsblk | grep "^sd{1,}"| tr -s " "|cut -d" " -f1,4`echo -e "The System Hostname is : $Start_Color `hostname` $End_Color"echo -e "The System IP ADDER is : $Start_Color $Ipaddr $End_Color"echo -e "The System Version is : $Start_Color $Version $End_Color" echo -e "The System kerneVer is : $Start_Color $KerneVer $End_Color"echo -e "The System Cpu_Info is :$Start_Color $Cpu_Info $End_Color"echo -e "The System Mem_Info is : $Start_Color $Mem_Info $End_Color"echo -e "The System HD_Info is : $Start_Color $HD_Info $End_Color"disk.shunset Start_Colorunset End_Colorunset Ipaddrunset Versionunset KerneVerunset Cpu_Infounset Mem_Infounset HD_Info
3,统计文件空行总数
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-03-20#FileName: argsum.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------if [ $# -lt 1 ];then echo "At least one argument(must be file)." exit 10elif [ -e $1 ] && [ -f $1 ];then Space_Line=`grep "^$" $1 | wc -l` echo "the $1 has ${Space_Line} space line" fi
4,检测硬盘利用率,如果超过百分之17就报警,并发送wall
#!/bin/bash ##---------------------------------------#Author: MartinHe#Date: 2019-03-20#FileName: checkdisk.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------Use_disk=`df |grep "^/dev/sd"| grep -Eo "[0-9]+%"|tr -d %|sort -nr|head -1`[ $Use_disk -ge 17 ] && wall "space will full"
5,添加一个用户
#!/bin/bash ##---------------------------------------#Author: MartinHe#Date: 2019-03-20#FileName: createuser.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------read -p "Please input a username: " USERif id $USER & /dev/null;then echo "the user is exist"else useradd $USER & /dev/null echo "add $USER successful"fi
6,文件类型判断
#!/bin/bash ##---------------------------------------#Author: MartinHe#Date: 2019-03-20#FileName: filetype.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------read -p "Plesa input(example: /etc/passwd or /etc ...) : " PATHHif [ -z "$PATHH" ];then echo "there must be a filename argument" exit 1elif [ ! -e "$PATHH" ];then echo "No such file,Are You Kidding Me?" exit 1elsea=`ls -l $PATHH|grep -o "^."`case $a in-)echo "common file" ;;d)echo "directory file" ;;b)echo "block file" ;;l)echo "sybolink file" ;;s)echo "socket file" ;;p)echo "pipe file" ;;c)echo "character file" ;;*)echo "other file" ;;esacfi
7,某ip是否在线
ping -c1 -W1 $1 & /dev/null && echo "$1 is up" || echo "$1 is donw"8,显示两个文件空行的和
#!/bin/bash ##---------------------------------------#Author: MartinHe#Date: 2019-03-19#FileName: sumspace.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------if [ $# -lt 2 ];then echo "Usage: sumspace.sh /PATH/TO/SOMEFILE /PATH/TO/SOMEFILE" exit 10elif [ $# -gt 2 ];then echo "two argument only." exit 20else FILE1=`grep "^$" $1|wc -l` FILE2=`grep "^$" $2|wc -l` SPACE_SUM=$[$FILE1+$FILE2] echo "The Space SUM is: $SPACE_SUM."fi
9,选择YES or NO
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-03-20#FileName: yesorno.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------read -p "Do you agree?(yes or NO):" ANSans1=`echo $ANS | grep -E -i "^[y]([e][s])?$"|tr [:upper:] [:lower:]`ans2=`echo $ANS | grep -E -i "^[n][o]$"|tr [:upper:] [:lower:]`if [ "$ans1" = "yes" ];then echo "It's ok" exitelif [ "$ans2" = "no" ];then echo "It's not ok" exitelse echo "Wrong input"fi
10,批量改某路径下的文件名后缀(更改后的文件名粉色显示)
DIR=/data/script36/workdir/cd $DIREND=logBC="e[1;"EC="e[0m"for file in *;do file_name=`echo $file | sed -r 's@^(.*)..*$@1@'` mv $file ${file_name}.$END echo -e "${BC}31m $file $EC is rename ${BC}35m ${file_name}.$END $EC success" done10,CentOS7 系统中 /data/路径下有大量的 jpg 小图片文件, 文件名称都是以 4 位小写字符加 4 位数字构成的,请编写一个 shell 或者 python 脚本将文件前四个小写字符相同的文件移动到以这四个字母为名字的文件夹中
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-05-28#FileName: archive.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------DIR=/tmpcd $DIRfor k in `ls -1 | while read filename;do echo ${filename:0:4};done | sort -u`;do mkdir $kdonefor i in *.jpg;do mv $i ${i:0:4}done#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-05-28#FileName: archive.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------DIR=/tmpcd $DIRls -1 | grep -oE '^[[:alpha:]]+' |sort -u | xargs mkdir for i in *.jpg;do mv $i ${i:0:4}done11,求1-100的和(for)(while)= seq -s + 100 | bc =echo {1..100} | tr " " + |bc
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: sum100.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------sum=0for i in `seq 100`;do let sum+=idone echo "The sum(1..100) is $sum" #!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: sum100_for.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------for ((sum=0,i=0; i=100; i++));do let sum+=idoneecho "the sum(1-100) is $sum"#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-08#FileName: sum100_while.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------sum=0i=1while [ $i -le 100 ];do let sum+=i let i++ done echo "the sum(1-100) is $sum"12,批量创建用户and批量删除用户(可以合成一个脚本,$0 create|del)
批量创建用户 首先定义一个userlist.txt
#!/bin/bash ##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: createuser.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------BC="e[1;"EC="e[0m"for user in `cat /data/script36/userlist.txt`;do if id $user & /dev/null;then echo -e "${BC}32m $user ${BC}5;31m exist $EC" else useradd $user echo $user | passwd --stdin $user & /dev/null echo -e "${BC}32m $user $EC create successed" fidone批量删除用户,也需要定义一个userlist.txt
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: deluser.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------BC="e[1;"EC="e[0m"for user in `cat /data/script36/userlist.txt`;do if id $user & /dev/null;then userdel -r $user echo -e "${BC}32m $user delete success!!! $EC" else echo -e "${BC}32m $user ${BC}5;31m not exist!!! $EC" fidone13,并发扫描多网段IP是否在线
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: scanip.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------NET=172.22for SUBNET in {0..255};do { for HOST in {1..254};do { ping -c1 -W1 $NET.$SUBNET.$HOST & /dev/null && echo $NET.$SUBNET.$HOST is up || echo $NET.$SUBNET.$HOST is down; }& done }& donewaitecho "scan host ip is finished"14,小星星之一闪一闪亮晶晶
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: star.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------read -p "Please input Column num:" colread -p "Please input Line num:" line#col=10#line=10BC="e[1;5;"for i in `seq $line`;do for j in `seq $col`;do COL=$[RANDOM%7+31]m echo -e "${BC}${COL}*c" doneecho -e "e[0m"done15,九九乘法表(正反)
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: 9x9.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------col=9for ((i=1;i=9;i++));do for ((j=1;j=$i;j++));do echo -e "$j*$i=$[j*i]tc" done #echo "$i" echodone#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-07#FileName: 9x9_rev.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------col=9for ((i=9;i=1;i--));do for ((j=1;j=$i;j++));do echo -e "$j*$i=$[j*i]tc" done #echo "$i" echodone16,打印等腰三角形
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-04#FileName: showstar.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------read -p "please input the Line num:" lineread -p "please input the Column num:" col#read -p "please input the light:" lnum #总行#line=9 #总列#col=9 # i是当前行号 每行打印的星星数位2i-1 # space空格数为line减去当前的i行号for i in `seq $line`;do light= space=$[line-i] star=$[2*i-1] for j in `seq $space`;do echo -e " c"; done for k in `seq $star`;do echo -e "*c";done echodone17,猜数字(1-10)
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-09#FileName: guessnum.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------NUM=$[RANDOM%10+1]while true ; do read -p "Please guess a num(1-10):" num if [ $num -lt $NUM ];then echo "$num small than the guess num" elif [ $num -gt $NUM ];then echo "$num bigger than the guess num" else echo "yeah,you are right" break fidone18,创建用户(shift)
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-09#FileName: createuser_shift.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------if [ -z "$1" ];then echo "Usage:`basename $0` username ..." exitfiwhile [ "$1" ];do if id $1 & /dev/null;then echo "$1 is exist" else useradd $1 echo $1 is added fi shiftdone19,监控http服务运行状况(while)
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-09#FileName: monitor.httpd_while.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------SLEEPING=20SERVICE=httpdLOG=/var/log/monitor_$SERVICE.logwhile true;do if killall -0 $SERVICE & /dev/null;then true else systemctl restart $SERVICE echo "AT `date +'%F %T'` $SERVICE restart" | tee -a $LOG | mail -s "warning $SERVICE" root fi sleep $SLEEPINGdone20,根据访问lastb,禁止10次以上ip访问(while)
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-10#FileName: denyip_while.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------LOG=cat /data/script36/ss.log | sed -nr '/^ESTAB/s@.* ([0-9.]+):[0-9]+.* ([0-9.]+):[0-9]+.*$@2@p'| sort | uniq -ccat $LOG | while read num ip;do if [ $num -gt 10 ];then iptables -A INPUT -s $ip -j REJECT fidone21,磁盘利用率报警(while read)
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-08#FileName: diskcheck_while.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------df | while read line;do DEVICE=echo $line | sed -nr '/^/dev/sd/s@([^ ]+).* ([0-9]+).*$@1@p' USED=echo $line | sed -nr '/^/dev/sd/s@([^ ]+).* ([0-9]+).*$@2@p' if [ $USED -gt 80 ];then wall $DEVICE will be full:$USED fidone22,点菜(select menu in ...)-可用于多脚本合成
#!/bin/bash##---------------------------------------#Author: MartinHe#Date: 2019-04-10#FileName: menu_select.sh#URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt#Copyright(c) 2019 All rights reserved#---------------------------------------# REPLY 是程序内部变量,用于存储用户输入的选项PS3="Please choose a num which one you want to do: "select menu in suancaiyu kaoquanyang yangxiezi huoguo quit;do case $menu in suancaiyu) echo "$menu 30 yuan" echo you input is $REPLY ;; kaoquanyang) echo "$menu 600 yuan" echo you input is $REPLY ;; yangxiezi) echo "$menu 150 yuan" echo you input is $REPLY ;; huoguo) echo "$menu 100 yuan" echo you input is $REPLY ;; quit) echo you input is $REPLY break ;; *) echo "Wrong choose,stupid!!!" esacdoneecho "Good job"23,比较两个参数的大小与阶乘(函数调用脚本)
函数定义 functions
is_digit () { while [ "$1" ];do [[ ! "$1" =~ ^[0-9]+$ ]] && { echo input not a digit;return 1; } shift done return}compare () { is_digit $* [ "$?" -eq 1 ] && return if [ "$1" -gt "$2" ];then echo $1 else echo $2 fi}jiecheng () { if [ $1 -eq 1 ];then echo 1 else echo $[`jiecheng $[$1-1]`*$1] fi}正式脚本compare.sh
#!/bin/bash##------------------------------------













