shell腳本編程

本節所講內容

       shell 基本語法

       變量

       表達式

       判斷語句

       if表達式

 

先看一個簡單的shell程序

[root@server63test]# chmod +x example01.sh

[root@server63 test]# cat example01.sh

#!/bin/bash        

#Thisis to show what a example looks like.

echo"Our first example"

echo# This inserts an empty line in output

echo"We are currently in the following directory."

pwd

echo

echo"This directory contains the following files."

ls

註釋

#!/bin/bash  #! 跟 shell命令的完整路徑 作用:顯示後期命令以哪種shell來執行這些命令

如不指定以當前shell 執行

#Thisis to show what a example looks like.  shell中以#開頭表示註釋 執行時被忽略

 

shell程序 一般以.sh 結尾

 

總結

       創建shell程序的步驟

       第一步: 創建一個包含命令和控制結構的shell文件

       第二步: 修改這個文件的權限使它可以執行

              使用 chmod u+x

       第三步:執行1  ./example01.sh

                     方法2  使用絕對路徑/root/test/example01.sh

                     方法3  bash example01.sh

 

shell變量

變量是shell傳遞數據的一種方法。變量是用來代表每個值的符號名

例:x=3  x代表3

      

shell有兩種變量: 臨時變量和永久變量

臨時變量:是shell程序內部定義的,其使用範圍僅限於定義它的程序,對其他程序不可見。包括用戶自定義變量,位置變量和預定變量。

 

永久性變量 是環境變量,其值不隨shell腳本的執行結束而消失

例:

如 $PATH

[root@server63test]# echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

#用作運行某個命令的時候,本地查找不到某個命令,會到這個聲明的目錄中去查找

 

爲什麼ls可以執行  因爲 ls是別名  /bin/ls, /bin/ls 已寫在$PATH中

[root@server63test]# which ls

aliasls='ls --color=auto'

        /bin/ls

 

用戶自定義的變量:由字母或下劃線開頭,由字母、數字或下劃線序列組成,並且大小寫字母意義不同。變量名長度沒有限制。

 

使用變量值時,要在變量名前加上前綴“$”

 

例如 1var 是非法變量 以數字開頭

 

變量賦值: 賦值號 “=” 兩邊不能有空格

[root@server63test]#

[root@server63 test]# A=aaa

[root@server63 test]# A = aaa

bash:A: command not found

 

例:將一個命令的執行結果賦給變量

[root@server63 test]# A=`date`   反引號中接可執行的命令  將shell命令中的輸出賦值給變量

[root@server63 test]# echo $A

ThuMay 12 17:15:15 CST 2016

 

[root@server63test]# B=$(ls -l)

[root@server63test]# echo $B

total4 -rwxr-xr-x. 1 root root 241 May 12 16:28 example01.sh

 

可以利用變量和其他字符組成一個新的字符串

[root@server63test]# MYDIR=/home/zhh/

[root@server63test]# echo $MYDIR/zhangsan

/home/zhh//zhangsan

 

[root@server63test]# DAY=mon

[root@server63test]# echo Tody is $DAY day

Todyis mon day

[root@server63test]# echo Today is ${DAY}day

Todayis monday

 

列出所有的變量:

       set 命令

 

[root@server63test]#  set | grep DAY

DAY=mon

 

給變量賦值多個單詞

bash:Ron: command not found

[root@server63test]# NAME="mike Ron"

[root@server63test]# echo $NAME

mikeRon

[root@server63test]# name='jj dd'

[root@server63test]# echo $NAME

mikeRon

 

單引號和雙引號的區別:

單引號之間的內容原封不動的輸出給變量

雙引號取消了空格的作用,特殊符號的含義保留

 

[root@server63~]# NAME="aaa bbb"

[root@server63~]# echo $NAME

aaabbb

[root@server63~]# NAME="mike jone $NAME"

[root@server63~]# echo $NAME

mikejone aaa bbb

[root@server63~]# NAME='mike jone $NAME'

[root@server63~]# echo $NAME

mikejone $NAME

 

刪除變量:  unset

[root@server63~]# NAME="john aa"

[root@server63~]# echo $NAME

johnaa

[root@server63~]# unset NAME

[root@server63~]# echo $NAME

 

位置變量和特殊變量

位置變量:shell解釋執行用戶的命令時,講命令行的第一個字作爲命令名,而其他字作爲參數。由出現在命令行上的位置確定的參數稱爲位置參數

ls-l   ls 命令  -l  位置參數

 

位置變量:使用$N 來表示

[root@server63~]# ./example.sh  file1 file2 file3

位置變量用$N 來表示

$0  這個程序的文件名 example.sh

$N這個程序的第n個參數  n=1...N

 

[root@server63~]# cat example.sh.sh

#!/bin/bash

echo"$1"

 

[root@server63 ~]# ./1.sh /etc/passwd /etc/hosts

/etc/passwd

/etc/hosts

特殊變量

有些變量是一開始執行Script腳本時就會設定,且不能被修改,但我們不叫它只讀的系統變量,而叫它特殊變量。這些變量當一執行程序時就有了,一下是一些特殊變量:

$*     這個程序的所有參數

$#     這個程序的參數個數

$$     這個程序PID

$!     執行上一個後臺程序的PID

$?     執行上一個質量的返回值

 

[root@xuegod63~]# cat a.sh

#!/bin/bash

echo"$* Represents all parameters of this program"

echo"$# Number of parameters representing the program"

 

touch/tmp/a.txt

echo"$$  The program process ID"

 

touch/tmp/b.txt &

echo"$!   Execution on a backgroundinstruction PID"

 

[root@xuegod63~]# cat a.sh

#!/bin/bash

echo"$* 表示這個程序的所有參數"

echo"$# 表示這個程序的參數個數"

 

touch/tmp/a.txt

echo"$$  表示程序的進程ID"

 

touch/tmp/b.txt &

echo"$!   執行上一個後臺指令的PID"

 

[root@xuegod63~]# sh a.sh

 表示這個程序的所有參數

0表示這個程序的參數個數

24354  表示程序的進程ID

24356   執行上一個後臺指令的PID

 

 

[root@xuegod63~]# chmod +x a.sh

[root@xuegod63~]# ./a.sh aaa bbb ccc

aaabbb ccc 表示這個程序的所有參數

3表示這個程序的參數個數

24429  表示程序的進程ID

24431   執行上一個後臺指令的PID

 

例:變量在shell中的使用

 

[root@xuegod63~]# cat b.sh

#!/bin/bash

var1="abcdefg"

echo$var1

var2=1234

echo"The value of var2 is $var2"

echo$HOME

echo$PATH

echo$PWD

 

read命令

作用:從磁盤讀入數據,賦值給變量

[root@xuegod63~]# read a b c

12 3

[root@xuegod63~]# echo $a $b $c

12 3

[root@xuegod63~]# echo $a

1

例:在shell中用read命令

[root@xuegod63~]# cat read.sh

#!/bin/bash

echo"input first second third:"

read  first second third

echo"the first parameter is $frist"

echo"the second parameter is $second"

echo"the third parameter is $third"

 

[root@xuegod63~]# ./read.sh

ab c

thefirst parameter is

thesecond parameter is b

thethird parameter is c

 

 

expr 命令

作用:     shell變量的算術運算:

              expr命令:對整數型變量進行算術運算

              語法: expr 表達式  # 注意 運算符之間要有空格

[root@xuegod63~]# expr 3 + 5

8

 

[root@xuegod63~]# var1=8

[root@xuegod63~]# var2=2

[root@xuegod63~]# expr $var1 - 5

3

[root@xuegod63~]# expr $var1 / $var2

4

[root@xuegod63~]# expr $var1 * $var2

expr:syntax error

[root@xuegod63~]# expr $var1 \* $var2   \ 轉譯

16

 

expr程序例子

[root@xuegod63~]# cat expr.sh

#!/bin/bash

a=10

b=20

c=30

value1=`expr$a + $b + $c`

echo"The value of value1 is $value1"

value2=`expr$c / $b`

echo"The value of value2 is $value2"

value3=`expr$c \* $b`    #整除的意思

echo"The value of value3 is $value3"

value4=`expr$a + $c / $b`

echo"The value of value4 is $value4"

 

[root@xuegod63~]# chmod +x expr.sh

[root@xuegod63~]# ./expr.sh

Thevalue of value1 is 60

Thevalue of value2 is 1

Thevalue of value3 is 600

Thevalue of value4 is 11

 

複雜的運算:

[root@xuegod63~]# expr `expr 5 + 11` / $var4  

2

 

變量測試語句:

test

格式

test  測試條件

 

測試範圍:整數,字符串,文件

 

字符串和變量

test$str1 == $str2 是否相等

test$str1!= $str2 是否不相等

 

test  $str1  測試字符串是否不空   如果$str1不爲空 則返回結果爲真

test-n $str1 測試字符串是否不爲零  如果字符串長度不爲零  則返回結果爲真

test  -z $str1 測試字符串長度爲零   如果字符串長度爲零 則返回結果爲真

 

 

測試整數:

test  int1 -eq int2    =

test  int1 -ge int2    >=

test  int1 -gt int2   >

test  int1 -le  int2  <=

test  int1 -lt  int2  <   

說明:也可以省略 test 寫成 [int1 -lt int2]

 

文件測試:

test-d file       #測試是否爲目錄

test  -f file     #測試是否爲文件

test  -r/w/x    #測試權限

test  -s file     #測試大小爲空。是否爲空文件

說明:

test-x   file   簡寫成 [-x file]

 

跟流程控制結合使用

if語句

wKioL1ekNJ_xuL9YAABJTXBrSZk266.jpg


語法:

if 條件

then

語句

fi

 

擴展 ; 分號   表示兩個命令寫在一起。互不影響。  &&是前面執行成功才執行後面的

[root@xuegod64 ~]# ls /oppt && ls

ls: cannot access /oppt: No such file ordirectory

[root@xuegod64 ~]# ls /oppt ; ls

ls: cannot access /oppt: No such file ordirectory

anaconda-ks.cfg  install.log install.log.syslog  key

 

 

[root@xuegod64 ~]# cat if.sh

#!/bin/bash

echo "if test"

if [ -x /bin/ls ] ; then

/bin/ls

fi

echo "+++++++++++++++++++++++++++++"

 

[root@xuegod64 ~]# ./if.sh

if test

if.sh

+++++++++++++++++++++++++++++

 

 

if /else 用法:

wKiom1ekNMyDyTk9AAB344YjdPk464.jpg


語法:

if   條件1  ; then

    命令1

else

    命令2

fi

 

[root@xuegod64 ~]# vim else.sh

#!/bin/bash

if [ -x /etc/passwd ] ; then

       /bin/ls

else  

       pwd

fi

 

[root@xuegod64 ~]# ./else.sh

/root

 

[root@xuegod64 ~]# chmod +x /etc/passwd

[root@xuegod64 ~]# ./else.sh

else.sh if.sh

 

 

多個條件的聯合

-a 或 && : 邏輯與,僅當兩個條件都成立時 結果爲真

-o 或 ll : 邏輯或。兩個條件有一個成立,結果爲真

 

更復雜的if語句

語法:

if     條件1 ; then

      命令1

elif    條件2 ; then

       命令2

elif    條件3 ; then

       命令3

else

       命令4

fi

 

 

綜合實例 : 測試文件類型

[root@xuegod64 ~]# cat elif.sh

#!/bin/bash

 

echo "input a file name:"

read file_name

if [ -d $file_name ] ; then

       echo "$file_name is a dir"

elif [ -f $file_name ] ; then

       echo "$file_name is a file"

elif [ -c $file_name -o -b $file_name ] ; then

       echo "$file_name is a device file"

else 

       echo "$file_name is an unknow file"

fi

 

 

 

brw-rw----. 1 root disk 8, 16 May 20 09:16/dev/sdb

[root@xuegod64 ~]# ll /dev/ | grep c

crw-rw----. 1 root video    10, 175 May 20 09:16 agpgart

crw-rw----. 1 root root     10, 57 May 20 09:16 autofs

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章