6.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" [email protected]
: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]# cat 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 [email protected]
set smtp=smtp.qq.com
set [email protected]
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 bin]# vim for_99.sh
#!/bin/bash
#
#********************************************************************
#Author: zhanghui
#QQ: 19661891
#Date: 2020-11-28
#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
x=$[i*j]
echo -e "${j}x${i}=${x}\t\c"
done
echo
done
:wq
[root@centos8 bin]# 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
echo -e "${j}*${i}=$[i*j]\t\c"
done
echo
done
:wq
[root@centos8 bin]# bash for_99_2.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_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
第六部分 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 bin]# vim while_99.sh
#Copyright (C): 2020 All rights reserved
#********************************************************************
i=1
j=1
while ((i<=9))
do
while ((j<=i))
do
let "temp=i*j"
echo -n "$i*$j=$temp "
let j++
done
let i++
let j=1
echo ""
done
:wq
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=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