shell实例(七) --参数/字符串替换

1.说明${parameter-default}--------如果变量parameter没被声明,那么就使用默认值

${parameter:-default}-------如果变量parameter没被设置,那么就使用默认值

filename=${1:-$DEFAULT_FILENAME}---------如果参数1没有设置,那么就取默认的DEFAULT_FILENAME

${parameter=default}----如果变量parameter没声明,那么就把它的值设为default.

${parameter:=default}-----如果变量parameter没设置,那么就把它的值设为default.

${parameter+alt_value}----如果变量parameter被声明,那么就是用alt_value,否则使用null字符串.

${parameter:+alt_value}-----如果变量parameter被设置,那么就是用alt_value,否则使用null字符串.

${parameter?err_msg}-----如果parameter已经被声明,那么就使用设置的值,否则打印err_msg错误消息.

${parameter:?err_msg}----如果parameter已经被设置,那么就使用设置的值,否则打印err_msg错误消息.

冒号在linux中的使用

1、注释

:thisiscomment

:'thisisamultiple

linescomment.'

将注释作为参数传递给冒号命令,因为没有输出所以可以当作注释阿,够巧妙吧。

2、清空文件

:>file

2.使用参数替换和错误消息

#!/bin/sh

:${HOSTNAME?}${USER?}${HOME?}${MAIL?}

echo

echo"Nameofthemachineis$HOSTNAME."

echo"Youare$USER."

echo"Yourhomedirectoryis$HOME."

echo"YourmailINBOXislocatedin$MAIL."

echo

echo"Ifyouarereadingthismessage,"

echo"criticalenvironmentalvariableshavebeenset."

echo

echo

ThisVariable=Value-of-ThisVariable

:${ThisVariable?}

echo"ValueofThisVariableis$ThisVariable."

echo

echo

:${ZZXy23AB?"ZZXy23ABhasnotbeenset."}

echo"Youwillnotseenthismessage,becausescriptalreadyterminated."

HERE=0

exit$HERE

3.变量长度

${#array

}和${#array[@]}表示数组中元素的个数  #!/bin/sh

E_NO_ARGS=65

if[$#-eq0]

then

echo"Pleaseinvokethisscriptwithoneortwocommand-linearguments."

exit$E_NO_ARGS

fi

var01=abcdEFGH28ij

echo"var01=${var01}"

echo"Lengthofvar01=${#var01}"

var02="abcdEFGH28ij"

echo"var01=${var02}"

echo"Lengthofvar02=${#var02}"

echo"Numberofcommand-lineargumentspassedtoscript=${#@}"-----参数个数

echo"Numberofcommand-lineargumentspassedtoscript=${#*}"-----参数个数

exit0

4.参数替换中的模式匹配

#!/bin/sh

var1=abcd12345abc6789

pattern1=a*c

echo

echo"var1=$var1"

echo"var1=${var1}"

echo"Numberofcharactersin${var1}=${#var1}"

echo

echo"pattern1=$pattern1"

echo"---------1--------"

echo'${var1#$pattern1}='"${var1#$pattern1}"

echo'${var##$pattern1}='"${var1##$pattern1}"

echo;echo;echo

pattern2=b*9

echo"var1=$var1"

echo

echo"pattern2=$pattern2"

echo"-------------------"

echo'${var1%pattern2}='"${var1%$pattern2}"

echo'${var1%%pattern2}='"${var1%%$pattern2}"

echo

exit0

5.修改文件扩展名

#!/bin/sh

E_BADARGS=65

case$#in

0|1)

echo"Usage:`basename$0`oldfilesuffixnewfilesuffix"

exit$E_BADARGS

;;

esac

forfilenamein*.$1

do

mv$filename${filename%$1}$2

done

exit0

6.用模式匹配来解析任意字符串

#!/bin/sh

var1=abcd-1234-defg

echo"var1=$var1"

t=${var1#*-*}

echo"var1(witheverything,uptoandincludingfirst-strippedout)=$t"

t=${var1##*-*}

echo"Ifvar1containsa\"-\",returnsemptystring...var1=$t"

t=${var1%*-*}

echo"var1(witheverythingfromthelast-onstrippedout)=$t"

echo

path_name=/home/bozo/ideas/thoughts.for.today

echo"path_name=$path_name"

t=${path_name##/*/}------截去最大匹配/*/

echo"path_name,strippedofprefixes=$t"

t=${path_name%/*.*}------从尾部截去/*.*

echo"path_name,strippedofsuffixes=$t"

echo

t=${path_name:11}

echo"$path_name,withfirst11charsstrippedof=$t"

t=${path_name:11:5}

echo"$path_name,withfirst11charsstrippedoff,length5=$t"

echo

t=${path_name/bozo/clown}

echo"$path_namewith\"bozo\"replacedby\"clown\"=$t"

t=${path_name/today/}

echo"$path_namewith\"today\"delete=$t"

t=${path_name//o/O}

echo"$path_namewithallo'scapitalized=$t"

t=${path_name//o/}

echo"$path_namewithallo'sdeleted=$t"

exit0

7.对字符串的前缀和后缀使用匹配模式

${var/#pattern/replacement}----如果变量var的前缀匹配pattern,那么就是用replacement来替换匹配到的pattern字符串

${var/%pattern/replacement}----如果变量var的后缀匹配pattern,那么就是用replacement来替换匹配到的pattern字符串

#!/bin/bash

v0=abc1234zip1234abc

echo"v0=$v0"

echo

v1=${v0/#abc/ABCDEF}

echo"v1=$v1"

v2=${v0/%abc/ABCDEF}

echo"v2=$v2"

echo

v3=${v0/#123/100}

echo"v3=$v3"

v4=${v0/%123/100}

echo"v4=$v4"

exit0

相关推荐