bash編程之變量替換 及 函數

bash編程之變量替換


${parameter:-word}如果變量的值爲空則使用默認值word。

Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.


${parameter:=word}如果變量的值爲空或者未設置則使用默認值word並且設置變量等於默認值word即賦值給變量。

Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way.


${parameter:?word}如果變量值爲空或者未設置則顯示錯誤信息word。

Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted.


${parameter:+word}如果變量值爲空或者未設置反而什麼都不做當變量值不爲空時則替換變量值爲word。

Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.



${parameter#*word}表示以word 爲分隔符從左往右找匹配到第一個word後刪除word左邊的所有內容包括word本身。


${parameter##*word}表示以word爲分隔符從左往右找匹配到最後一個word後刪除word左邊的所有內容包括word本身。

The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of

parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ?..?.case) or the longest matching pattern (the ?..#?..case) deleted. If parameter is @ or *, the pattern removal operation is

applied to each positional parameter in turn, and the expansion is the resultant list. scripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.


例子

FILE=/usr/local/src

${FILE#*/}: usr/local/src

${FILE##*/}: src



${parameter%word*}表示以word 爲分隔符從右往左找匹配到第一個word後刪除word右邊的所有內容包括word本身

${parameter%%word*}表示以word 爲分隔符從右往左找匹配到最後一個word後刪除word右邊的所有內容包括word本身

例子

${FILE%/*}: /usr/local

${FILE%%/*}:


練習

1、取出root用戶的shell

User=`grep "^root:" /etc/passwd`

echo ${User##*:}


練習複製一個命令及其依賴的庫文件至目標目錄下的目錄結構中例如如果我們要複製cat程序至目標目錄/mnt/sysroot則先查找cat程序文件的位置假設爲/bin/cat於是複製/bin/cat至/mnt/sysroot/bin目錄下且文件名稱依然爲cat

提示獲取一個命令所依賴的庫文件的命令爲ldd

#!/bin/bash

#

declare -i DebugLevel=0 #設置調試功能當Debuglevel=1時調試爲0時關閉此功能

Target=/mnt/sysroot

[ -d $Target ] ||mkdir $Target &> /dev/null

read -p "A command:" Command

while [ $Command !='q' -a $Command !='Q' ]; do

Command=`which $Command|grep -v "^alias"|grep -o "[^[:space:]]*"`

[ $DebugLevel -eq 1 ] && echo $Command

ComDir=${Command%/*}

[ $DebugLevel -eq 1 ] && echo $ComDir

[ -d $Target$Command ] || mkdir -p $Target$ComDir

[ ! -f $Target$Command ] && cp $Command $Target$Command && echo "Copy $Command to $Target finished"


for Lib in `ldd $Command |grep -o "[^[:space:]]"`;do

LibDir=${Lib%/*}

[ $DebugLevel -eq 1 ] && echo $LibDir

[ -d $Target$LibDir ] || mkdir -p $Target$LibDir

[ ! -f $Target$Lib ] $$ cp $Lib $Target$Lib && echo "copy $Lib to $Target finished."

done

read -p "A command:" Command

done



echo -e 可以讓字體顯示顏色。

格式:\033[3#;4#;#mabc\033[0m

echo -e "\033[31mabc\033[0m"

\033[是ASCIS碼 以\033[開始要以\033[結束

3#爲前景色31紅色32綠色33黃色,34藍色,35紫色,36藍綠色...

4#爲背景色 也可以不用41紅色42綠色43黃色,44藍色,45紫色,46藍綠色...

#:(1-9)爲字體 斜體粗體下劃線閃爍

abc爲顯示的字符

\033[0m:結束符



以後可以不用這麼麻煩我們可以設定變量如

Red=‘\033[31m’ #定義紅色

Ter='\033[om' #定義結束符


echo -e "${Red}hello world${Ter}"


MOVE_TO_COL="echo -en \\033[${RES_CO;}G" #G 可以移動光標20G表示光標從特定位置開始向右移動20個字符終端一般是80*10

echo -e "\033[20Gabc"

abc

echo -e "\033[30Gabc"

abc

echo -e "\033[30Gabc"

abc

echo -e "\033[60G[ \033[32mOK ]\033[0m"

[ OK ]

echo -n "Starting network service ....";echo -e "\033[60G[ \033[32mOK ]\033[0m"

Starting network service .... [ OK ]







bash如何定義函數

格式一

function FUNC_NAME {

函數體

}

格式二:

FUNC_NAME() {

函數體

}


bash函數可以接受參數在調用函數時直接向其傳遞即可例如

FUNC_NAME argu1 argu2 ...


bash函數的返回值

1、執行結果通常使用echo或print輸出

2、狀態返回值0表示成功, 1-255表示失敗。

自定義函數在函數體執行的任何位置遇到return語句函數就結束執行並且返回執行狀態碼

在函數中給變量加上local 表示這個變量只在函數中生效。以後在寫函數的時候儘可能的用local 定義變量這樣可以避免與其他其他函數中的變量重名。

例如

在函數中如果不使用local 變量

ShowUser() {

UserName=magedu

echo $UserName

}


UserName=Jason

ShowUser

echo $UserName

執行結果

Jason

magedu

magedu

在函數中加上local 定義變量


ShowUser() {

local UserName=magedu

echo $UserName

}


UserName=Jason

ShowUser

echo $UserName

執行結果爲

Jason

magedu

Jason

Notes:這兩個例子的區別在於用local 定義的變量只作用於函數 不加的話變量就是全局變量會把相同變量的值替換的。


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