【linux】shall 腳本

shall,是一些命令的集合

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

例子1:test.sh (cat輸出內(nèi)容為vim輸入內(nèi)容)

vim test.sh
cat test.sh

#! /bin/bash
## This is my first shell script.
## Writen by Shen 2020-07-21.
date
echo "Hello World."

sh test.sh

Sat Jul 11 12:59:47 CST 2020
Hello World.

Shell腳本通常都是以.sh 為后綴名的,test.sh中第一行一定是 “#! /bin/bash”它代表的意思是,該文件使用的是bash語法。如果不設置該行,那么你的shell腳本就不能被執(zhí)行?!?’表示注釋,在前面講過的。后面跟一些該腳本的相關注釋內(nèi)容以及作者和創(chuàng)建日期或者版本等等。當然這些注釋并非必須的,如果你懶的很,可以省略掉,但是筆者不建議省略。因為隨著你工作時間的增加,你寫的shell腳本也會越來越多,如果有一天你回頭查看你寫的某個腳本時,很有可能忘記該腳本是用來干什么的以及什么時候?qū)懙摹K詫懮献⑨屖怯斜匾?。另外系統(tǒng)管理員并非你一個,如果是其他管理員查看你的腳本,他看不懂豈不是很郁悶。該腳本再往下面則為要運行的命令了。

例子2:默認vim文檔無執(zhí)行權限,可以添加執(zhí)行權限,"./filename" 執(zhí)行腳本。

chmod +x test.sh

./test.sh

Sat Jul 11 13:07:52 CST 2020
Hello World.

使用 sh + x + *.sh 可以查看腳本的執(zhí)行過程,方便調(diào)試。

sh -x test.sh

+ date
Sat Jul 11 14:55:25 CST 2020
+ echo Hello World.
Hello World.

date 用于獲得系統(tǒng)時間,%Y表示年,%m表示月,%d表示日期,%H表示小時,%M表示分鐘,%S表示秒(區(qū)分大小寫)

date "+%Y%m%d %H%M%S"

20200711 150121

date -d 可以打印 n 天/月/年 前或者 n 天/月/年 后的日期。

date -d "-1 day" "+%Y%m%d"

20200710

date + %w 打印星期幾

date +%w

6

shell 腳本中的變量

變量來代替長且出現(xiàn)頻率高的命令或路徑。賦予變量時用的不是引號而是 "`"。

vim test2.xh

cat test2.sh 

#! /bin/bash
# In this script we will use variables.
# Writen by Shen.
d=`date +%H:%M:%S`
echo "the script begin at $d"
echo "now we will sleep 2 seconds."
sleep 2
d=`date +%H:%M:%S`
echo "the script end at $d"

sh test2.sh 

the script begin at 15:34:20
now we will sleep 2 seconds.
the script end at 15:34:22

這里(下面)有個有趣的發(fā)現(xiàn)

使用 shell 計算兩個數(shù)的和

cat test3.sh 

#! /bin/bash
a=1
b=2
sum=$[$a+$b]
echo "sum is $sum"

sh test3.sh 

sum is $[1+2]

bash test3.sh 

sum is 3

sh 輸出結(jié)果為 $[1+2],而 bash 輸出結(jié)果為 3.

原因是Ubuntu系統(tǒng) sh 指向的是 dash shell(一個小巧的shell,功能沒有 bash 強大),而在 centOS 上,sh 指向的是 bash shell。

shall 腳本與用戶互動

read 命令,read x 表示變量 x 的值需要用戶通過鍵盤輸入得到。

例子:

cat test.sh 

#! /bin/bash
echo "please input a number:"
read x
echo "please input anather number:"
read y
sum=$[$x+$y]
echo "the sum of two number is: $sum."

bash test.sh 

please input a number:
4
please input anather number:
6
the sum of two number is: 10.

上述步驟更簡練的實現(xiàn)方法:
echo 和 read,整合為 read -p

cat test2.sh 

#! /bin/bash
read -p"please input a number:" x
read -p"please input another number:" y
sum=$[$x+$y]
echo "the sum of two number is: $sum."

bash test2.sh 

please input a number:9
please input another number:4
the sum of two number is: 13.

shell 腳本的預設變量

例子:此處 $0 表示腳本的名字,$1 表示第一個輸入變量 ...

cat test3.sh 

#! /bin.bash
echo "$0 $1 $2"

bash test3.sh 12

test3.sh 12 

bash test3.sh 12 36

test3.sh 12 36

shell 腳本中的邏輯判斷

例子: if 語句

具體格式:

if 判斷語句一 ; then
command
elif 判斷語句二; then
command
else
command
fi

cat iftest.sh 

#! /bin/bash
read -p "please input your score:" a
if ((a<60)) ; then
    echo "you didn't pass the exam."
else
    echo "Good!, you passed the exam."
fi

bash iftest.sh 

please input your score:45
you didn't pass the exam.

同樣可以使用 elif 語句,&& 表示 并且 ,|| 表示 或者。

在判斷數(shù)值大小除了可以用”(( ))”的形式外,還可以使用”[ ]”。但是就不能使用>, < , = 這樣的符號了,要使用 -lt (小于),-gt (大于),-le (小于等于),-ge (大于等于),-eq (等于),-ne (不等于)。

shell 腳本判斷檔案屬性

-e :判斷文件或目錄是否存在
-d :判斷是不是目錄,并是否存在
-f :判斷是否是普通文件,并存在
-r :判斷文檔是否有讀權限
-w :判斷是否有寫權限
-x :判斷是否可執(zhí)行

例子:

if [ -f test2.sh ] ; then echo ok; fi

ok

if [ -d /home ] ; then echo ok; fi

ok

if [ -f test5.sh ] ; then echo ok; fi

例子:case 語句

具體格式為:

case 變量 in
value1)
command
;;
value2)
command
;;
value3)
command
;;
*)
command
;;
esac

上面的結(jié)構(gòu)中,不限制value的個數(shù),*則代表除了上面的value外的其他值。下面筆者寫一個判斷輸入數(shù)值是奇數(shù)
或者偶數(shù)的腳本。

cat cash.sh 

#! /bin/bash
read -p "please input a number:" n
a=$[$n%2]
case $a in

1)
    echo "the number is odd."
    ;;
0)
    echo "the number is even."
    ;;
esac

bash cash.sh 

please input a number:56
the number is even.

bash cash.sh 

please input a number:23
the number is odd.

shell 腳本中的循環(huán)

例子:for 循環(huán)

基本機構(gòu):
for 變量名 in 循環(huán)的條件; do
command
done

cat for.sh 

#! /bin/bash
for i in `seq 1 5`; do
    echo $i
done

bash for.sh 

1
2
3
4
5

例子:while 循環(huán)

基本格式為:

while 條件; do
command
done

cat while.sh 

#! /bin/bash
a=10
while (( $a > 3 )); do
    echo "$a"
    a=$[$a-1]
done

bash while.sh 

10
9
8
7
6
5
4

shall 腳本中的函數(shù)

shell 腳本中,函數(shù)一定要寫在最前面,防止出現(xiàn)還沒出現(xiàn)就被調(diào)用的錯誤

在shell腳本中要用:

function 函數(shù)名() {
command
}

這樣的格式去定義函數(shù)。

例子:sum()

cat fun.sh 

#! /bin/bash
function sum(){
    sum=$[$1+$2]
    echo $sum
}
sum $1 $2

bash fun.sh 5 6

11
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容