天天看点

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

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

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

执行结果

Jason

magedu

在函数中加上local 定义变量

local UserName=magedu

执行结果为

继续阅读