1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版 本,内核版本,CPU型号,内存大小,硬盘大小。
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小
echo "The hosname is: $(hostname)"
echo "The kernel version is: $(uname -r)"
echo "The server_ip is: $( ifconfig |grep 'inet\b' |grep -v '127.0.0.1' |sed 's/.*addr:\b//' |sed 's/Bcast.*//')"
echo "The CPU is: $(lscpu |grep -i 'model name' |tr -s ' ' |sed 's/.*://')"
echo "The OS version is: $(cat /etc/redhat-release)"
echo "The memorysize is: $(free |sed -n '2p' |tr : ' ' |tr -s ' ' |cut -d' ' -f2)"
echo "The disksize is: $(fdisk -l |sed -n '2p')"
[root@localhost bin]# bash -n systeminfometion.sh
[root@localhost bin]# bash systeminfometion.sh
The hosname is: localhost.localdomain
The kernel version is: 2.6.32-642.el6.x86_64
The server_ip is: 10.1.253.35
The CPU is: Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
The OS version is: CentOS release 6.8 (Final)
The memorysize is: 1004108
The disksize is: Disk /dev/sda: 128.8 GB, 128849018880 bytes
[root@localhost bin]# bash -n systeminfometion.sh
[root@localhost bin]# vim systeminfometion.sh
You have new mail in /var/spool/mail/root
2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
backdir="/root/etc$(date +%F)"
echo $backdir
cp -a /etc/. $backdir && echo "backup finished"
unset backdir
[root@localhost ~]# ll
drwxr-xr-x. 75 root root 4096 Aug 12 05:11 etc2016-08-11
3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 显示当前硬盘分区中空间利用率最大的值
disk_max=$(df |tr -s ' ' % |cut -d% -f5 |grep '[0-9]' |sort -nr |head -1)
echo "The diskused_max is $disk_max"
unset disk_max
[root@localhost bin]# bash disk.sh
The diskused_max is 53
4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数 从大到小排序
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数 从大到小排序
netstat -nt |tr -s ' ' |cut -d' ' -f5 |grep '[0-9]' |sed 's/:.*//' |sort |uniq -c |sort
[root@localhost bin]# bash link.sh
1 10.10.10.1
1 10.1.250.69
3 10.1.50.10
5、写一个脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 计算/etc/passwd文件中的第10个用户和第20用户的ID之和
user10_id=$(sed -n '10p' /etc/passwd |cut -d: -f3)
user20_id=$(sed -n '20p' /etc/passwd |cut -d: -f3)
sumid=$[$user10_id+$user20_id]
echo "The sum_id is: $sumid"
unset user10_id
unset user20_id
unset sumid
6、写一个脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空 白行之和
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
file1=$(grep '^$' $1 |wc -l)
file2=$(grep '^$' $2 |wc -l)
sumspace=$[$file1+$file2]
echo "The total spacelines is $sumspace"
unset file1
unset file2
[root@localhost bin]# bash sumspace.sh /etc/fstab /etc/profile
The total spacelines is 12
6、写一个脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 统计/etc, /var, /usr目录中共有多少个一级子目录和文件
etc_sum=$(ls -A /etc |wc -l)
var_sum=$(ls -A /var |wc -l)
usr_sum=$(ls -A /usr |wc -l)
sumfile=$[$etc_sum+$var_sum+$usr_sum]
echo "The total file is $sumfile"
unset etc_sum
unset var_sum
unset usr_sum
[root@localhost bin]# bash sumfile.sh
The total file is 208
7、写一个脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用 户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件 中的空白行数
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数
[ $# -lt 1 ] && echo "please give a argument" || grep '^$' $1 |wc -l
[root@localhost bin]# bash argsnum.sh /etc/fstab
1
8、写一个脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能 ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”
[ $# -ge 1 ] && echo "$1" |egrep -o '(\
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com