设为首页 收藏本站
查看: 1018|回复: 0

简单shell脚本练习

[复制链接]

尚未签到

发表于 2018-8-27 11:28:58 | 显示全部楼层 |阅读模式
  1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。
#!/bin/bash  
echo Hostname: `hostname`
  
echo IP address: `ifconfig|egrep -o "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}"|head -n1`
  
echo System version: `cat /etc/system-release`
  
echo kernel version: `uname -r`
  
echo CPU type : `lscpu |grep 'Model name'|sed 's/.*://'|sed 's/^[[:space:]]\+//'`
  
echo Disk size :`fdisk -l|grep "sda:"|sed 's/.*://'|sed 's/,.*//'|sed 's/^[[:space:]]\+//'`
  
echo Memory size : `free -m|grep "Mem"|tr -s [[:space:]]|cut -d" " -f2` MB
  运行结果
[root@shao ~]# bash systeminfo.sh  
Hostname: shao
  
IP address: 10.1.53.2
  
System version: CentOS Linux release 7.2.1511 (Core)
  
kernel version: 3.10.0-327.el7.x86_64
  
CPU type : Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz
  
Disk size :214.7 GB
  
Memory size : 1824 MB
  2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#!/bin/bash  
cp -arv /etc /root/etc`date +%F`
  3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash  
echo "The max using rate is:`df |grep "/dev/sd"| cut -c 44-46 | sort -nr | head -1`%"
  运行结果
[root@shao ~]# bash disk.sh  
The max using rate is: 74%
  4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
#!/bin/bash  
echo "the links number is:"
  
netstat -nt |tr -s ' '  |cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr
  运行结果
[root@shao ~]# bash links.sh  
the links number is:
  
      2 10.1.53.3
  5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#!/bin/bash  
echo 'The sum is'
  
id1=`cat /etc/passwd | sed -n '10p' | cut -d: -f3`
  
id2=`cat /etc/passwd | sed -n '20p' | cut -d: -f3`
  
let idsum=$id1+$id2
  
echo "The sum is:$idsum"
  
unset idsum
  运行结果
[root@shao bin]# bash sumid.sh  
The sum is:70
  6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
#!/bin/bash  
file1_blank=`grep "^[[:space:]]*$" $1 | wc -l`
  
file2_blank=`grep "^[[:space:]]*$" $2 | wc -l`
  
sumspace=$[$file1_blank+$file2_blank]
  
echo "The tatal blank line:$sumspace"
  运行结果
[root@shao ~]# bash sumspace.sh /etc/passwd /etc/fstab  
The tatal blank line:1
  7、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bash  
etcnum=`ls -d /etc/*|wc -l`
  
varnum=`ls -d /var/*|wc -l`
  
usrnum=`ls -d /usr/*|wc -l`
  
echo "the totalfile is $[etcnum+varnum+usrnum]"
  运行结果
[root@shao bin]# bash sumfile.sh  
the totalfile is 298
  8、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
#!/bin/bash  
read -p "Enter a filename:" file
  
[ -z "$file" ] && echo "please enter a file name" && exit 2
  
echo -n "The  blank line number is:"
  
echo `grep -c  '^[[:space:]]*$'  $file`
  运行结果
[root@shao ~]# bash sumspace.sh  
Enter a filename:
  
please enter a file name
  
[root@shao ~]# bash sumspace.sh
  
Enter a filename:/etc/fstab
  
The  blank line number is:1
  9、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash  
[ $# -ne 1 ] && echo "please input one ipaddr" && exit 2
  
ping -c1 -w1 $1 &> /dev/null && echo "ping successful" || echo "ping failed"
  运行结果
[root@shao bin]# bash hostping.sh  
please input one ipaddr
  
[root@shao bin]# bash hostping.sh 10.1.254.254
  
ping successful
  10、判断硬盘的每个分区空间和inode的利用率是否大于80,如果是,发邮件通知root磁盘满
#!/bin/bash  
mount=$((df;df -i) | grep "/dev/sd" | tr -s ' ' | cut -d' ' -f5 | grep -o "[[:digit:]]\+" | sort | tail -1)
  
[ "$mount" -ge 80 ] && echo "disk almost full" | mail -s "dangerous" root || exit 3
  11、指定文件做为参数,判断文件是否为.sh后缀,如果是,添加x权限
#!/bin/bash  
read -p "please input a test file: " FILENAME
  
[ ! -f $FILENAME ] && echo "please input a real file" && exit 100
  
echo $FILENAME | grep -q '.*\.sh$'  && (chmod +x $FILENAME ; echo "Add x right for the file successfully") || echo "this is not a .sh file"
  运行结果
[root@shao bin]# ls  
excute.sh  hostping.sh  per.sh  sumfile.sh
  
[root@shao bin]# bash excute.sh
  
please input a test file: hostping.sh
  
Add x right for the file successfully
  12、判断输入的IP是否为合法IP
#!/bin/bash  
read -p "please input one useful ip:" ip_addr
  
echo $ip_addr | grep -E "^(\.){3}\$" &> /dev/null  && echo "this is a useful ip" || echo "this is not a useful ip"
  运行结果
[root@shao bin]# bash checkip.sh  
please input one useful ip:10.1.534.1
  
this is not a useful ip
  
[root@shao bin]# bash checkip.sh
  
please input one useful ip:10.1.53.1
  
this is a useful ip
  13、计算1+2+3+...+100
#!/bin/bash  
#!/bin/bash
  
echo -e "\033[34m1-100 all positive integer's sum\033[0m : `echo {1..100} | tr " " "+" | bc`"
  运行结果
[root@shao bin]# bash 100sum.sh  
1-100 all positive integer's sum : 5050
  方法二:seq -s + 1 100 | bc
  14、输入起始值A和最后值B,计算从A+(A+1)...+(B-1)+B的总和
#!/bin/bash  
read -p "first number:" a
  
read -p "second number:" b
  
[ $a -ge $b ] && echo "sum is `seq -s+ $b $a | bc`" || echo "sum is `seq -s+ $a $b | bc`"
  运行结果
[root@shao bin]# bash sumAB.sh  
first number:4
  
second number:8
  
sum is 30
  
[root@shao bin]# bash sumAB.sh
  
first number:100
  
second number:1
  
sum is 5050
  15、chmod -rw /tmp/file1,编写脚本/root/bin/per.sh,判断当前用户对/tmp/file1文件 是否不可读且不可写
#!/bin/bash  
[ -r /tmp/file1 -o  -w /tmp/file1 ] && echo "`whoami` can read or write /tmp/file1" || echo  "`whoami` can not read and can not write /tmp/file1"
  运行结果
[root@shao bin]# bash per.sh  
root can read or write /tmp/file1
  16、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统。
  禁止
#!/bin/bash  
[ -e /etc/nologin ] && echo "the normal user can not login already" ; exit || touch /etc/nologin
  
echo "disable normal user login "
  允许
#!/bin/bash  
[ -e /etc/nologin ] && rm -f /etc/nologin
  
echo "enable user login "
  17、写一个脚本/root/bin/createuser.sh,实现如下功能: 使用一个用户名做为参数,如果指定参数的用户存在,就显 示其存在,否则添加之;显示添加的用户的id号等信息
#!/bin/bash  
read -p "please input you name:" name
  
if cat /etc/passwd | cut -d: -f1 | grep -q "^$name$";then
  
    echo "the username already exits"
  
else
  
    useradd $name
  
    cat /etc/passwd | grep "^$name\b"
  
fi
  运行结果
[root@shao bin]# bash createuser.sh  
please input you name:shao
  
the username already exits
  
[root@shao bin]# bash createuser.sh
  
please input you name:liu
  
liu:x:1014:1019::/home/liu:/bin/bash
  18、写一个脚本/root/bin/yesorno.sh,提示用户输入yes或 no,并判断用户输入的是yes还是no,或是其它信息
#!/bin/bash  
[ $# -ne 1 ] && echo "please input an arg" && exit
  
ans=`echo $1 | tr 'A-Z' 'a-z'`
  
if [ $ans = yes -o $ans = y ]
  
then
  
    echo "yes"
  
elif [ $ans = n -o $ans = no ]
  
then
  
    echo "no"
  
else
  
    echo "error"
  
fi
  运行结果
[root@shao bin]# bash yesorno.sh y  
yes
  
[root@shao bin]# bash yesorno.sh
  
please input an arg
  
[root@shao bin]# bash yesorno.sh No
  
no
  
[root@shao bin]# bash yesorno.sh YES
  
yes
  方法二
#!/bin/bash  
read -p "Please input yes or no:" ans
  
case $ans in
  
[yY]|[yY][eE][Ss])
  
        echo "The user input yes"
  
        ;;
  
[nN]|[Nn][oO])
  
        echo "The user input no"
  
        ;;
  
*)
  
        echo "The user input other words"
  
esac
  19、写一个脚本/root/bin/filetype.sh,判断用户输入文件路 径,显示其文件类型(普通,目录,链接,其它文件类型)
#!/bin/bash  
read -p "please input a filename:" filename
  
! [ -e $filename ] && echo "The filename do not exist" && exit
  
if  [ -h $filename ] ;then
  
    echo "filetype is  syslink file"
  
elif [ -d $filename ] ;then
  
    echo "filetype is dirction"
  
elif [ -f $filename ] ;then
  
    echo "filetype is common file"
  
else
  
    echo "filetype is other"
  
fi
  运行结果
[root@shao bin]# bash filetype.sh  
please input a filename:/etc
  
filetype is dirction
  
[root@shao bin]# bash filetype.sh
  
please input a filename:/etc/redhhat-release
  
The filename do not exist
  
[root@shao bin]# bash filetype.sh
  
please input a filename:/etc/redhat-release
  
filetype is  syslink file
  
[root@shao bin]# bash filetype.sh
  
please input a filename:/etc/issue
  
filetype is common file
  20、写一个脚本/root/bin/checkint.sh,判断用户输入的参数是否为正整数
#!/bin/bash  
[ $# -ne 1 ] && echo "Please input a number" && exit
  
if [[ $1 =~ ^0*[1-9][0-9]*$ ]]
  
then
  
    echo "int"
  
else
  
    echo "not int"
  
fi
  运行结果
[root@shao bin]# bash  checkint.sh  
Please input a number
  
[root@shao bin]# bash  checkint.sh 0
  
not int
  
[root@shao bin]# bash  checkint.sh 12
  
int
  
[root@shao bin]# bash  checkint.sh 1.1
  
not int



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-557261-1-1.html 上篇帖子: 代码函数名提取与shell 回顾 下篇帖子: Linux 的shell脚本编程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表