SHELL脚本编程课后练习
- 一.
- 二.
-
- 1、编写脚本 argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给
- 2、编写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
- 3、编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
- 4、编写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
- 5、编写脚本 excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
- 6、编写脚本 nologin.sh和 login.sh,实现禁止和允许普通用户登录系统
- 三.
- 四.
- 五. for循环练习
-
- 1. 判断/var/目录下所有文件的类型
- 2.添加10个用户user1-user10,密码为8位随机字符
- 3./etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件名加stop,以S开通的输出为文件名加start,如K34filename stop S66filename start
- 4.编写脚本,提示输入正整数n的值,计算1+2+...+n的总和
- 5.计算100以内所有能被3整除的整数之和
- 6.编写脚本,提示输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
- 7.打印9x9乘法表
- 8.在/testdir目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如:1AbCdeFgH.html
- 9.打印等腰三角形
- 10.猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个。第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半加一个。到第10天早上想再吃时,只剩下一个桃子了。求第一天共摘了多少?
- 11.鸡兔同笼算法,今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?
- 12.选择输入yes or no
- for循环总结
- 六. while循环
- 七. 循环控制continue,break,shift
- 八. 函数
- 九. 数组
- 十.expect的应用
一.
1、编写脚本 systeminfo.sh,显示当前主机系统信息,包括:主机名,IPv4地址,操作系统版本,内核
版本,CPU型号,内存大小,硬盘大小
[root@centos8 script]#cat system_info.sh
#!/bin/bash
RED="\E[1;31m"
GREEN="echo -e \E[1;32m"
END="\E[0m"
$GREEN----------------------Host systeminfo--------------------$END
echo -e "HOSTNAME: $RED`hostname`$END"
#echo -e "IPADDR: $RED` ifconfig eth0|grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' |head -n1`$END"
echo -e "IPADDR: $RED` hostname -I`$END"
echo -e "OSVERSION: $RED`cat /etc/redhat-release`$END"
echo -e "KERNEL: $RED`uname -r`$END"
echo -e "CPU: $RED`lscpu|grep 'Model name'|tr -s ' '|cut -d : -f2`$END"
echo -e "MEMORY: $RED`free -h|grep Mem|tr -s ' ' : |cut -d : -f2`$END"
echo -e "DISK: $RED`lsblk |grep '^sd' |tr -s ' ' |cut -d " " -f4`$END"
$GREEN---------------------------------------------------------$END
#颜色设定
random_num=$[$RANDOM%7+31]
echo -e "\033[1;${random_num}m${random_num}\033[0m"
2、编写脚本 backup.sh,可实现每日将/etc/目录备份到/backup/etcYYYY-mm-dd中
[root@centos8 script]#cat backup.sh
#!/bin/bash
cp -r /etc/* /backup/etc`date +%Y-%m-%d`
[root@centos8 script]#vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
30 1 * * * /usr/bin/bash /root/script/backup.sh
3、编写脚本 disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
echo `df | grep 'sda' |tr -s " " % |cut -d% -f5|sort -nr | head -n1`
4、编写脚本 links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排
序
#!/bin/bash
last | grep '^root' | tr -s " "|cut -d " " -f3 |sort|uniq -c
二.
1、编写脚本 argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给
一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
[root@centos8 script]#cat argsnum.sh
#!/bin/bash
[ -z $1 ] && echo 至少应该给一个参数 || grep '^$' $1 |wc -l
[root@centos8 script]#bash argsnum.sh /etc/crontab
3
2、编写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
[root@centos8 script]#cat hostping.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-14
#FileName: hostping.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请输入一个IP地址:" ip
[ -z $ip ] && {
echo "输入有误,请重新输入" ;exit; } || {
ping -c1 -w1 $ip > /dev/null && echo 该IP地址可访问 || echo 该IP地址不可访问 ; }
[root@centos8 script]#bash hostping.sh
请输入一个IP地址:
输入有误,请重新输入
[root@centos8 script]#bash hostping.sh
请输入一个IP地址:10.0.0.188
该IP地址不可访问
[root@centos8 script]#bash hostping.sh
请输入一个IP地址:10.0.0.2
该IP地址可访问
3、编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满
[root@centos8 script]#vim check_disk2.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-14
#FileName: check_disk2.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
inode=`df -i |sed -nr 's@\/dev\/sd.* ([0-9]+)% .*@\1@p' |sort -nr |head -n1`
space=`df |sed -nr 's@\/dev\/sd.* ([0-9]+)% .*@\1@p' |sort -nr |head -n1`
WARNING=80
if [ $inode -gt $WARNING ];then
echo "Disk inodes used $inode%,it will be full" | mail -s "disk warning" 1589640561@qq.com
fi
if [ $space -gt $WARNING ];then
echo "Disk spaces used $inode%,it will be full" | mail -s "disk warning" 1589640561@qq.com
fi
4、编写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写
[root@centos8 script]#cat per.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-14
#FileName: per.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请输入文件路径:" fpath
#fpath=/root/script/perfile.txt
[ -z $fpath ] && {
echo "输入的文件路径有误,请重新输入" ;eixt; }
username=`whoami`
#写法1
[ ! -r $fpath ] && [ ! -w $fpath ] && echo "1 $username 对该文件不可读且不可写"
#写法2
[ ! -r $fpath -a ! -w $fpath ] && echo "2 $username 对该文件没有读写权限"
[root@centos8 script]#ll /root/script/perfile.txt
-rw-r----- 1 root root 0 Jul 14 22:41 /root/script/perfile.txt
[root@centos8 script]#bash per.sh
请输入文件路径:/root/script/perfile.txt
[liudh@centos8 ~]$bash /root/script/per.sh
请输入文件路径:/root/script/perfile.txt
1 liudh 对该文件不可读且不可写
2 liudh 对该文件没有读写权限
5、编写脚本 excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件
[root@centos8 practise练习]#vim excute.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-15
#FileName: excute.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
#read -p "请输入检测文件的路径:" FILE
FILE=/root/script/hello_word.sh
[ `ls $FILE |grep '.*.sh$'` ] && {
chmod a+x $FILE;ls -l $FILE; } || echo "$FILE is not a script file "
6、编写脚本 nologin.sh和 login.sh,实现禁止和允许普通用户登录系统
[root@centos8 script]#vim login_choice.sh
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-15
#FileName: login_choice.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请选择禁止或允许用户$1登录系统(yes/no):" choice
choice=`echo $choice |tr "A-Z" "a-z"`
case $choice in
y|yes)
usermod -s /bin/bash $1
echo "已允许用户$1登录系统"
;;
n|no)
usermod -s /sbin/nologin $1
echo "已禁止用户$1登录系统"
;;
*)
echo "输入有误,请重新输入"
exit
;;
esac
[root@centos8 script]#bash login_choice.sh liudh
请选择禁止或允许用户liudh登录系统(yes/no):n
已禁止用户liudh登录系统
[root@centos8 script]#getent passwd | grep liudh
liudh:x:1000:1000::/home/liudh:/sbin/nologin
[root@centos8 script]#bash login_choice.sh liudh
请选择禁止或允许用户liudh登录系统(yes/no):Y
已允许用户liudh登录系统
三.
1、让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin
[root@centos8 ~]#vim /etc/profile.d/editPATH.sh
#!/bin/bash
PATH="$PATH:/usr/local/apache/bin"
[root@centos8 ~]#source /etc/profile.d/editPATH.sh
[root@centos8 ~]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/apache/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-eno16777736 或 ifcfg-ens33 ’ (如果系统是CentOS7)
[root@centos8 practise练习]#vim setrootlogin.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-15
#FileName: setrootlogin.sh
#URL: https://blog.csdn.net/weixin_41142408
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
[ ! `whoami` = root ] && echo "You are not root" && exit
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
3、任意用户登录系统时,显示红色字体的警示提醒信息“Hi,dangerous!”
[root@centos8 script]#cat logintips.sh
#!/bin/bash
cat >> ~/.bash_profile <<EOF
echo -e '\E[1;31mHi,dangerous!\E[0m'
EOF
4、编写生成脚本基本格式的脚本,包括作者,联系方式,版本,时间,描述等
[root@centos8 ~]#cat .vimrc
set ts=4
set expandtab
set ignorecase
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: LingXia")
call setline(5,"#QQ: 1589640561")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: https://blog.csdn.net/weixin_41142408")
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
实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之。并设置初始密码为123456,显示添加的用户的id号等信息,在此新用户第一次登录时,会提示用户立即改密码,如果没有参数,就提示:请输入用户名
[root@centos8 script]#vim creaeuser.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-07-15
#FileName: creaeuser.sh
#URL: https://blog.csdn.net/weixin_41142408
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
[ -z $1 ] && echo "请输入需要创建的用户名,expr:$0 hehe" && exit
id $1 &> /dev/null && echo "user $1 exist" && exit
useradd $1
echo "123456" |passwd --stdin $1 > /dev/null
id $1
echo "$1 is created"
[root@centos8 script]#bash creaeuser.sh doudou
uid=1008(doudou) gid=1011(doudou) groups=1011(doudou)
doudou is created
[root@centos8 script]#bash creaeuser.sh root
user root exist
[root@centos8 script]#bash creaeuser.sh
请输入需要创建的用户名,expr:creaeuser.sh hehe
2、编写脚本 yesorno.sh,提示用户输入yes或no,并判断用户输入的是yes还是no,或是其它信息
[root@centos8 script]#cat bookmark/yesorno_case_tr.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-06-18
#FileName: case_yesorno.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "Are you OK (Yes/No) ?" answer
answer=`echo $answer |tr "A-Z" "a-z"`
case $answer in
y|yes)
echo "Yes"
;;
n|no)
echo "No"
;;
*)
echo "You input is error"
;;
esac
3、编写脚本 filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
[root@centos8 script]#cat for/for_test_filetype.sh
#!/bin/bash
#
#********************************************************************
#Author: LingXia
#QQ: 1589640561
#Date: 2021-06-21
#FileName: test_file.sh
#URL: https://www.baidu.com
#Description: The test script
#Copyright (C): 2021 All rights reserved
#********************************************************************
read -p "请输入完整的目录路径:" DIR
[ ! -z "${DIR}" ] && [ -d ${DIR} ] || {
echo "You input is false,please check it !" ; exit; }
echo "The directory is ${DIR}"
cd ${DIR}
for file in `ls ${
DIR}` ;do
if [ -d "${file}" ];then
echo -e "${DIR}/${file} \t \t \t \t Directory"
elif [ -L "${file}" ];then #如果先检查普通文件后再检查链接文件,链接文件会被识别为普通文件
echo -e "${DIR}/${file} \t \t \t \t Symbolic Link"
elif [ -f "${file}" ];then
echo -e "${DIR}/${file} \t \t \t \t Regular File"
elif [ -b "${file}" ];then
echo -e "${DIR}/${file} \t \t \t