Linux Bourne shell 替换问题
我试图在一个(非常)小的嵌入式 busybox 中使用内置数据命名空间的 BS 中的替换(没有人,总共 60 个命令),但是一旦有超过 2 个数据,我就无法回显数据回显:
这没问题:
a=$(echo -e ${smtp_0} ${smtp_4})
echo $a
# returns: "0 4" as expected, also all individually printed datas are echoed as expected
这不会回显预期数据无论数据是什么:
b=$(echo -e ${smtp_0} ${smtp_4} ${smtp_5})
echo $b
# returns: "54" , same with double-quotes (Nok, it should return "0 4 5")
数据是这样构建的:
“数据文件样本”
val0=1
val1=1
...
读取数据外壳:
#!/bin/sh
x=0
while read line
do
# fetch values, removing blank and commented lines, eg keeping only lines starting with data namespace
formatted_line=$(echo $line | sed -e "/^[^a-z].*$/d" | cut -d= -f2)
# store file's value into a data array-like
if [ ! -z $formatted_line ];then
eval "`echo $x | sed -e 's/.*/smtp_&=$formatted_line/'`"
x=$(($x+1))
fi
done < $DATA_FILE
# Then try echoing datas...
# ... see above ...
编辑: 所以看起来确实有 nthg 错误,但数据文件 EOL 误导了内置数据的串联。我结束了这一点,感谢丹尼斯帮助解决了这个头痛问题。
I'm trying to use substitution in a BS with built data namespace within a (very) small embedded busybox (no man, 60 cmds all in all), but I can't echo the data as soon as there are more than 2 data echoed :
this is OK :
a=$(echo -e ${smtp_0} ${smtp_4})
echo $a
# returns: "0 4" as expected, also all individually printed datas are echoed as expected
this does not echo expected datas whatever data is:
b=$(echo -e ${smtp_0} ${smtp_4} ${smtp_5})
echo $b
# returns: "54" , same with double-quotes (Nok, it should return "0 4 5")
Datas are built like this :
"data file sample"
val0=1
val1=1
...
Reading datas shell:
#!/bin/sh
x=0
while read line
do
# fetch values, removing blank and commented lines, eg keeping only lines starting with data namespace
formatted_line=$(echo $line | sed -e "/^[^a-z].*$/d" | cut -d= -f2)
# store file's value into a data array-like
if [ ! -z $formatted_line ];then
eval "`echo $x | sed -e 's/.*/smtp_&=$formatted_line/'`"
x=$(($x+1))
fi
done < $DATA_FILE
# Then try echoing datas...
# ... see above ...
EDITED:
So it does look like there is nthg mistaken in there but the data file EOL misleading the concatenation of the builtin data. I close the point and thx to Dennis helping getting this headhache fixed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了保留空格,应该引用变量:
但是,为什么要使用
echo
?另外,您应该在代码中使用缩进(或者如果您是这样,那么您应该在发布问题时保留它)。
To preserve whitespace, variables should be quoted:
However, why are you using
echo
?Also, you should use indenting in your code (or if you are then you should retain it when posting questions).