1. 程式人生 > >shell腳本中的dat,計算器,內置變量的用法

shell腳本中的dat,計算器,內置變量的用法

記錄 出現 我想 註釋 並不是 定時 put 臃腫 min

什麽是shell腳本。首先它是一個腳本,並不能作為正式的編程語言。因為是跑在linux的shell中,所以叫shell腳本。說白了,shell腳本就是一些命令的集合。舉個例子,我想實現這樣的操作:

1)進入到/tmp/目錄;

2)列出當前目錄中所有的文件名;

3)把所有當前的文件拷貝到/root/目錄下;

4)刪除當前目錄下所有的文件。

簡單的4步在shell窗口中需要你敲4次命令,按4次回車。這樣是不是很麻煩?所以不妨把所有的操作都記錄到一個文檔中,然後去調用文檔中的命令,這樣一步操作就可以完成。其實這個文檔呢就是shell腳本了,只是這個shell腳本有它特殊的格式。

Shell腳本能幫助我們很方便的去管理服務器,因為我們可以指定一個任務計劃定時去執行某一個shell腳本實現我們想要需求。比如利用郵件的便利,我們可以在我們的linux服務器上部署監控的shell腳本,比如網卡流量有異常了或者服務器web服務器停止了就可以發一封郵件給管理員,同時發送給管理員一個報警短信這樣可以讓我們及時的知道服務器出問題了。

凡是自定義的腳本建議放到/usr/local/sbin/目錄下,這樣做的目的是,一來可以更好的管理文檔;二來以後接管你的管理員都知道自定義腳本放在哪裏,方便維護。

shell腳本的基本結構以及如何執行

第一個shell腳本

[[email protected] ~]# cd /usr/local/sbin/
[[email protected] sbin]# vim first.sh
#! /bin/bash

## This is my first shell script.
##  2017.09.02.

date
echo "Hello world!"
Shell腳本通常都是以.sh 為後綴名的,這個並不是說不帶.sh這個腳本就不能執行,只是大家的一個習慣而已。所以,以後你發現了.sh為後綴的文件那麽它可能是一個shell腳本了。shell腳本中的第一行
要以 “#! /bin/bash” 開頭,它代表的意思是,該文件使用的是bash語法。如果不設置該行,雖然你的shell腳本也可以執行,但是這不符合規範。 # 表示註釋,後面跟一些該腳本的相關註釋內容以及作者和創建日期或者版本等等。當然這些註釋並非必須的,但建議還是寫上。。因為隨著工作時間的逐漸過渡,你寫的shell腳本也會越來越多,如果有一天你回頭查看自己寫過的某個腳本時,很有可能忘記該腳本是用來幹什麽的以及什麽時候寫的。所以寫上註釋是有必要的。另外系統管理員並非只有你一個,如果是其他管理員查看你的腳本,他看不懂豈不是很郁悶。下面該運行一下這個腳本了:
[[email protected] sbin]# sh first.sh
2017年 09月 2日 星期六 18:58:02 CST
Hello world!

其實shell腳本還有一種執行方法就是:

其實shell腳本還有一種執行方法就是:

[[email protected] sbin]# ./first.sh
-bash: ./first.sh: 權限不夠
[[email protected] sbin]# chmod +x first.sh
[[email protected] sbin]# ./first.sh
2017年 09月 02日 星期六 18:58:51 CST
Hello world!

要想使用該種方法運行shell腳本,前提是腳本本身有執行權限,所以需要給腳本加一個 ‘x’ 權限。另外使用sh命令去執行一個shell腳本的時候是可以加-x選項來查看這個腳本執行過程的,這樣有利於我們調試這個腳本哪裏出了問題:

[[email protected] sbin]# sh -x first.sh
+ date
2017年 09月 02日 星期六 20:00:11 CST
+ echo ‘Hello world!‘
Hello world!
命令 : date, 在shell中的用法
[[email protected] sbin]# date +"%Y-%m-%d %H:%M:%S"
2017-09-2 19:41:01

date在腳本中最常用的幾個用法:

data +%Y 以四位數字格式打印年份

date +%y 以兩位數字格式打印年份

date +%m 月份

date +%d 日期

date +%H 小時

date +%M 分鐘

date +%S

date +%w 星期,如果結果顯示0 則表示周日

有時在腳本中會用到一天前的日期:

[[email protected] sbin]# date -d "-1 day" +%d
1

或者一小時前:

[[email protected] sbin]# date -d "-1 hour" +%H
18

甚至1分鐘前:

[[email protected] sbin]# date -d "-1 min" +%M
50

shell腳本中的變量

如果你寫了一個長達1000行的shell腳本,並且腳本中出現了某一個命令或者路徑幾百次。突然你覺得路徑不對想換一下,那豈不是要更改幾百次?你固然可以使用批量替換的命令,但也是很麻煩,並且腳本顯得臃腫了很多。變量的作用就是用來解決這個問題的。

[[email protected] sbin]# cat variable.sh
#! /bin/bash

## In this script we will use variables.
## Writen by Aming 2017-09-2.

d=`date +%H:%M:%S`
echo "The script begin at $d."
echo "Now we‘ll sleep 2 seconds."
sleep 2
d1=`date +%H:%M:%S`
echo "The script end at $d1."
‘d’ 和 ‘d1’ 在腳本中作為變量出現,定義變量的格式為 變量名=變量的值 當在腳本中引用變量時需要加上 ‘$’ 符號,下面看看腳本執行結果吧:
[[email protected] sbin]# sh variable.sh
The script begin at 20:16:57.
Now we‘ll sleep 2 seconds.
The script end at 20:16:59.

下面我們用shell計算兩個數的和:

[[email protected] sbin]# cat sum.sh
#! /bin/bash

## For get the sum of tow numbers.
## 2017.09.02.

a=1
b=2
sum=$[$a+$b]

echo "$a+$b=$sum"

數學計算要用[ ]括起來並且外頭要帶一個 ‘$’ 腳本結果為:

[[email protected] sbin]# sh sum.sh
1+2=3

Shell腳本還可以和用戶交互:

[[email protected] sbin]# cat read.sh
#! /bin/bash

## Using ‘read‘ in shell script.
## 2017.09.02.

read -p "Please input a number: " x
read -p "Please input another number: " y
sum=$[$x+$y]
echo "The sum of the two numbers is: $sum"

read 命令就是用在這樣的地方,用於和用戶交互,把用戶輸入的字符串作為變量值。腳本執行過程如下:

[[email protected] sbin]# sh read.sh
Please input a number: 2
Please input another number: 10
The sum of the two numbers is: 12
有時候我們會用到這樣的命令 /etc/init.d/iptables restart 前面的/etc/init.d/iptables文件其實就是一個shell腳本,為什麽後面可以跟一個 “restart”? 這裏就涉及到了shell腳本的預設變量。實際上,shell腳本在執行的時候後邊是可以跟參數的,而且還可以跟多個。

[[email protected] sbin]# cat option.sh
#! /bin/bash

sum=$[$1+$2]
echo "sum=$sum"

執行結果為:

[[email protected] sbin]# sh -x option.sh 1 2
+ sum=3
+ echo sum=3
sum=3
$1和$2,這其實就是shell腳本的預設變量,其中$1的值就是在執行的時候輸入的1,而$2的值就是執行的時候輸入的$2,當然一個shell腳本的預設變量是沒有限制的,另外還有一個$0,不過它代表的是腳本本身的名字。不妨把腳本修改下

[[email protected] sbin]# cat option.sh
#! /bin/bash

echo "$1 $2 $0"

執行結果:

[[email protected] sbin]# sh option.sh  1 2
1 2 option.sh

shell腳本中的dat,計算器,內置變量的用法