初窺門徑之腳本書寫

一、幾個腳本的編寫:

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

#!/bin/bash
echo "HOSTNAME: $HOSTNAME"
echo "IPV4 ADDRESS: `ip a |grep "\<inet\>[ ].*brd"|tr -s " "|cut -d" " -f 3|tr '\n' ' '`"
echo "OS RELEASE: `cat /etc/centos-release`"
echo "CPU INFORMATION: `lscpu | grep "Model name"|tr -s " "|cut -d: -f2 |sed -r 's@[ ](.*)@\1@'`"
echo "KERNEL RELEASE:`uname -r`"
echo "MEMORY INFORMATION: ` cat /proc/meminfo|head -n 1|cut -d: -f 2|tr -d " "`"
echo "HARD DISK INFOMATION:"` fdisk -l |grep "Disk.*sd"|cut -d: -f2|cut -d ' ' -f2,3|tr -d ','`

測試運行:

HOSTNAME: centos7.localdomain
IPV4 ADDRESS: 10.1.255.177/16 192.168.122.1/24
OS RELEASE: CentOS Linux release 7.2.1511 (Core)
CPU INFORMATION: AMD E2-1800 APU with Radeon(tm) HD Graphics
KERNEL RELEASE:3.10.0-327.el7.x86_64
MEMORY INFORMATION: 1001336kB
HARD DISK INFOMATION:107.4 GB

2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd

#!/bin/bash
#author jack_cui
#version 1.0
#description: backup directory
read -p "please input backup directory: " backdir
read -p "please input destination directory: " desdir
cp -r $backdir $desdir/backup`date +%F`
echo "backup successful!"

測試運行:

[root@centos7 bin]# backup.sh
please input backup directory: /root/bin/
please input destination directory: /testdir/
backup successful!
[root@centos7 bin]# ls /testdir
backup2016-08-12  fs.txt  info.txt  info.txt.bak  info.txt.bak.backup  vimtu

3、編寫腳本/root/bin/disk.sh,顯示當前硬盤分區中空間利用率最大的值

[root@centos7 bin]# cat disk.sh
#!/bin/bash
#author:jack_cui
#description=show the system disk use
disk_use=`df |grep sd|tr -s " "|cut -d " " -f5|sed -n 's/%//p'|sort -rn|head -n 1`
echo $disk_use
unset disk_use

測試運行:

[root@centos7 bin]# bash disk.sh
63

4、編寫腳本/root/bin/links.sh,顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數從大到小排序

[root@centos7 bin]# cat link.sh
#!/bin/bash
#author:jackcui
#description: disk use infomation
netstat -nt |tr -s ' '|cut -d ' ' -f5 |cut -d: -f1 |grep [0-9]|sort |uniq -c|sort -nr
[root@centos7 bin]# link.sh
      5 10.1.1.88

5、寫一個腳本/root/bin/sumid.sh,計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和

#!/bin/bash
#author:jackcui
#description:sum two user's id
numbera=`sed -n '10p' /etc/passwd |cut -d: -f3`
numberb=`sed -n '20p' /etc/passwd |cut -d: -f3`
let suma_b=numbera+numberb
echo "user1 uig is $numbera"
echo "user2 uid is $numberb"
echo "two users id sum is $suma_b"
unset numberb  suma_b numbera
 [root@centos7 bin]# sumid.sh
user1 uig is 11
user2 uid is 59
two users id sum is 70

6、寫一個腳本/root/bin/sumspace.sh,傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和

兩個測試文本:test.txt test2.txt 中間有6個空白行或全是空白字符的行

 [root@centos7 ~]# cat test.txt
aaaaaaaaaaaaaaaaaaaaaaaa
 
bbbbbbbbbbbbbbbbbbbbbbbbbb
     ccccccccccccccccccc
 
     
[root@centos7 ~]# cat test2.txtt
cat: test2.txtt: No such file or directory
[root@centos7 ~]# cat test2.txt
cccccccccccccccccccccccc
 
bbbbbbbbbbbbbbbbbbbbbbbbbb
  
cccccccccccccccc

   腳本:

[root@centos7 bin]# cat sumspace.sh
#!/bin/bash
#author:jackcui
#description:calculate two files blank line
read -p "Input first path of files: " first_path
read -p "Input second path of files:" second_path
a=`cat $first_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
b=`cat $second_path |grep -e "^$" -e "[[:space:]]\+$" |wc -l`
let sum=a+b
echo "The total blank lines is: $sum"
unset a b sum

腳本運行:

 [root@centos7 bin]# sumspace.sh
Input first path of files: /root/test.txt
Input second path of files:/root/test2.txt
The total blank lines is: 6

7、寫一個腳本/root/bin/sumfile.sh,統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件

[root@centos7 bin]# cat sumfile.sh
#!/bin/bash
#author:jackcui
#description:sum directorys's files and directorys 
a=`ls -A /etc/|wc -l`
b=`ls -A /var/|wc -l`
c=`ls -A /usr/|wc -l`
let sumfile=a+b+c
echo "three directory have $sumfile files and directorys"
unset a b c sumfile

腳本運行:

[root@centos7 bin]# sumfile.sh
three directory have 296 files and directorys

8、寫一個腳本/root/bin/argsnum.sh,接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數

[root@centos7 bin]# cat argsnum.sh
#!/bin/bash
#author:jackcui
#description:dispaly file blank lines count
[ "$#" -lt 1 ]&& echo "You should give one parameter!" && exit
line=`cat $1|grep -c "^[[:space:]]*$" 2> /dev/null`
echo $line
unset line
exit
[root@centos7 bin]# argsnum.sh /root/test.txt
3
 [root@centos7 bin]# argsnum.sh //沒有參數時直接退出,并打印提示信息
You should give one parameter!

9、寫一個腳本/root/bin/hostping.sh,接受一個主機的IPv4地址做為參數,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”

[root@centos7 bin]# cat hostping.sh
#!/bin/bash
#author:jackcui
#description:Judge a ip address whether access
ping -W 1 -c3 $1 > /dev/null 2>&1   //指定ping超時時間1s,ping3次
suss=`echo $?`
[ $suss -eq 0 ] && echo "IP address can access!"|| echo "IP address can not access!"
[root@centos7 bin]# bash  hostping.sh 10.1.0.2 //ping 不存在的地址報錯
IP address can not access!
[root@centos7 bin]# bash  hostping.sh 10.1.0.1 //ping 存在的地址提示地址可以訪問
IP address can access!

10、判斷硬盤的每個分區空間和inode的利用率是否大于80,如果是,發郵件通知root磁盤滿

[root@centos7 bin]# cat diskAlarm.sh

#!/bin/bash
#author:jackcui
#description:show the disk use
diskuse=`df |grep 'sd'|tr -s " " |cut -d " " -f 5|sed -n 's/%//p'|sort -rn|head -n1`
inodeuse=` df -i|grep 'sd'|tr -s " "|cut -d" " -f 5|sed  -n 's/%//p'|sort -rn|head -n1`
#[ $diskuse -ge 80 ]||[ $inodeuse -ge 80 ]&&echo "Dear root, disk has used too much!!!"|mail -s "Disk alarm!" root
[ $diskuse -ge 80 -o $inodeuse -ge 80 ]&&echo  "Dear root, disk has used too much!!!"|mail -s "Disk alarm!" root

測試運行:

[root@centos7 bin]# diskAlarm.sh
[root@centos7 bin]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Sat Aug 13 12:49  18/626   "Disk alarm!"
& q
Held 1 message in /var/spool/mail/root

11、指定文件做為參數,判斷文件是否為.sh后綴,如果是,添加x權限

[root@centos7 bin]# cat ifsh.sh
#!/bin/bash
#author:jack_cui
#description:show the file whether ended “.sh”
[[ "$1" =~ .*\.sh$ ]]&&chmod a+x $1||echo "file is not ended '.sh'"
[root@centos7 bin]# touch bb.sh
[root@centos7 bin]# ifsh.sh bb.sh
[root@centos7 bin]# ll bb.sh
-rwxr-xr-x. 1 root root 0 Aug 13 13:59 bb.sh
[root@centos7 bin]#

12、判斷輸入的IP是否為合法IP

[root@centos7 bin]# cat ipcorrect.sh
#!/bin/bash
#author:jack_cui
#description:display input ip whether correct!"
corr=`echo $1 |grep -E "(\<([0-9]|[1-9][0-9]|1[0-9]{2}|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])\>"|wc -l`
[ $corr -ne 0  ]&&echo "The IP address is correct!"||echo "The IP is not correct!"

執行結果:

[root@centos7 bin]# ipcorrect.sh 2.2.2.2
The IP address is correct!
[root@centos7 bin]# ipcorrect.sh 233.2333.333.2
The IP is not correct!

13、計算1+2+3+…+100

[root@centos7 bin]# cat sum1_100
#!/bin/bash
#author:jackcui
#description:Add 1 to 100
sum=`seq -s "+" 1 100 |bc`
echo "Add 1 to 100 sum is : $sum"
[root@centos7 bin]# sum1_100
Add 1 to 100 sum is : 5050

14、輸入起始值A和最后值B,計算從A+A+1…+(B-1)+B的總和

[root@centos7 bin]# cat A2Bsum.sh
#!/bin/bash
#author:jackcui
#description:calculator A to B (A and B is two number),INCREMENT can be given,for eg:A=2,B=6,INCREMENT is 2 ,the sum=2+4+6=12;
read -p "Input first number: " A
read -p "Input second number: " B
read -p "Iput INCREMENT number: " I
echo "The sum is : `seq -s '+' $A $I $B|bc`"
unset A B I
[root@centos7 bin]# A2Bsum.sh
Input first number: 2
Input second number: 4
Iput INCREMENT number: 1 //遞增值為1
The sum is : 9
[root@centos7 bin]# A2Bsum.sh
Input first number2  //計算2至6遞增值為2的數字的和
Input second number6
Iput INCREMENT number2  //遞增值為2
The sum is : 12

注釋:seq –s 指定分割符為” + ” ,seq可以產生遞增性的數字,之后傳值給bc命令進行計算

二、位置變量$0 $1,$2,$# $@ $*的區別:

 

$0 :表示執行命令時,命令本身;

$1:表示命令的第一個參數

$#:表示命令的參數的個數

$@,$*:這兩個只有在命令的參數用引號括起來時,前者表示在引號中以空格為分隔符,將引號中的多段分別當成一個參數傳遞值腳本,后者表示將引號中字符串當成一個參數傳遞至腳本

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

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

相關推薦

  • 系統基礎之文件管理grep練習題

    1、顯示/proc/meminfo文件中以大小s開頭的行; (要求:使用三種方式) [root@wen-7 ~]# grep -i "^s" /proc/meminfo  SwapCached:       &…

    Linux干貨 2016-08-07
  • Nginx基礎

    Nginx基礎 目錄 Nginx概述 Nginx的優點 Nginx相比Apache 編譯安裝Nginx Nginx配置文件講解 ngx_http_access_module模塊 ngx_http_auth_basic_module模塊 ngx_http_log_module模塊 ngx_http_stub_status_module模塊 Nginx概述 En…

    Linux干貨 2016-11-01
  • 系統啟動流程相關概念

    前言: 了解系統內核基本知識 內核功能:進程管理、內存管理、網絡協議棧、文件系統、驅動程序、安全功能等  用戶空間:應用程序其中有進程或者線程 運行中的系統可分為兩層:內核空間、用戶空間  內核設計流派:  單內核設計:把每種功能集成于一個程序中;例如:linux 微內核設計:每種功能使用一個單獨的子系統實現;例如:Window…

    Linux干貨 2016-09-19
  • 12.http學習與應用實現

    1、請描述一次完整的http請求處理過程; 1)建立或處理連接:客戶端發送http請求報文,服務器端接收或拒絕請求; 2)接收請求:服務器端接收來自客戶端對某些資源的請求; 3)處理請求:服務器端解析客戶端請求報文,獲取客戶端請求的資源及請求方法等信息; 4)訪問資源:服務器端獲取客戶端請求的資源; 5)構建響應報文; 6)發送響應報文; 7)日志記錄; 2…

    2017-09-20
  • 1.初識linux

    1.計算機組成 計算機五大部件:運算器、控制器、存儲器、輸入設備、輸出設備。對應實際情況大致如下: CPU CPU(Central Processing Unit)主要包含運算器、控制器;也包含寄存器、緩存用于輔助: CPU在同一時間能并行處理的二進制數的位數稱為CPU字長。字長由CPU對外數據通路的數據總線條數決定。 運算器 運算器也稱…

    Linux干貨 2016-08-22
  • 十四.Linux博客-2016年8月22日數組

    格式說明: 操作 概念 命令 說明及舉例 十四.數組 數組概念 變量:存儲單個元素的內存空間數組:存儲多個元素的連續的內存空間,相當于多個變量的集合。數組名和索引索引:編號從0開始,屬于數值索引注意:索引可支持使用自定義的格式,而不僅是數值格式,即為關聯索引,bash4.0版本之后開始支持。bash的數組支持稀疏格式(索引不連續) 聲明數組declare -…

    Linux干貨 2016-08-24
欧美性久久久久