shell字符串拼接2,作为结果传递参数
打印日志需涵盖很多固定输出,所以我shell输出调用函数,也就是把特定字符串传给打印函数
存在一个问题,就是字符串包含空格时,只输出第一个空格前的内容。我盲猜,可能作为多个参数了。
不作为函数参数时,可以正常输出
$cat aaa.sh #! /bin/bash pack="" tmp1="apple banana" tmp2="orange" pack=${pack}${tmp1}" " pack=${pack}${tmp2}" " echo ${pack} $ ./aaa.sh apple banana orange
作为参数时,输出有毛病
$cat ccc.sh #! /bin/bash function print() { echo $1 } pack="" tmp1="apple banana" tmp2="orange" pack=${pack}${tmp1}" " pack=${pack}${tmp2}" " print $pack $./ccc.sh apple
错误改法+验证
$cat ddd.sh #! /bin/bash IFS_BACKUP=$IFS function print() { IFS=$(echo -en "\n\b") echo $1 $2 IFS=$IFS_BACKUP } pack="" tmp1="apple banana" tmp2="orange" pack=${pack}${tmp1}" " pack=${pack}${tmp2}" " print $pack $ ./ddd.sh apple banana
正确改法
$ cat fff.sh #! /bin/bash IFS_BACKUP=$IFS function print() { echo $1 } pack="" tmp1="apple banana" tmp2="orange" pack=${pack}${tmp1}" " pack=${pack}${tmp2}" " IFS=$(echo -en "\n\b") print $pack IFS=$IFS_BACKUP $ ./fff.sh apple banana orange
最后,一个测试
$ cat ggg.sh
#! /bin/bash
IFS_BACKUP=$IFS
function print()
{
echo $1
}
pack=""
tmp1="apple banana"
tmp2="orange"
pack=${pack}${tmp1}" "
pack=${pack}${tmp2}" "
print 'apple orange yellow'
print "apple orange yellow"
print $pack
print '$pack'
print "$pack"
$./ggg.sh
apple orange yellow
apple orange yellow
apple
$pack
apple banana orange
https://www.jb51.net/article/179534.htm
posted on 2021-07-09 15:58 toughcactus 阅读(631) 评论(0) 编辑 收藏 举报