bash小腳本

1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。

[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中

[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,顯示當前硬盤分區中空間利用率最大的值

[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地址和連接數,并按連接數從大到小排序

[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之和

[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,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和

[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目錄中共有多少個一級子目錄和文件

[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,則顯示第一個參數所指向的文件中的空白行數

[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地址不可訪問”

[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]#

本章總結blog地址:http://purify.blog.51cto.com/10572011/1836818

原創文章,作者:alren,如若轉載,請注明出處:http://www.www58058.com/33831

(0)
alrenalren
上一篇 2016-08-15
下一篇 2016-08-15

相關推薦

  • Linux用戶和組的配置相關文件

    Linux用戶和組的配置相關文件     在linux下,用戶的相關配置文件一般是放在/etc目錄下,此文主要對以下幾個配置文件作介紹:/etc/passwd;/etc/shadow;/etc/group;/etc/gpasswd   一、/etc/passwd:此目錄下放的是用戶的屬性信息,包括組名、UID、GID等,它格式固…

    Linux干貨 2016-10-30
  • N23-卡卡琦-第一周

    1、描述計算機的組成及其功能 硬件: 控制器:是整個計算機的中樞神經,其功能是對程序規定的控制信息進行解釋,根據其要求進行控制,調度程序、數據、地址,協調計算機各部分工作及內存與外設的訪問等;運算器:運算器的功能是對數據進行各種算術運算和邏輯運算,即對數據進行加工處理;存儲器:存儲器的功能是存儲程序、數據和各種信號、命令等信息,并在需要時提供這些信息;輸入輸…

    Linux干貨 2016-11-16
  • 網絡管理

    網絡管理 本章內容 網絡概念 OSI模型 網絡設備 TCP/IP IP地址 配置網絡 實現網絡組 測試網絡 網絡工具 為linux網卡配置ip地址,不是給網卡配置地址,是給內核的網絡功能配置,地址是屬于內核。為內核配置即時生效,修改配置文件,是永久生效。ifconfig 配置信息,會立即生效,但是重啟網絡服務或主機,都失效。網絡服務/etc/init.d/n…

    Linux干貨 2016-09-05
  • 20160809作業

    一、如何設置tab縮進為4個字符       臨時設置:set tabstop=4       全局設置vim /etc/vimrc—>添加set tabstop=4       當前用戶生效,手動添加文…

    Linux干貨 2016-08-10
  • mount掛載

    mount 1 掛載mount                                               &nb…

    Linux干貨 2017-04-24
  • 馬哥教育網絡班20期+第6周課程練習

    請詳細總結vim編輯器的使用并完成以下練習題 vim三種模式: 用法:vim [option…] FILE 編輯模式:     字符跳轉:         h:向左移動一個字符     &…

    系統運維 2016-07-22
欧美性久久久久