我有一个bash脚本,该脚本循环遍历一组文件,并用版本号替换每个文件中的字符串。
#!/bin/bash
ANGULAR_APP_VERSION=6.6.6
echo $ANGULAR_APP_VERSION
declare -a arr=(
"test1.txt"
"test2.txt"
)
for i in "${arr[@]}"
do
sed 's/"@@BUILD_VERSION@@"/"'$ANGULAR_APP_VERSION'"/g' ${arr[$i]}
done
每次我运行脚本时,它都会产生以下错误:
./test1.txt: syntax error: operand expected (error token is "./test1.txt")
我不明白为什么这是错的。文件存在。
#!/bin/bash
ANGULAR_APP_VERSION="6.6.6"
echo "$ANGULAR_APP_VERSION"
arr=(
"test1.txt"
"test2.txt"
)
for i in "${arr[@]}"; do
sed -i "s/@@BUILD_VERSION@@/$ANGULAR_APP_VERSION/g" "$i"
done
($i
是数组中的每个值一次迭代一次)
或使用数组key
:
for i in "${!arr[@]}"; do
sed -i "s/@@BUILD_VERSION@@/$ANGULAR_APP_VERSION/g" "${arr[i]}"
done
了解如何在shell中正确报价,这非常重要:
“双引号”包含空格/元字符,每字面每一个扩展:
"$var"
,"$(command "$var")"
,"${array[@]}"
,"a & b"
。使用'single quotes'
代码或文字$'s: 'Costs $5 US'
,ssh host 'echo "$HOSTNAME"'
。参见http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句