SHELL脚本练习

发布时间:2025-01-06 点击:52
系统运维
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%%uff0c就发?播警告空间将满
#!/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,判断?户输件路径,显?其?件类型(普

电脑中的IE浏览器无法上传/下载文件如何解决
搞定小屏!5大黄金准则帮你设计移动端邮件
云服务器怎么雷电
.com域名和.net域名有什么区别 注册.com好还是.net好
这个空间过期续费的现在已经续费了帮忙打开一下吧
SEO优化?中,新站如何快速提升百度收录
一名成熟的SEO优化主管,除了专业技术外还需要哪些情商
阿里云怎么看自己的服务器续费