初窺門徑之腳本書寫

一、幾個腳本的編寫:

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 12:06
下一篇 2016-08-15 12:06

相關推薦

  • samba服務

    一、什么是SAMBA   Linux之間可以通過NFS服務方便的掛載遠程主機上的共享目錄,那么SAMBA主要是用于實現Windows主機與Linux主機之間共享文件互訪。它們之間的通信依賴于CIFS協議。 二、SAMBA使用的daemon     1、nmbd:在Linux上實現NetBIOS;     &n…

    Linux干貨 2016-02-14
  • 軟鏈接與硬鏈接

    軟鏈接 硬鏈接

    2017-11-18
  • 10.腳本練習2

    1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) POST?–>?Boot?Sequence(BIOS)?–>?Boot?Loader(MBR)? ?–>?Kernel+ramdisk(initrd)?–>?rootfs?–>?/sbin/init? ?–>?(/etc/inittab…

    2017-09-20
  • DNS

    配置緩存服務器: #vim /etc/named.conf Options { Listen-on port 53 { 127.0.0.1;172.16.0.68;}; //listen-on-v6 port 53 {::1;}; ……. Allow-query {any;}; …… Dnssec-enable…

    2017-06-04
  • shell腳本語言中的選擇執行

    shell腳本語言中的選擇執行 概述 程序執行過程分為順序執行、選擇執行和循環執行。順序執行是指程序按照步驟一步一步地運行。選擇執行是指程序根據特定條件選擇兩項或者多項中的一項運行。循環執行是指程序根據特定條件重復執行直到某個節點結束,繼續運行其他步驟。本篇文章從判斷條件和條件判斷式簡要說明shell腳本語言中程序選擇執行的用法。 shell腳本中的判斷條件…

    Linux干貨 2017-04-17
  • awk,systemctl,破解7root口令

    awk -F 指明輸入時用到的字段分隔符 默認空格為分隔符 -v 自定義變量 基本格式:awk [options] 'program' file $1,$2..$n稱為域標識,$0為所有域。 文件的每一行稱為記錄 awk '{print}' /etc/passwd 默認 print $0 顯示全段   awk…

    Linux干貨 2016-10-05
欧美性久久久久