7. 第六章 shell脚本编程基础
一.第一部分
1、编写脚本systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
[root@centos8 ~]# vim systeminfo.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-25
#FileName: systeminfo.sh
#URL: www.neteagles.cn
#Description: Show system information
#Copyright (C): 2020 All rights reserved
#********************************************************************
RANDOM_COLOR="\E[1;"$[RANDOM%7+31]"m"
GREEN="echo -e \E[1;32m"
COLOR_END="\E[0m"
ETHNAME=`ifconfig |head -1| tr -s ":" " "|cut -d" " -f 1`
$GREEN----------------------Host systeminfo--------------------$COLOR_END
echo -e "HOSTNAME: $RANDOM_COLOR`hostname`$COLOR_END"
echo -e "IPADDR: $RANDOM_COLOR` ifconfig $ETHNAME|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$COLOR_END"
echo -e "OSVERSION: $RANDOM_COLOR`cat /etc/redhat-release`$COLOR_END"
echo -e "KERNEL: $RANDOM_COLOR`uname -r`$COLOR_END"
echo -e "CPU: $RANDOM_COLOR`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`$COLOR_END"
echo -e "MEMORY: $RANDOM_COLOR`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$COLOR_END"
echo -e "DISK: $RANDOM_COLOR`lsblk |grep '^sd' |tr -s ' ' |cut -d " " -f4`$COLOR_END"
$GREEN---------------------------------------------------------$COLOR_END
2、编写脚本backup.sh,可事先每日将/etc目录备份到/backup/etcYYYY-mm-dd中
[root@centos8 ~]# vim backup.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-25
#FileName: backup.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
COLOR='\E[1;31m'
COLOR_END='\E[0m'
SRC=/etc
DEST=/data
echo -e ${COLOR}Starting backup...${COLOR_END}
sleep 2
cp -av $SRC $SRC$DEST`date +%F_%H-%M-%S`
echo -e ${COLOR}Backup is finished${COLOR_END}
3、编写脚本disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@centos8 ~]# vim disk.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-26
#FileName: disk.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
df |grep "/dev/sda*" |grep -o '[0-9]\{1,3\}%'|sort -nr |head -1
:wq
4、编写脚本links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
[root@centos8 ~]# vim link.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-26
#FileName: link.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
last | grep '^root' | tr -s " "|cut -d " " -f3 |sort|uniq -c
:wq
二.第二部分
1、编写脚本argsnum.sh,接受一个文件路径作为参数:如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
[root@centos8 bin]# vim argsnum.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: argsnum.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
FILE=$1
[ -z $FILE ] && echo "至少应该给一个参数" || grep '^$' $FILE |wc -l
:wq
[root@centos8 bin]# argsnum.sh
至少应该给一个参数
[root@centos8 bin]# argsnum.sh /etc/init.d/functions
91
2、编写脚本hostping.sh,接受一个主机的IPv4地址做为参数,测试是否连通。如果能ping通,则提示用户“该ip地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
[root@centos8 bin]# vim hostping.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: hostping.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
IP=10.0.0.7
ping -c1 -w1 $IP &> /dev/null && echo "该$IP地址可访问!" || { echo "该$IP地址不可访问!";exit; }
echo "Scripts is finshed"
:wq
3、编写脚本checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
[root@centos8 bin]# vim checkdisk.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: checkdisk.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
WARNING=80
SPACE_USED=`df |grep '^/dev/sd' |grep -oE '[0-9]+%' |tr -d % |sort -nr |head -1`
INODE_USED=`df -i |grep '^/dev/sd' |grep -oE '[0-9]+%' |tr -d % |sort -nr |head -1`
[ "$SPACE_USED" -gt $WARNING -o "$INODE_USED" -gt $WARNING ] && echo "DISK_USED:$SPACE_USED%,INODE_USED:$INO
DE_USED,will be full" |mail -s "DISK warning" 19661891@qq.com
:wq
4、编写脚本per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
[root@centos8 bin]# vim per.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: per.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
FILE=/etc/shadow
[ ! -r "$FILE" -a ! -x "$FILE" ]
:wq
[root@centos8 bin]# bash per.sh
[root@centos8 bin]# echo $?
1
[root@centos8 bin]# su neteagle
[neteagle@centos8 bin]$ bash per.sh
[neteagle@centos8 bin]$ echo $?
0
[neteagle@centos8 bin]$ exit
exit
5、编写脚本excute.sh,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
[root@centos8 bin]# vim excute.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: excute.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
FILE=~/bin/ping.sh
[ -f "$FILE" ] && [[ "$FILE" =~ .*\.sh$ ]] && chmod +x $FILE
:wq
[root@centos8 bin]# bash excute.sh
[root@centos8 bin]# ll ping.sh
-rwxr-xr-x 1 root root 481 Nov 27 17:19 ping.sh
6、编写脚本nologin.sh和login.sh ,实现禁止和允许普通用户登录系统
[root@centos8 bin]# vim nologin.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: nologin.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
echo Deny common user log > /etc/nologin
:wq
[root@centos8 bin]# vim login.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: login.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
rm -f /etc/nologin
:wq
三.第三部分
1、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin
[root@centos8 ~]# vim /etc/profile.d/pro.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: /etc/profile.d/pro.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
PATH="$PATH:/usr/local/apache/bin"
:wq
[root@centos8 ~]# . /etc/profile.d/pro.sh
[root@centos8 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin;/usr/local/apache/bin:/root/bin
2、用户root登陆时,将命令指示符变成红色,并自启动如下别名:
rm='rm -i'
cdnet='cd /etc/sysconfig/network-scripts'
editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
editnet='vim /etc/sysconfig/network-scripts/ifcfg-eto16777736 或 ifcfg-ens33'(如果系统是Centos 7)
[root@centos8 ~]# vim env.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: env.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
cat >>~/.bashrc <<EOF
PS1='\[\e[1;31m\][\u@\h \W]\\$ \[\e[0m\]'
alias rm='rm -i'
alias cdnet='cd /etc/sysconfig/network-scripts'
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
EOF
:wq
[root@centos8 ~]# alias rm
alias rm='rm -i'
[root@centos8 ~]# alias cdnet
alias cdnet='cd /etc/sysconfig/network-scripts'
[root@centos8 ~]# alias editnet
alias editnet='vim /etc/sysconfig/network-scripts/ifcfg-eth0'
3、任意用户登录系统是,显示红色字体的警示提醒信息“Hi,dangerous! ”
[root@centos8 ~]# vim env2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: env2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
cat >>~/.bash_profile <<EOF
echo -e '\E[1;31mHi,dangerous!\E[0m'
EOF
:wq
[root@centos8 ~]# exit
4、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@centos8 ~]# cat .vimrc
set ts=4
set expandtab
set ignorecase
set cursorline
set autoindent
autocmd 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: zhanghui")
call setline(5,"#QQ: 19661891")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: www.neteagles.cn")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
四.第四部分
1、编写脚本createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示启存在,否则添加之;显示添加的用户的id号等信息
[root@centos8 bin]# vim createuser.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: createuser.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
USER=haha
id $USER &> /dev/null && echo $USER is exist || { useradd $USER ; echo $USER is created;id $USER; }
:wq
[root@centos8 bin]# vim createuser2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: createuser2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
USER=hehe
if id $USER &> /dev/null;then
echo $USER is exist
else
useradd $USER ; echo $USER is created;id $USER
fi
:wq
[root@centos8 bin]# bash createuser.sh
hehe is created
uid=1003(hehe) gid=1003(hehe) groups=1003(hehe)
2、编写脚本yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
[root@centos8 bin]# cat yesorno.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: yesorno.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "Do you agree(yes/no)?" INPUT
INPUT=`echo $INPUT | tr 'A-Z' 'a-z'`
case $INPUT in
y|yes)
echo "You input is YES"
;;
n|no)
echo "You input is NO"
;;
*)
echo "Input fales,please input yes or no!"
esac
:wq
[root@centos8 bin]# bash yesorno.sh
Do you agree(yes/no)?yes
You input is YES
[root@centos8 bin]# bash yesorno.sh
Do you agree(yes/no)?n0
Input fales,please input yes or no!
[root@centos8 bin]# bash yesorno.sh
Do you agree(yes/no)?nO
You input is NO
[root@centos8 bin]# vim yesorno2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: yesorno2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "Do you agree(yes/no)? " INPUT
case $INPUT in
[yY]|[Yy][Ee][Ss])
echo "You input is YES"
;;
[Nn]|[Nn][Oo])
echo "You input is NO"
;;
*)
echo "Input fales,please input yes or no!"
esac
:wq
3、编写脚本filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其他文件类型)
[root@centos8 bin]# vim filetype.sh
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: filetype.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
BEGINCOLOR="\033[$[RANDOM%6+31]m"
ENDCOLOR="\033[0m"
read -p "Please input file name: " file
if [ -L $file ];then
echo -e $BEGINCOLOR"$file 文件为链接文件"$ENDCOLOR
elif [ -d $file ];then
echo -e $BEGINCOLOR"$file 文件为目录文件"$ENDCOLOR
elif [ -f $file ];then
echo -e $BEGINCOLOR"$file 文件为普通文件"$ENDCOLOR
else
echo -e $BEGINCOLOR"$file 文件为其它文件类型"$ENDCOLOR
fi
:wq
[root@centos8 bin]# bash filetype.sh
Please input file name: /etc/issue
/etc/issue 文件为普通文件
[root@centos8 bin]# bash filetype.sh
Please input file name: /bin
/bin 文件为链接文件
[root@centos8 bin]# bash filetype.sh
Please input file name: /
/ 文件为目录文件
[root@centos8 bin]# bash filetype.sh
Please input file name: /dev/null
/dev/null 文件为其它文件类型
4、编写脚本checkint.sh,判断用户输入的参数是否为正整数
[root@centos8 bin]# vim checkint.sh
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: checkint.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
BEGINCOLOR="\033[$[RANDOM%6+31]m"
ENDCOLOR="\033[0m"
read -p "请输入一个参数: " data
if [[ "$data" =~ ^[0-9]+$ ]];then
if [ $data -eq 0 ];then
echo -e $BEGINCOLOR"0 不是正整数"$ENDCOLOR
else
echo -e $BEGINCOLOR"$data 为正整数"$ENDCOLOR
fi
else
echo -e $BEGINCOLOR"输入格式不对,请输入数字!"$ENDCOLOR
fi
:wq
[root@centos8 bin]# bash checkint.sh
请输入一个参数: 10
10 为正整数
[root@centos8 bin]# bash checkint.sh
请输入一个参数: 0.5
输入格式不对,请输入数字!
5、编写脚本reset.sh ,实现系统安装后的初始化环境,包括:(1)别名,(2)环境变量,如PS等,(3)安装常用软件包,如:tree,(5)实现固定IP的设置,(6)vim的设置等
[root@centos8 ~]# cat reset_centos.sh
#!/bin/bash
#
#*************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-27
#FileName: reset.sh
#URL: www.neteagles.cn
#Description: Reset CentOS
#Copyright (C): 2020 All rights reserved
#*************************************************************
. /etc/init.d/functions
centos_version() {
sed -rn 's#^.* ([0-9]+)\..*#\1#p' /etc/redhat-release
}
disable_selinux(){
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
action "CentOS`centos_version`SELinux已禁用,请重新启动系统后才能生效!"
}
set_alias(){
cat >>~/.bashrc <<EOF
alias cdnet="cd /etc/sysconfig/network-scripts"
alias editeth0="vim /etc/sysconfig/network-scripts/ifcfg-eth0"
alias scandisk="echo '- - -' > /sys/class/scsi_host/host0/scan;echo '- - -' > /sys/class/scsi_host/host1/scan;echo '- - -' > /sys/class/scsi_host/host2/scan"
EOF
action "CentOS`centos_version`系统别名已设置成功,请重新登陆后生效!"
}
set_vimrc(){
cat >~/.vimrc <<EOF
set ts=4
set expandtab
set ignorecase
set cursorline
set autoindent
autocmd 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: zhanghui")
call setline(5,"#QQ: 19661891")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: www.neteagles.cn")
call setline(9,"#Description: The test script")
call setline(10,"#Copyright (C): ".strftime("%Y")." All rights reserved")
call setline(11,"#********************************************************************")
call setline(12,"")
endif
endfunc
autocmd BufNewFile * normal G
EOF
action "CentOS`centos_version`vimrc设置完成,请重新系统启动才能生效!"
}
set_mailrc(){
cat >~/.mailrc <<EOF
set from=19661891@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=19661891@qq.com
set smtp-auth-password=hrlnpctmxkpqbjdd
set smtp-auth=login
set ssl-verify=ignore
EOF
action "CentOS`centos_version`mailrc设置完成,请重新登录后才能生效!"
}
disable_firewalld_centos78(){
systemctl disable --now firewalld &> /dev/null
action "CentOS`centos_version`防火墙已关闭!"
}
disable_firewalld_centos6(){
chkconfig iptables off
action "CentOS`centos_version`防火墙已关闭!"
}
disable_firewalld(){
centos_version | while read ov ;do
if [ $ov -eq 6 ];then
disable_firewalld_centos6
else
disable_firewalld_centos78
fi
done
}
set_yum_centos8(){
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[BaseOS]
name=BaseOS
baseurl=https://mirrors.aliyun.com/centos/\$releasever/BaseOS/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[AppStream]
name=AppStream
baseurl=https://mirrors.aliyun.com/centos/\$releasever/AppStream/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
[EPEL]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/\$releasever/Everything/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
enabled=1
[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/os/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
EOF
dnf clean all
dnf repolist
action "CentOS`centos_version`阿里YUM源设置完成!"
}
set_yum_centos67(){
mkdir /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup
cat > /etc/yum.repos.d/base.repo <<EOF
[base]
name=base
baseurl=https://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-\$releasever
[extras]
name=extras
baseurl=https://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[updates]
name=updates
baseurl=https://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
[centosplus]
name=centosplus
baseurl=https://mirrors.aliyun.com/centos/\$releasever/centosplus/\$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-\$releasever
EOF
yum clean all
yum repolist
action "CentOS`centos_version`阿里YUM源设置完成!"
}
set_yum(){
centos_version | while read ov2 ;do
if [ $ov2 -eq 8 ];then
set_yum_centos8
else
set_yum_centos67
fi
done
}
set_eth(){
ETHNAME=`ifconfig |head -1| tr -s ":" " "|cut -d" " -f 1`
#修改网卡名称配置文件
sed -ri.bak '/^GRUB_CMDLINE_LINUX=/s@"$@ net.ifnames=0"@' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg >& /dev/null
#修改网卡文件名
mv /etc/sysconfig/network-scripts/ifcfg-${ETHNAME} /etc/sysconfig/network-scripts/ifcfg-eth0
#修改配置文件
sed -i.bak -e 's/NAME="'${ETHNAME}'"/NAME="eth0"/' -e 's/DEVICE="'${ETHNAME}'"/DEVICE="eth0"/' /etc/sysconfig/network-scripts/ifcfg-eth0
#修改IP地址
read -p "请输入IP地址:" IP
echo $IP
echo "$IP" | grep -E --color '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$'
if [ $? -ne 0 ];then
echo "你输入ip地址不符和要求"
exit
fi
cat >> /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
IPADDR=$IP
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=223.5.5.5
DNS2=223.6.6.6
EOF
sed -i.bak -e 's/BOOTPROTO="dhcp"/BOOTPROTO="none"/' /etc/sysconfig/network-scripts/ifcfg-eth0
action "CentOS`centos_version`网卡名和IP地址已修改成功,请重新启动系统后才能生效!"
}
set_ip_centos6(){
read -p "请输入IP地址:" IP
echo $IP
echo "$IP" | grep -E --color '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$'
if [ $? -ne 0 ];then
echo "你输入ip地址不符和要求"
exit
fi
cat >> /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
IPADDR=$IP
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=223.5.5.5
EOF
sed -i.bak -e 's/BOOTPROTO=dhcp/BOOTPROTO=none/' /etc/sysconfig/network-scripts/ifcfg-eth0
action "CentOS`centos_version`IP地址已修改成功,请重新启动系统后才能生效!"
}
centos_minimal_install(){
yum -y install gcc make autoconf gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel vim lrzsz tree tmux lsof tcpdump wget net-tools iotop bc bzip2 zip unzip nfs-utils man-pages
action "CentOS`centos_version`最小化安装建议安装软件已安装完成!"
}
set_centosps1(){
TIPS="action CentOS`centos_version`PS1已设置完成,请重新登录生效!"
while true;do
echo -e "\E[$[RANDOM%7+31];1m"
cat <<EOF
1)31 红色
2)32 绿色
3)33 黄色
4)34 蓝色
5)35 紫色
6)36 青色
7)随机颜色
8)退出
EOF
echo -e '\E[0m'
read -p "请输入颜色编号(1-8)" NUM
case $NUM in
1)
echo "PS1='\[\e[1;31m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
2)
echo "PS1='\[\e[1;32m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
3)
echo "PS1='\[\e[1;33m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
4)
echo "PS1='\[\e[1;34m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
5)
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
6)
echo "PS1='\[\e[1;36m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
7)
echo "PS1='\[\e[1;"$[RANDOM%7+31]"m\][\u@\h \W]\\$ \[\e[0m\]'" > /etc/profile.d/env.sh
$TIPS
;;
8)
break
;;
*)
echo -e "\e[1;31m输入错误,请输入正确的数字(1-8)!\e[0m"
;;
esac
done
}
PS3="请选择相应的编号(1-13):"
MENU="
CentOS禁用SELinux
CentOS设置系统别名
CentOS设置vimrc配置文件
CentOS设置mailrc配置文件
CentOS关闭防火墙
CentOS设置阿里YUM源
CentOS_Minimal安装建议安装软件
CentOS1-7全执行
CentOS7和8修改网卡名和IP地址
CentOS6修改IP地址
CentOS设置PS1(请进入选择颜色)
重启系统
退出
"
select menu in $MENU;do
case $REPLY in
1)
disable_selinux
;;
2)
set_alias
;;
3)
set_vimrc
;;
4)
set_mailrc
;;
5)
disable_firewalld
;;
6)
set_yum
;;
7)
centos_minimal_install
;;
8)
disable_selinux
set_alias
set_vimrc
set_mailrc
disable_firewalld
set_yum
centos_minimal_install
;;
9)
set_eth
;;
10)
set_ip_centos6
;;
11)
set_centosps1
;;
12)
reboot
;;
13)
break
;;
*)
echo -e "\e[1;31m输入错误,请输入正确的数字(1-13)!\e[0m"
;;
esac
done
五.第五部分 for
用for实现
1、判断/var/目录下所有文件的类型
[root@centos8 bin]# vim for_var.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_var.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "Please input directory: " DIR
for FILE in `ls $DIR` ;do
TY=`file $DIR/$FILE | egrep -o "link|text|block|directory"`
case $TY in
text)
echo "file $DIR/$FILE is file"
;;
link)
echo "file $DIR/$FILE is Link"
;;
block)
echo "file $DIR/$FILE is Block"
;;
directory)
echo "file $DIR/$FILE is Directory"
;;
*)
echo "file $DIR/$FILE is Others"
esac;
done
:wq
[root@centos8 bin]# bash for_var.sh
Please input directory: /var/
file /var//account is Directory
file /var//adm is Directory
file /var//cache is Directory
file /var//crash is Directory
file /var//db is Directory
file /var//empty is Directory
file /var//ftp is Directory
file /var//games is Directory
file /var//gopher is Directory
file /var//kerberos is Directory
file /var//lib is Directory
file /var//local is Directory
file /var//lock is Link
file /var//log is Directory
file /var//mail is Link
file /var//nis is Directory
file /var//opt is Directory
file /var//preserve is Directory
file /var//run is Link
file /var//spool is Directory
file /var//tmp is Directory
file /var//yp is Directory
2、添加10个用户user1-user10,密码为8位随机字符
[root@centos8 bin]# vim for_user.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_user.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
USER=`echo user{1..10}`
for NAME in $USER;do
useradd $NAME
PASS=`cat /dev/urandom |tr -dc '[:alnum:]' |head -c8`
echo $PASS | passwd --stdin $NAME &> /dev/null
passwd -e $NAME &> /dev/null
echo $NAME:$PASS >>~/user.txt
echo "$NAME is created"
done
:wq
[root@centos8 ~]# bash for_user.sh
user1 is created
user2 is created
user3 is created
user4 is created
user5 is created
user6 is created
user7 is created
user8 is created
user9 is created
user10 is created
[root@centos8 ~]# getent passwd |grep ^user
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash
user5:x:1005:1005::/home/user5:/bin/bash
user6:x:1006:1006::/home/user6:/bin/bash
user7:x:1007:1007::/home/user7:/bin/bash
user8:x:1008:1008::/home/user8:/bin/bash
user9:x:1009:1009::/home/user9:/bin/bash
user10:x:1010:1010::/home/user10:/bin/bash
[root@centos8 ~]# cat user.txt
user1:23qmQSfC
user2:He6G7ece
user3:BgXS7h0E
user4:LOJRARlx
user5:PduPowgz
user6:AKsDJl4y
user7:cPq5gmqf
user8:h4hHZAfR
user9:IcU8ZvRZ
user10:l9EqkZmh
3、 /etc/rc.d/rc3.d目录下分别有多个K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加 start,如K34filename stop S66filename start
[root@centos7 ~]# vim for_rc.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_rc.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for FILE in `ls /etc/rc.d/rc3.d/ | grep -E '^K|^S'` ;do
S=`echo $FILE | grep -Eo '^K|^S'`
if [ "$S" = "K" ];then
echo $FILE stop
else
echo $FILE start
fi;
done
:wq
[root@centos7 ~]# bash for_rc.sh
K50netconsole stop
S10network start
4、编写脚本,提示输入正整数n的值,计算1+2+...+n的总和
[root@centos8 bin]# vim for_sum.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_sum.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
sum=0
N=1000
for i in `seq $N`;do
let sum+=i
done
echo $sum
:wq
[root@centos8 bin]# bash for_sum.sh
500500
[root@centos8 bin]# vim for_sum2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_sum2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
N=1000
for ((sum=0,i=1;i<=$N;i++));do
let sum+=i
done
echo $sum
:wq
[root@centos8 bin]# bash for_sum2.sh
500500
[root@centos8 bin]# vim for_sum3.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_sum3.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "Please enter a positive integer: " num
if [ $num -gt 0 ];then
sum=0
for i in `seq $num`;do
let sum+=$i;
done
echo $sum
else
echo "Please input digit!"
fi
:wq
[root@centos8 bin]# bash for_sum3.sh
Please enter a positive integer: 50
1275
[root@centos8 bin]# bash for_sum3.sh
Please enter a positive integer: 1000
500500
5、计算100以内所有能被3整除的整数之和
[root@centos8 bin]# vim for_divide3.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_divide3.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
sum=0
for i in {3..100..3};do
let sum+=$i
done
echo $sum
:wq
[root@centos8 bin]# bash for_divide3.sh
1683
6、编写脚本,提示输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
[root@centos8 bin]# vim for_scanip.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_scanip.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
net=192.168.0
for ((i=1;i<=254;i++));do
ping -c1 -w1 $net.$i &> /dev/null && echo "$net.$i is up" || echo "$net.$i is down"
done
:wq
[root@centos8 bin]# vim for_scanip2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_scanip2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
NET=192.168.0
for ID in {1..254};do
{
ping -c1 -w1 $NET.$ID &> /dev/null && echo $NET.$ID is up || echo $NET.$ID id down
}&
done
wait
:wq
7、打印九九乘法表
[root@centos8 ~]# vim for_99.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-29
#FileName: for_99.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for i in {1..9};do
for j in `seq $i`;do
echo -e "${j}x${i}=$[j*i]\t\c"
done
echo
done
:wq
[root@centos8 ~]# bash for_99.sh
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
[root@centos8 bin]# vim for_99_2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_99_2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for i in {1..9};do
for j in `seq $i`;do
x=$[i*j]
echo -e "${j}x${i}=${x}\t\c"
done
echo
done
:wq
[root@centos8 bin]# bash for_99_2.sh
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
[root@centos8 bin]# vim for_99_3.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_99_3.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for ((i=1;i<10;i++));do
for ((j=1;j<=i;j++));do
echo -e "${j}*${i}=$[i*j]\t\c"
done
echo
done
:wq
[root@centos8 bin]# bash for_99_3.sh
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
[root@centos8 bin]# vim for_99_4.sh
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_99_4.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "请输入您想要打印的乘法表行数: " line
if [ -z $line ];then
line=9
fi
for i in `seq $line`;do
for j in `seq $i`;do
echo -e "${j} x ${i} = $((i*j))\t\c"
done
echo
done
:wq
[root@centos8 bin]# bash for_99_4.sh
请输入您想要打印的乘法表行数: 5
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
8、在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
[root@centos8 bin]# vim for_testdir.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_testdir.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
mkdir /data/testdir
for((i=1;i<=10;i++));do
RAN=`tr -dc 'A-Za-z' </dev/urandom | head -c 8`
touch /data/testdir/${i}${RAN}.html
done
echo "Ten files created successfully"
:wq
[root@centos8 bin]# bash for_testdir.sh
Ten files created successfully
[root@centos8 bin]# ll /data/testdir/
total 0
-rw-r--r-- 1 root root 0 Nov 28 20:08 10AKkGegRC.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 1fQecGbzw.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 2YwnKRlOs.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 3zhiZUEFg.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 4maWGftwn.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 5mTwCdHBg.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 6VHohDOBC.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 7tlBAnYhU.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 8XFUZptEz.html
-rw-r--r-- 1 root root 0 Nov 28 20:08 9bRkliopj.html
9、打印等腰三角形
[root@centos8 bin]# vim for_triangle.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_triangle.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "请输入三角形的行数:" line
for ((i=1;i<=line;i++));do
for ((k=0;k<=line-i;k++));do
echo -e ' \c'
done
for ((j=1;j<=2*i-1;j++));do
echo -e '*\c'
done
echo
done
:wq
[root@centos8 bin]# bash for_triangle.sh
请输入三角形的行数:10
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
10、猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,只剩下一个桃子。求第一天一共摘了多少?
[root@centos8 bin]# vim monkey.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: monkey.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
sum=1
for i in {9..1};do
let sum=(sum+1)*2
done
echo "桃子的个数是: $sum"
unset sum
:wq
[root@centos8 bin]# bash monkey.sh
桃子的个数是: 1534
[root@centos8 bin]# vim for_monkey2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: for_monkey2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "请输入天数: " day
read -p "请输入最后一天剩余个数: " sum
let day=day-1
for i in `seq 1 $day`;do
let sum=(sum+1)*2
done
echo "桃子的个数是: $sum"
unset sum
:wq
[root@centos8 bin]# bash for_monkey2.sh
请输入天数: 20
请输入最后一天剩余个数: 5
桃子的个数是: 3670014
[root@centos8 bin]# bash for_monkey2.sh
请输入天数: 5
请输入最后一天剩余个数: 10
桃子的个数是: 190
11.编写脚本,实现打印国际象棋棋盘
[root@centos8 ~]# vim for_chess.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: chess.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for i in {1..8};do
for j in {1..8};do
flag=$[(j+i)%2]
if [ $flag -eq 0 ];then
echo -e "\033[47m \033[0m\c"
else
echo -e " \c"
fi
done
echo
done
:wq
[root@centos8 ~]# vim for_chess2.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: chess2.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for i in {1..8};do
for j in {1..4};do
if [ $[i%2] -eq 0 ];then
echo -e "\033[47m \033[0m \c"
else
echo -e " \033[47m \033[0m\c"
fi
done
echo
done
:wq
[root@centos8 ~]# vim for_chess3.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: chess3.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
for i in {1..4};do
for j in {1..4};do
echo -e "\033[47m \033[0m \c"
done
echo
for k in {1..4};do
echo -e " \033[047m \033[0m\c"
done
echo
done
:wq
六.第六部分 while
用while实现
1、编写脚本,求100以内所有正奇数之和
[root@centos8 bin]# vim while_sum.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: while_sum.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
sum=0
i=1
while [ $i -le 100 ];do
sum=$[${sum}+${i}]
i=$[$i+2]
done
echo "100以内所有正奇数之和为:$sum"
:wq
[root@centos8 bin]# bash while_sum.sh
100以内所有正奇数之和为:2500
2、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线和离线主机各多少
[root@centos8 bin]# cat while_scanhost.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: while_scanhost.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
read -p "Please enter the network address(eg:192.168.0.0): " NET
netid=echo $NET|cut -d. -f1-2
i=0
up=0
down=0
while [ $i -le 254 ];do
j=1
while [ $j -le 254 ];do
echo $netid.$i.$j
if `ping -c1 -w1 $netid.$i.$j &>/dev/null`;then
echo "The $netid.$i.$j is up"
let up++
else
echo "The $netid.$i.$j is down"
let down++
fi
let j++
done
let i++
done
echo "The up is $up"
echo "The down is $down"
:wq
3、编写脚本,打印九九乘法表
[root@centos8 ~]# vim while_99.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-29
#FileName: while_99.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
i=1
while [ $i -le 9 ];do
j=1
while [ $j -le $i ];do
echo -e "${j}x${i}=$[j*i]\t\c"
let j++
done
echo
let i++
done
:wq
[root@centos8 ~]# bash while_99.sh
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
4、编写脚本,利用变量RANDOM生成10个随机数字,输出这10个数字,并显示其中的最大值和最小值
[root@centos8 bin]# vim while_random.sh
#Date: 2020-11-28
#FileName: while_random.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
declare -i a=0
max=0
min=$RANDOM
while [ $a -le 9 ];do
num=$RANDOM
if [ $num -le $max ];then
if [ $num -le $min ];then
min=$num
fi
else
max=$num
fi
let a++
done
echo "the min number is $min,the max number is $max"
:wq
[root@centos8 bin]# bash while_random.sh
the min number is 2405,the max number is 28407
[root@centos8 bin]# bash while_random.sh
the min number is 748,the max number is 31986
5、编写脚本,实现打印国际象棋棋盘
[root@centos8 bin]# vim while_chess.sh
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: while_chess.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
let i=0
while [ $i -le 15 ];do
for((j=0;j<4;j++));do
if [ $[i%4] -eq 0 -o $[i%4] -eq 1 ];then
echo -e "\033[1;41m \033[0m\033[1;47m \033[0m\c"
else
echo -e "\033[1;47m \033[0m\033[1;41m \033[0m\c"
fi
done
echo
let i++
done
:wq
6、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f63是通过对随机数变量RANDOM随机执行命令:echo $RANDOM | md5sum | cut -c1-10 后的结果,请破解这些字符串对应的RANDOM值
[root@centos8 bin]# vim while_md5sum_random.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#FileName: while_md5sum_random.sh
#URL: www.neteagles.cn
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
cat >test.txt <<EOF
efbaf275cd
4be9c40b8b
44b2395c46
f8c8873ce0
b902c16c8b
ad865d2f63
EOF
cat test.txt | while read CHESS;do
{ while true;do
MD=`echo $RANDOM|md5sum|cut -c1-10`
if [[ "$MD" == "$CHESS" ]];then
echo $RAN
break
else
let RAN++
fi
done }&
wait
done
:wq
[root@centos8 bin]# bash while_md5sum_random.sh
1152
36060
12754
7940
6945
4420