bash小小小脚本

简介:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@centos6 scripts] # cat systeminfo.sh
#!/bin/bash
HostName=` uname  -n`
Ipv4=` ifconfig  | sed  -nr  '2s#.*addr:(.*)  Bca.*$#\1#gp' `
Cpu=`lscpu| sed  -n  '13p' | tr  -s  ' ' `
Mem=` free  -hm | grep  "Mem" | tr  -s  ' '  ':' | cut  -d: -f2`
Disk=` fdisk  -l | grep  '/dev/sda:' | sed  -r  's#.*: (.*),.*$#\1#g' `
  
  
  
echo  the  hostname  is: $HostName
echo  the server_ip is: $Ipv4
echo  the opeartion system is: $( cat  /etc/redhat-release )
echo  the kernel verison is: $( uname  -r)
echo  the cpuinfo is: $Cpu
echo  the memery size is: $Mem
echo  the Hardisk size is: $Disk
[root@centos6 scripts] # sh systeminfo.sh
the  hostname  is: centos6.localdomain
the server_ip is: 10.1.249.94
the opeartion system is: CentOS release 6.8 (Final)
the kernel verison is: 2.6.32-642.el6.x86_64
the cpuinfo is: Model name: AMD A8-4500M APU with Radeon(tm) HD Graphics
the memery size is: 980M
the Hardisk size is: 128.8 GB
[root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
8
9
10
[root@centos6 scripts] # cat backup.sh
#!/bin/bash
backup_dir= "/tmp/etc-$(date +%F)"
cp  -aR  /etc/  $backup_dir &&  echo  "the $backup_dir is backup successfull"
[root@centos6 scripts] # sh backup.sh
the  /tmp/etc-2016-08-11  is backup successfull
[root@centos6 scripts] # ll /tmp/
总用量 40
drwxr-xr-x. 122 root root 12288 8月  11 19:03 etc-2016-08-11
  [root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
[root@centos6 scripts] # cat disk.sh
#!/bin/bash
MaxUse=` df  | grep  "/dev/sda" | sed  -r  's#.* ([0-9]+%).*#\1#g' | sort  -nr| head  -1`
echo  "当前硬盘分区中空间利用率最大的值为:$MaxUse"
[root@centos6 scripts] # sh disk.sh
当前硬盘分区中空间利用率最大的值为:22%
[root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
[root@centos6 scripts] # cat links.sh
#!/bin/bash
Links=` netstat  -tn | grep  tcp | tr  -s  ' '  ':' | cut  -d: -f6| sort  -nr| uniq  -c`
echo  -e  "主机的每个远程主机的IPv4连接数和地址为:$Links"
[root@centos6 scripts] # sh links.sh
主机的每个远程主机的IPv4连接数和地址为:      2 10.1.250.79
[root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
8
9
[root@centos6 scripts] # cat sumid.sh
#!/bin/bash
uuid1=` cat  /etc/passwd | sed  -n  '10p' | cut  -d: -f3`
uuid2=` cat  /etc/passwd | sed  -n  '20p' | cut  -d: -f3`
sum =$(($uuid1+$uuid2))
echo  "第10个用户和第20用户的ID之和为:$sum"
[root@centos6 scripts] # sh sumid.sh
第10个用户和第20用户的ID之和为:180
[root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@centos6 scripts] # cat sumspace.sh
#!/bin/bash
[ $ # -lt 2 ] && echo "please input two path:" && exit 1
  
Args1=` grep  "^$"  $1 | wc  -l`
Args2=` grep  "^$"  $2 | wc  -l`
  
let  sumspace=$Args1+$Args2
  
echo  "这两个文件中所有空白行之和为:$sumspace"
[root@centos6 scripts] # sh sumspace.sh  /etc/issue /etc/issue
这两个文件中所有空白行之和为:2
[root@centos6 scripts] # sh sumspace.sh
please input two path:
[root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@centos6 scripts] # sh sumfile.sh
please input at least three  dir :
[root@centos6 scripts] # sh sumfile.sh /etc /var /usr
/etc , /var , /usr 共有一级子目录和文件292个
[root@centos6 scripts] # cat  sumfile.sh
#!/bin/bash
[ $ # -lt 3 ] && echo "please input at least three dir:" && exit 1
  
total1=` ls  -A $1 | wc  -l`
total2=` ls  -A $2 | wc  -l`
total3=` ls  -A $3 | wc  -l`
  
sumfile=$(($total1+$total2+$total3))
echo  "$1,$2,$3共有一级子目录和文件$sumfile个"
[root@centos6 scripts] #

 

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

1
2
3
4
5
6
7
8
9
[root@centos6 scripts] # sh argsum.sh
at least input one args:
[root@centos6 scripts] # sh argsum.sh /etc/issue
the args you input has 1  lines space
[root@centos6 scripts] # cat argsum.sh
#!/bin/bash
  
[ $ # -lt 1 ] && echo "at least input one args:" && exit 1 || echo "the args you input has `grep "^$" $1|wc -l`  lines space"
[root@centos6 scripts] #


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

1
2
3
4
5
6
7
8
9
10
11
12
[root@centos6 scripts] # sh hostping.sh
please input one ip address:
[root@centos6 scripts] # sh hostping.sh  10.1.249.94
you can access the website!
[root@centos6 scripts] # sh hostping.sh  10.1.249.93
the ip is not unreachable
[root@centos6 scripts] # cat hostping.sh
#!/bin/bash
[ $ # -lt 1 ] && echo "please input one ip address:" && exit 1
ping  -c1 -W1 $1 &> /dev/null   &&  echo  "you can access the website!"  ||  echo  "the ip is not unreachable"
  
[root@centos6 scripts] #

本文转自chengong1013 51CTO博客,原文链接:http://blog.51cto.com/purify/1837417,如需转载请自行联系原作者

相关文章
|
4月前
|
监控 安全 Shell
防止员工泄密的措施:在Linux环境下使用Bash脚本实现日志监控
在Linux环境下,为防止员工泄密,本文提出使用Bash脚本进行日志监控。脚本会定期检查系统日志文件,搜索敏感关键词(如"password"、"confidential"、"secret"),并将匹配项记录到临时日志文件。当检测到可疑活动时,脚本通过curl自动将数据POST到公司内部网站进行分析处理,增强信息安全防护。
163 0
|
4月前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
95 0
Linux Bash 脚本中的 IFS 是什么?
|
28天前
|
Shell
一个能够生成 Markdown 表格的 Bash 脚本
【8月更文挑战第20天】这是一个使用Bash脚本生成Markdown表格的示例。脚本首先设置表头与内容数据,然后输出Markdown格式的表格。用户可以根据需要自定义表格内容。使用时,只需将脚本保存为文件(如 `generate_table.sh`),赋予执行权限,并运行它,即可在终端看到生成的Markdown表格。
|
27天前
|
Unix Shell Linux
在Linux中,什么是Bash脚本,并且如何使用它。
在Linux中,什么是Bash脚本,并且如何使用它。
|
29天前
|
Shell 开发者
深入理解Bash脚本中的函数
【8月更文挑战第20天】
14 0
|
29天前
|
存储 Shell 数据处理
深入探讨Bash脚本中的数组
【8月更文挑战第20天】
11 0
|
1月前
|
存储 Shell
Bash 脚本中的 `hash` 命令
【8月更文挑战第19天】
9 0
|
3月前
|
Unix Shell Linux
技术经验分享:Bash脚本命令使用详解
技术经验分享:Bash脚本命令使用详解
28 0
|
4月前
|
存储 弹性计算 运维
用bash脚本创建目录
【4月更文挑战第29天】
35 3
|
4月前
|
存储 Unix Shell
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
【简化Cmake编译过程 】编写通用的bash脚本:简化和构建cmake高效自动化任务
174 0