shell 脚本之基础篇作业
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')"
# bash -n systeminfometion.sh
# 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
# bash -n systeminfometion.sh
# 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
# 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 '' |sort -nr |head -1)
echo "The diskused_max is $disk_max"
unset disk_max
# 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 '' |sed 's/:.*//' |sort |uniq -c |sort
# 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
# 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
# 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
# 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]