Linux Bourne shell 替换问题

发布于 2024-10-20 05:48:07 字数 1150 浏览 7 评论 0原文

我试图在一个(非常)小的嵌入式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-10-27 05:48:07

为了保留空格,应该引用变量:

b=$(echo -e "${smtp_0} ${smtp_4} ${smtp_5}")
echo "$b"

但是,为什么要使用echo

b="${smtp_0} ${smtp_4} ${smtp_5}"
echo "$b"

另外,您应该在代码中使用缩进(或者如果您是这样,那么您应该在发布问题时保留它)。

To preserve whitespace, variables should be quoted:

b=$(echo -e "${smtp_0} ${smtp_4} ${smtp_5}")
echo "$b"

However, why are you using echo?

b="${smtp_0} ${smtp_4} ${smtp_5}"
echo "$b"

Also, you should use indenting in your code (or if you are then you should retain it when posting questions).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文