SHELL脚本练习

  • 来源:网络
  • 更新日期:2020-08-21

摘要:系统运维 1.编写脚本 sumid.sh,计算/etc/passwd⽂件中的第10个⽤户和第20⽤户的ID之和 #!/bin/bashS_O=head /etc/passwd | tail -1 | cu

系统运维

1.编写脚本 sumid.sh,计算/etc/passwd⽂件中的第10个⽤户和第20⽤户的ID之和

#!/bin/bash
S_O=head /etc/passwd | tail -1 | cut -d: -f3
S_T=head -20 /etc/passwd | tail -1 | cut -d: -f3
echo -e "/etc/passwd 文件中的第10个和第20个用户的ID之和为:\\e[1;33m$[${S_O}+${S_T}]\\e[0m"

2.编写脚本 sumspace.sh,传递两个⽂件路径作为参数给脚本,计算这两个⽂件中所有空⽩⾏之和

#!/bin/bash
read -p "please input One path : " ONE_PATH
read -p "please input Two path : " TWO_PATH
S_ONE=grep ^$ ${ONE_PATH} |wc -l
S_TWO=grep ^$ ${TWO_PATH} |wc -l
echo "file spaces is : ${S_ONE} ${S_TWO}"
sum=$[${S_ONE}+${S_TWO}]
echo -e "${ONE_PATH}、${TWO_PATH} space sum is \\e[31m${sum}\\e[0m"

3.编写脚本 sumfile.sh,统计/etc, /var, /usr⽬录中共有多少个⼀级⼦⽬录和⽂件

#!/bin/bash
read -p "please input first_dir_path : " FIRST
read -p "please input second_dir_path : " SECOND
read -p "please input three_dir_path : " THREE
FIR_F=find ${FIRST} -maxdepth 1 | wc -l
SEC_F=find ${SECOND} -maxdepth 1 | wc -l
THR_F=find ${THREE} -maxdepth 1 | wc -l
FIR_S=find ${FIRST} -maxdepth 1 -type d | wc -l
SEC_S=find ${SECOND} -maxdepth 1 -type d | wc -l
THR_S=find ${THREE} -maxdepth 1 -type d | wc -l
fir_dir=$[${FIR_S}+${SEC_S}+${THR_S}-3]
echo -e "\\e[1;33m${FIRST}、${SECOND}、${THREE}\\e[0m three first dirs sum : \\e[1;31m${fir_dir}\\e[0m"
SUM=$[${FIR_F}+${SEC_F}+${THR_F}]
echo -e "\\e[1;33m${FIRST}、${SECOND}、${THREE}\\e[0m three files sum : \\e[1;31m$[${SUM}-${fir_dir}]\\e[0m"

4.编写脚本 argsnum.sh,接受⼀个⽂件路径作为参数;如果参数个数⼩于1,则提⽰⽤户“⾄少应该给⼀个参数”,并⽴即退出;如果参数个数不⼩于1,则显⽰第⼀个参数所指向的⽂件中的空⽩⾏数

#!/bin/bash
[[ $# -lt 1 ]] && (echo "at least one argument"&&exit)|| echo -e "\\e[1;31m$1\\e[0m 文件空白行数为:\\e[1;33mgrep \'^$\' $1|wc -l\\e[0m"

5.编写脚本 hostping.sh,接受⼀个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提⽰⽤户“该IP地址可访问” ;如果不可ping通,则提⽰⽤户“该IP地址不可访问”

#!/bin/bash
read -p "please input IP:" IP
LOG_COLOR=\'\\e[1;31m\'
AND_COLOR=\'\\e[0m\'
A=[[ "$IP" =~ ^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$ ]];echo $?;
B=ping -c1 -w1 $IP &>/dev/null;echo $?;
if [ $A -eq 0 ];then
if [ $B -eq 0 ];then
echo -e "${LOG_COLOR}该IP地址可以访问${AND_COLOR}"
else
echo -e "${LOG_COLOR}该IP地址不可访问${AND_COLOR}"
fi
else
echo -e "${LOG_COLOR}IP 格式输入不正确${AND_COLOR}"
exit
fi

6.编写脚本 checkdisk.sh,检查磁盘分区空间和inode使⽤率,如果超过80%,就发⼴播警告空间将满

#!/bin/bash
disk=df|egrep /dev/sd|tr -s \' \' \'%\'|cut -d% -f5|sort -nr|head -n1
inode=df -i|egrep /dev/sd|tr -s \' \' \'%\'|cut -d% -f5|sort -nr|head -n1
[ $disk -ge 80 -o $inode -ge 80 ] && wall space will full.

7.编写脚本 per.sh,判断当前⽤户对指定参数⽂件,是否不可读并且不可写

#!/bin/bash
[ ! -r $1 -a ! -w $1 ] && echo "$1 is not read and write" || exit

8.编写脚本 excute.sh ,判断参数⽂件是否为sh后缀的普通⽂件,如果是,添加所有⼈可执⾏权限,否则提⽰⽤户⾮脚本⽂件

#!/bin/bash
[[ $1 =~ .sh$ ]] && [ -f $1 ] && (chmod a+x $1;echo "$1 is .sh")||echo "$1 is not
.sh"

9.编写脚本 nologin.sh 和 login.sh,实现禁⽌和允许普通⽤户登录系统

#!/bin/bash
[ -f /etc/nologin ] && echo “nologin”|| (touch /etc/nologin;echo “nologin”)
!/bin/bash
[ -f /etc/nologin ] && ( rm -f /etc/nologin;echo “login”)||echo “login”

10.编写脚本 createuser.sh,实现如下功能:使⽤⼀个⽤户名做为参数,如果指定参数的⽤户存在,就显⽰其存在,否则添加之;显⽰添加的⽤户的id号等信息

#!/bin/bash
read -p "please input username:" user
id $user &>/dev/null
[[ ! $? -eq 0 ]] && (useradd $user &> /dev/null && echo "add $user user") ||echo "the user is exits"

11.编写脚本 yesorno.sh,提⽰⽤户输⼊yes或no,并判断⽤户输⼊的是yes还是no,或是其它信息

#!/bin/bash
read -p "please input yes or no :" ANS
[[ "$ANS" =~ ^(Yy?)$ ]] && echo yes
[[ "$ANS" =~ ^(Nn?)$ ]] && echo no

12.编写脚本 filetype.sh,判断⽤户输⼊⽂件路径,显⽰其⽂件类型(普通,⽬录,链接,其它⽂件类型)

#!/bin/bash
read -p "please input file path: " PT
file $PT >/testdir/shell/test.txt
leixing=egrep -o link|text|block|directory /testdir/shell/test.txt
case $leixing in
text)
echo "the path is file"
;;
link)
echo "the path is Link"
;;
block)
echo "the path is Block"
;;
directory)
echo "the path is Directory"
;;
*)
echo "the path is Others"
esac

13.编写脚本 checkint.sh,判断⽤户输⼊的参数是否为正整数

#!/bin/bash
read -p "please input a number:" num
[[ $num =~ ^[0-9]+$ ]] && echo “$num is int”||{echo ”lease input a number”;exit;}

14.让所有⽤户的PATH环境变量的值多出⼀个路径,例如
/usr/local/apache/bin

vim /etc/profile
export PATH=/usr/local/apache/bin:$PATH
source /etc/profile 生效

15.⽤户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@CentOS ~]# vim ~/.bashrc
alias rm=\'rm –i\'
alias cdnet=\'cd /etc/sysconfig/network-scripts/\'
alias editnet1=\'vim /etc/sysconfig/network-scripts/ifcfg-eth0\'
alias editnet2=\'vim /etc/sysconfig/network-scripts/ifcfg-ens33\'
export PS1=\'[\\e[1;31m][\\u@\\h \\W]\\$[\\e[0m]\' 红色
source ~.bashrc 生效

16.任意⽤户登录系统时,显⽰红⾊字体的警⽰提醒信息“Hi,dangerous!”

[root@CentOS ~]# vim /etc/issue
^[[031m " Hi,dangerous!" ^[[0m"

17.编写⽣成脚本基本格式的脚本,包括作者,联系⽅式,版本,时间,描述等

[root@CentOS ~]# vim .vimrc
set nu
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: w")
call setline(5,"#QQ: 1")
call setline(6,"#Date: ".strftime("%Y-%m-%d"))
call setline(7,"#FileName: ".expand("%"))
call setline(8,"#URL: http://www.magedu.com")
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

18.编写脚本 systeminfo.sh,显⽰当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本, CPU型号,内存⼤⼩,硬盘⼤⼩

#!/bin/bash
LOG_COLOR=\'\\e[1;33m\'
AND_COLOR=\'\\e[0m\'
echo -e "当前主机信息如下:"
echo -e "主机名:${LOG_COLOR}${HOSTNAME}${AND_COLOR}"
echo -e "IPV4 地址为:${LOG_COLOR}ifconfig | grep broadcast | tr -s " " | cut -d" " -f3"${AND_COLOR}
echo -e "操作系统版本为:${LOG_COLOR}uname -r"${AND_COLOR}
echo -e "内核版本为:${LOG_COLOR}cat /etc/redhat-release | cut -d"." -f1-2"${AND_COLOR}
echo -e "CPU型号为:${LOG_COLOR}lscpu | grep Model | tail -1 | tr -s \' \' | cut -d: -f2"${AND_COLOR}
echo -e "内存大小为:${LOG_COLOR}free -mh | head -2 | tail -1 | tr -s \' \' | cut -d" " -f2"${AND_COLOR}
echo -e "硬盘容量为:${LOG_COLOR}fdisk -l | head -2 | tail -1 | awk -F \':|,\' \'{print $2}\'"${AND_COLOR}

19.编写脚本 backup.sh,可实现每⽇将/etc/⽬录备份到/root/etcYYYY-mm-dd中

#!/bin/bash
cp /etc /testdir/etcdate +%F
echo "/etc 备份完成"

20.编写脚本 disk.sh,显⽰当前硬盘分区中空间利⽤率最⼤的值

#!/bin/bash
echo "当前硬盘分区中空间利用率最大的值为:df |grep /dev/sd |grep -o [0-9]{1,3}% | sort -rn | head -1"

21.编写脚本 links.sh,显⽰正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从⼤到⼩排序

#!/bin/bash
echo "正在连接本主机的每个远程主机的 IPV4 的地址和连接数为:"
echo "netstat -tan | tr -s " " : | cut -d: -f6 | grep ^[0-9] | sort | uniq -c"

22.编写脚本 reset.sh,对新系统进行环境配置

#!/bin/bash
#字体颜色
LOG_COLOR=\'\\e[1;33m\'
AND_COLOR=\'\\e[0m\'
#网卡名
N_N=ifconfig|head -1 | tr -s : |cut -d: -f1
#网卡配置文件所在目录
D_NET="/etc/sysconfig/network-scripts/ifcfg-${N_N}"
#yum源配置文件所在目录
D_YUM="/etc/yum.repos.d/"
#关闭seliunx
setenforce 0 &>/dev/null
sed -i \'s#^SELINUX=.#SELINUX=disabled#g\' /etc/selinux/config
echo -e "${LOG_COLOR}SELINUX已经修改完毕,重启即生效。${AND_COLOR}"
read -p "即将修改主机名,请给主机起一个可爱的名字吧:" HOST
#更改主机名
hostname ${HOST}
cat >/etc/hostname <<EOF
${HOST}
EOF
echo -e "${LOG_COLOR}名ok,接下来是yum配置${AND_COLOR}"
#配置yum源
mkdir /media/cdrom -p &>/dev/null
mount /dev/sr0 /media/cdrom &>/dev/null
mkdir ${D_YUM}backup &>/dev/null
mv ${D_YUM}C ${D_YUM}backup
cat >>${D_YUM}CentOS.repo <<EOF
[CentOS]
name=CentOS
baseurl=file:///media/cdrom
gpgcheck=0
EOF
echo \'/dev/sr0 /media/cdrom iso9660 defaults 0 0\' >>/etc/fstab
yum clean all &>/dev/null
yum makecache &>/dev/null
echo -e "${LOG_COLOR}yum配置完成。${AND_COLOR}"
#关闭防火墙
systemctl stop firewalld.service &>/dev/null
systemctl disable firewalld.service &>/dev/null
echo -e "${LOG_COLOR}防火墙已经关闭,并禁止开机启动${AND_COLOR}"
#更改IP
read -p "接下来配置IP\\n请键入IP地址:" IPAD
read -p "请键入子网掩码:" NET
read -p "请键入网关地址:" GAT
sed -i \'s#^BOOT.*#BOOTPROTO=static#g\' ${D_NET}
cat >>${D_NET} <<EOF
IPADDR=${IPAD}
NETMASK=${NET}
GATEWAY=${GAT}
EOF
systemctl restart network.service &>/dev/null
echo -e "${LOG_COLOR}网卡配置已生效${AND_COLOR}"
sleep 1
echo -e "${LOG_COLOR}主机环境配置完成,系统自动重启 3...${AND_COLOR}"
sleep 1
echo -e "${LOG_COLOR}2..${AND_COLOR}"
sleep 1
echo -e "${LOG_COLOR}1...${AND_COLOR}"
sleep 1
reboot

新网虚拟主机