Shell 腳本作業(8月11號)

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小
echo "The hosname is: $(hostname)"
echo "The kernel version is: $(uname -r)"
echo "The server_ip is: $( ifconfig |grep 'inet\b' |grep -v '127.0.0.1' |sed 's/.*addr:\b//' |sed 's/Bcast.*//')"
echo "The CPU is: $(lscpu |grep -i 'model name' |tr -s ' ' |sed 's/.*://')"
echo "The OS version is: $(cat /etc/redhat-release)"
echo "The memorysize is: $(free |sed -n '2p' |tr : ' ' |tr -s ' ' |cut -d' ' -f2)"
echo "The disksize is: $(fdisk -l |sed -n '2p')"
[root@localhost bin]# bash -n systeminfometion.sh 
[root@localhost bin]# bash systeminfometion.sh 
The hosname is: localhost.localdomain
The kernel version is: 2.6.32-642.el6.x86_64
The server_ip is: 10.1.253.35  
The CPU is:  Intel(R) Core(TM) i3-2350M CPU @ 2.30GHz
The OS version is: CentOS release 6.8 (Final)
The memorysize is: 1004108
The disksize is: Disk /dev/sda: 128.8 GB, 128849018880 bytes
[root@localhost bin]# bash -n systeminfometion.sh 
[root@localhost bin]# vim systeminfometion.sh
You have new mail in /var/spool/mail/root

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中
backdir="/root/etc$(date +%F)"
echo $backdir
cp -a /etc/. $backdir && echo "backup finished"
unset backdir
[root@localhost ~]# ll
drwxr-xr-x. 75 root root 4096 Aug 12 05:11 etc2016-08-11

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  顯示當前硬盤分區中空間利用率最大的值
disk_max=$(df |tr -s ' ' % |cut -d% -f5 |grep '[0-9]' |sort -nr |head -1)
echo "The diskused_max is $disk_max"
unset disk_max
[root@localhost bin]# bash disk.sh 
The diskused_max is 53

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  顯示正連接本主機的每個遠程主機的IPv4地址和連接數,并按連接數   從大到小排序
netstat -nt |tr -s ' ' |cut -d' ' -f5 |grep '[0-9]' |sed 's/:.*//' |sort |uniq -c |sort
[root@localhost bin]# bash link.sh
 1 10.10.10.1
 1 10.1.250.69
 3 10.1.50.10

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  計算/etc/passwd文件中的第10個用戶和第20用戶的ID之和
user10_id=$(sed -n '10p' /etc/passwd |cut -d: -f3)
user20_id=$(sed -n '20p' /etc/passwd |cut -d: -f3)
sumid=$[$user10_id+$user20_id]
echo "The sum_id is: $sumid"
unset user10_id
unset user20_id
unset sumid

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  傳遞兩個文件路徑作為參數給腳本,計算這兩個文件中所有空白行之和
file1=$(grep '^$' $1 |wc -l)
file2=$(grep '^$' $2 |wc -l)
sumspace=$[$file1+$file2]
echo "The total spacelines is $sumspace"
unset file1
unset file2
[root@localhost bin]# bash sumspace.sh  /etc/fstab /etc/profile
The total spacelines is 12

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   統計/etc, /var, /usr目錄中共有多少個一級子目錄和文件
etc_sum=$(ls -A /etc |wc -l)
var_sum=$(ls -A /var |wc -l)
usr_sum=$(ls -A /usr |wc -l)
sumfile=$[$etc_sum+$var_sum+$usr_sum]
echo "The total file is $sumfile"
unset etc_sum
unset var_sum
unset usr_sum
[root@localhost bin]# bash sumfile.sh 
The total file is 208

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   接受一個文件路徑作為參數;如果參數個數小于1,則提示用戶“至少應該給一個參數”,并立即退出;如果參數個數不小于1,則顯示第一個參數所指向的文件中的空白行數
[ $# -lt 1 ] && echo "please give a argument" || grep '^$' $1 |wc -l
[root@localhost bin]# bash argsnum.sh /etc/fstab
1

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  接受一個主機的IPv4地址做為參數,測試是否可連通。如果能   ping通,則提示用戶“該IP地址可訪問”;如果不可ping通,則提示用戶“該IP地址不可訪問”
[ $# -ge 1 ] && echo "$1" |egrep -o '(\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m" || echo -e "\e[31m this ip is not accessed \e[0m")
[root@localhost bin]# bash hostping.sh 10.1.1.1
this ip is not accessed 
[root@localhost bin]# bash hostping.sh 10.1.253.35
this ip is accessed

9、chmod -rw /tmp/file1,編寫腳本/root/bin/per.sh,判斷當前用戶對/tmp/fiile1文件是否不可讀且   不可寫

[root@localhost tmp]# chmod -rw file1
[root@localhost tmp]# ll file1
----------. 1 user1 user1 0 Aug 12 08:10 file1
#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:    判斷當前用戶對/tmp/fiile1文件是否不可讀且不可寫
[ -r /tmp/file1 -a -w /tmp/file1 ] && echo "Yous can not to read and writ file1"
[root@localhost bin]# bash per.sh 
Yous can not to read and writ file1

10、編寫腳本/root/bin/nologin.sh和login.sh,實現禁止和充許普通用戶登錄系統。

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   實現禁止和充許普通用戶登錄系統
[ -f /etc/nologin ] && (rm -f /etc/nologin;echo user enable login) || echo user disable login already
[root@localhost bin]# bash login.sh 
user disable login already

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

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe: 接受一個主機的IPv4地址做為參數,先判斷是否合格IP,否,提示IP格式不合法并退出,是,測試是否可連通。如果能ping通,則提示用戶“該IP地址可訪問”;   如果不可ping通,則提示用戶“該IP地址不可訪問
#!/bin/bash
[ $# -ge 1 ] && echo "$1" |egrep -o '(\<([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.\<(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}\<([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))' &> /dev/null && (ping -c 3 $1 &>/dev/null && echo -e "\e[31m this ip is accessed\e[0m" || echo -e "\e[31m this ip is not accessed \e[0m")
[root@localhost bin]# bash hostping.sh 10.1.1.1
 this ip is not accessed 
[root@localhost bin]# bash hostping.sh 10.1.253.35
 this ip is accessed

12、計算1+2+3+…+100的值

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:   計算1+2+3+...+100的值
echo {1..10} |tr ' ' + |bc
[root@localhost bin]# bash sum1.sh 
5050

i=1
sum=0
for i in {1..100};do
    sum=$[$sum+$i]
    i=$i+1
done
echo "the sum is $sum"
unset i
unset sum
[root@localhost bin]# bash sum.sh 
the sum is 5050

13、計算從腳本第一參數A開始,到第二個參數B的所有數字的總和,判斷B是否大于A,否提示錯誤并退出,是則計算之

#!/bin/bash
# author:huiping
# version:1.0.1
# date:2016-08-11
# describe:  計算從腳本第一參數A開始,到第二個參數B的所有數字的總和,判斷B是否大于A,否提示錯誤并退出,是則計算之
#!/bin/bash
[[ $1 -lt $2 ]] && (echo $(seq $1 $2) |tr ' ' + |bc) || echo "error,please input effective number"
[root@localhost bin]# bash f1.sh 10 30
420
[root@localhost bin]# bash f1.sh 100 30
error,please input effective number

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

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

相關推薦

  • 軟件包管理、自建yum源與LAMP架構的自動編譯安裝

    軟件包管理 CentOS采用RedHat開發的rpm包管理器管理應用程序包。rpm包是由二進制可執行程序、庫、配置文件、幫助文件等組成,支持安裝、卸載、查詢、升級、降級、校驗等操作。 從組成結構上,rpm包由文件清單、安裝和卸載時運行的腳本構成。 包管理器有其自帶的公共數據庫。其數據包括:程序包的名稱、版本、依賴關系,功能說明,及各個文件的路徑及校驗碼信息等…

    Linux干貨 2016-12-05
  • linux中的幾種壓縮工具

    文件壓縮 compress 不能自動補齊 .Z -d 解壓縮 uncompress zcat X.Z > X -c 將結果打印到屏幕上,配合重定向,不會覆蓋原文件,但權限會變。 -f 默認不對硬鏈接數為2及以上的文件壓縮,加上f,強制壓縮指定文件,而其他同inode的文件硬鏈接數減1. -v 顯示詳細過程。 ———&…

    2017-08-11
  • 網絡協議端口號查詢表

    TCP 0= ReservedTCP 1=TCP Port Service MultiplexerTCP 2=DeathTCP 5=Remote Job Entry,yoyoTCP 7=EchoTCP 11=SkunTCP 12=BomberTCP 16=SkunTCP 17=SkunTCP 18=消息傳輸協議,skunTCP 19=SkunTCP 20=F…

    Linux干貨 2017-08-15
  • zabbix low-level discover 監控端口

    zabbix通過調用jason格式的輸出,實現數據的收集 獲取端口的shell腳本   #!/bin/bash port_array=(`netstat -tnl|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]…

    Linux干貨 2016-06-09
  • N25_第一周作業_leon

    第一周博客作業 1.       描述計算機的組成及其功能 計算機主要有五大部分組成:控制器;運算器;存儲器;輸入設備;輸出設備。   功能: 控制器:計算機的指揮系統??刂破魍ㄟ^地址訪問存儲器,從存儲器中取出指令,經譯碼器分析后,根據指令分析結果產生相應的操作控制信號作用于其他部件,使得…

    Linux干貨 2016-12-01
  • 第六周作業

    vim編輯器的使用 vim在工作過程中有三種模式:編輯模式、輸入模式、末行模式 編輯模式:也叫命令模式,鍵盤操作常被理解為編輯命令。 輸入模式:在文本文件中輸入內容。 末行模式:vim內置的命令行接口,執行vim的內置命令。 vim的使用 打開文件:#vim [OPTIONS]…[FILE]… +#:打開文件后,直接讓光標處于第#行 …

    Linux干貨 2017-07-04
欧美性久久久久