Shell指令碼程式設計

2021-08-28 03:36:19 字數 4588 閱讀 8715

1.linux中的變數

linux中的變數分為環境變數和普通變數,其中環境變數可以理解為全域性變數,在所有shell的子程式中都可以引用,普通變數只能在自己的shell程式中使用,程式結束後變數無法保留。

設定環境變數的方法:

1.export命令 使用者退出後失效

export test=12312
2..bash_profile、.bashrc、etc/bashrc或者etc/profile中定義

使用者的環境變數:

ls /root/.bashrc(推薦檔案)

ls /root/.bash_profile

全域性變數配置

/etc/profile

etc/bashrc(推薦)

所有環境變數應該為大寫

檢視環境變數:

env檢視環境變數列表

set 輸出所有變數(包括環境變數和普通變數)

unset消除環境變數

unset $path
2.變數定義

本地變數定義三種方法:

a=123+ $a 變數會被解析

a=123

b=123$a

echo $b

輸出以下:

123123
a='123 + $a' 變數不會被解析,單引號中的字串原樣輸出

a=123

b='123$a'

echo $b

輸出為:

123$a
a="123" 變數解析--一般用此種方法定義變數

a=123

b="123$a"

echo $b

命令結果作為變數的值(常見用法):
a=$(ls)
3.對於字串的操作

輸出字串長度:

name="testname"

方法一:

echo $
輸出結果:

8
方法二:

echo $|wc -l
方法三:

expr length "$"
擷取字串內容:

首先定義字串:

name=testnametestname
擷取字串:

echo $ 從第二位開始擷取
輸出結果:

stnametestname
字串匹配
echo $ 從頭開始最短匹配
輸出為:

ametestname

最長匹配:

echo $ 從頭開始最長匹配
輸出為:

ame從尾開始匹配:

echo  $
輸出為:

testnametest
最長匹配:

echo $

test

字串的替換:

echo $ 從頭匹配替換第乙個

echo $ 從頭匹配替換所有

將以下檔名中的all去掉
test-all-1.txt  test-all-2.txt  test-all-3.txt  test-all-4.txt  test-all.txt
指令碼為:

for f in $(ls *.txt)

domv $ $

done

4.算術運算

方法一:

echo $((2+2))

4

方法二:

let a=12+12

echo $

24

方法三:

expr 12 % 3

0echo "123 122" |awk ''

$ echo $[12+12] a=$[12+12+12]

5.條件判斷語句:

方法一:

test

test -f test4.txt && echo true || echo false

方法二:

[ -f test4.txt ] && echo true || echo false 檔案test4.txt存在輸出true,不存在輸出false

方法三:

 括號前後加空格

[[ -f test4.txt ]] && echo true || echo false

字串測試:

[ -z "" ] && echo false  -z 字串長度為0的時候為真

[ -n "test" ] && echo true || echo false -n 字串長度不為0的時候為真

[ "test" == "test" ] && echo true 字串是否相等 同= !=

等號和中括號兩端需要有空格

整數的比較:

[ 2 -eq 3 ] && echo true || echo false

邏輯操作符:

-a -o ! 與或非

[ 2 -eq 2 -a 3 -eq 3 ] && echo true || echo false

6.if條件語句
單分支結構:

if《條件表示式》

then

指令fiif 《條件表示式》; then

指令fi

多分支:

if《條件表示式》

then

else

fi

多分支:

if《條件表示式》

then

elif《條件表示式》

then

else

fi

7.shell函式

functiontest.sh指令碼內容:

function testfun()

testfun $1

sh functiontest.sh testname

while和until迴圈

while《條件表示式》

do 命令

done

until《表示式》

dodone

指令碼後台執行:

sh functiontest.sh&

control + c 停止

control + z 暫停

for 語句

for((i=1;i<5;i++))

do echo $i

done

select 語句

select name in ywp hf csq

do echo $name

done

break n 跳出整個迴圈

continue n 跳出本次迴圈

陣列:array=(ywp hf jc yc)

echo $

方法二:

array=([1]=one [2]=two [3]=threee )

echo $

echo $ *列印整個陣列的內容

for迴圈列印陣列內容

array=(test1 test2 test3)

for name in $

do echo $

done

動態陣列:

array=($(ls))

echo $

echo $ 列印陣列長度

9.shell指令碼開發規範
1.全域性變數 全部大寫

2.區域性變數 駝峰

3.變數引用 ${}

4.字串變數引用 "${}"

5.統一使用.sh命名

6.啟動和停止統一使用start和stop開頭

7.通用變數放在config目錄下

8.中括號兩邊新增空格

shell指令碼除錯:

sh [-nvx] test.sh

-n 不執行,僅檢查語法問題

-x將執行的指令碼輸出到螢幕上

vim 配置:

echo 'alias vi=vim' >>/users/***/.bash_profile

source /users/***/.bash_profile

Shell 指令碼程式設計

1 執行shell的方法 指定shell bin sh 由sh執行指令碼 指令碼總是由sh解釋 顯示呼叫shell sh scriptname 在當前shell中執行指令碼 profile profile是可執行的 ksh profile profile是不可執行的 改變當前的執行環境責應輸入.pr...

Shell 指令碼程式設計

a file 如果 file 存在則為真。b file 如果 file 存在且是乙個塊特殊檔案則為真。c file 如果 file 存在且是乙個字特殊檔案則為真。d file 如果 file 存在且是乙個目錄則為真。e file 如果 file 存在則為真。f file 如果 file 存在且是乙個...

shell指令碼程式設計

今天看看shell程式設計,記錄下期中與想象中不一樣的地方 0.注釋用 1.shell的變數賦值 your name zhm 在your name和等號中間不能有空格,這和別的語言很不一樣,那麼在使用變數時和別的語言也不一樣,要使用 your name,一般要用 一定是大括號 2.那麼如何把乙個變數...