shell脚本(二)

1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名, IPv4地址,操作系统版本,内核版本, CPU型号,内存大小,硬盘大小

[root@centos7 scripts]# vim systeminfo.sh 

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-07
#FileName: systeminfo.sh
#***************************
echo "hostname: `hostname`"
echo "ip: `ifconfig | egrep -o "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>"|head -n1`"
echo `cat /etc/redhat-release `                                                                                                                                                            
uname -r
lscpu | grep "型号名称"
free -h | grep "Mem" | tr -s ' ' ':'|cut -d: -f2
lsblk | grep disk
~                        

##执行结果
[root@centos7 scripts]# ./systeminfo.sh 
hostname: centos7.cwj.com
ip: 192.168.239.132
CentOS Linux release 7.4.1708 (Core)
3.10.0-693.el7.x86_64
型号名称:        Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
1.8G
sda      8:0    0  200G  0 disk 

2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etc-YYYY-mm-dd中

vim /root/bin/backup.sh 
cp -a /etc /root/etc-`date +%F`      

3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

[root@centos7 scripts]# vim disk.sh

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-07
#FileName: disk.sh
#***************************
disk_per=`df -h | grep "^/dev" |tr -s ' ' '%' | cut -d % -f5| sort -nr|head -n1`
disk_name=`df -h | grep $disk_per | cut -d' ' -f1`
echo "$disk_name : $disk_per"  

[root@centos7 scripts]# ./disk.sh 
/dev/sr0 : 100

4、编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

[root@centos7 scripts]# vim link.sh

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-07
#FileName: link.sh
#***************************
netstat -tan | grep 'ESTAB' | tr -s ' ' ':' | cut -d: -f6 | sort | uniq -c | sort -nr

[root@centos7 scripts]# ./link.sh 
      2 192.168.239.1

5、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和

[root@centos7 scripts]# vim sumid.sh

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-08
#FileName: sumid.sh
#***************************
id_a=`cat /etc/passwd | head -n10 | tail -n1 | cut -d : -f3`
id_b=`cat /etc/passwd | head -n20 | tail -n1 | cut -d : -f3`
let sum=id_a+id_b
echo $sum  

6、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和

[root@centos7 scripts]# cat sumspace.sh 
#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-08
#FileName: sumspace.sh
#***************************
read -p "please input first file: " first_file
read -p "please input second file: " second_file
fisrt_file_space=`cat $first_file | grep "^[[:space:]]*$" | wc -l`
second_file_space=`cat $second_file | grep "^[[:space:]]*$" | wc -l`
let sum_space=$first_file_space+$second_file_space
echo $sum_space


##运行结果
[root@centos7 scripts]# ./sumspace.sh 
please input first file: /etc/passwd
please input second file: /etc/rsyncd.conf
4

另一种实现

[root@centos7 scripts]# vim sumspace2.sh 

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-08
#FileName: sumspace.sh
#***************************
#read -p "please input first file: " first_file
#read -p "please input second file: " second_file
fisrt_file_space=`cat $1 | grep "^[[:space:]]*$" | wc -l`
second_file_space=`cat $2 | grep "^[[:space:]]*$" | wc -l`                                                                                                                                 
let sum_space=$first_file_space+$second_file_space
echo $sum_space


[root@centos7 scripts]# ./sumspace2.sh /etc/passwd /etc/rsyncd.conf
4

7、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件

[root@centos7 scripts]# vim sumfile.sh

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-08
#FileName: sumfile.sh
#***************************
num1=`ls -A $1 | wc -l`
num2=`ls -A $2 | wc -l`
num3=`ls -A $3 | wc -l`
let sum=$num1+$num2+$num3
echo $sum                                                                                                                                                                                  
##运行结果
[root@centos7 scripts]# ./sumfile.sh /etc /var /usr
319
       

1、编写脚本/root/bin/argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

[root@centos7 scripts]# vim argsum.sh

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-09
#FileName: argsum.sh
#***************************
if [ $# -lt 1 ];then
    echo "please enter at least one parameter!!!"
    exit 1
else
   space_num=`egrep "^[[:space:]]*$" $1 | wc -l`                                                                                                                                           
   echo $space_num
fi

##运行结果
[root@centos7 scripts]# ./argsum.sh 
please enter at least one parameter!!!
[root@centos7 scripts]# ./argsum.sh /data/scripts/hellodb_innodb.sql 
34

2、编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

[root@centos7 scripts]# vim hostping.sh 

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-09
#FileName: hostping.sh
#***************************
[[ $1 =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] || { echo  "IP error";exit 1; }                                     
ping $1 -c1 -W2 &>/dev/null && echo "This address can be accessed" || echo "This address cannot be accessed"


##运行结果
[root@centos7 scripts]# ./hostping.sh 10.10.100.
IP error
[root@centos7 scripts]# ./hostping.sh 10.10.100.244
PING 10.10.100.244 (10.10.100.244) 56(84) bytes of data.

3、编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-09
#FileName: checkdisk.sh
#***************************
disk_per=`df |grep ^/dev/sd |tr -s " " ":"|cut -d: -f5 |cut -d% -f1 |sort -nr`
for i in $disk_per;do
    if [ $i -gt 80 ];then
        echo "DISK WARING!!!"
    else
        echo "Nothing"
    fi
done

##
[root@centos7 scripts]# ./checkdisk.sh 
Nothing
Nothing
Nothing

1、编写脚本/bin/per.sh,判断当前用户对指定的参数文件,是否不可读并且不可写

[root@centos7 scripts]# vim per.sh

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-09
#FileName: per.sh
#***************************
[ ! -r $1 -a ! -w $1 ] && echo "$1 not writeble"

##扩展实现

#!/bin/bash
#****************************
#Author chenwj
#Date: 2021-03-09
#FileName: per2.sh
#***************************
read -p "please input a file:" file
if [ -r $file ];then
    if [ -w $file ];then
        echo "$file not only readable but also writeable "
    else
       echo "$file is not writeable"
    fi
else
    echo "$file is not readable"
fi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值