进程替换删除换行符并替换为空格?

阿萨兹
#!/bin/bash

declare -a org
while IFS=$'\n' read -r line
do

    org[0]+=$line+"\n"
done < <(iptables -L INPUT -v -n )

echo "${org[@]}"

我得到的输出是

Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination         47668 9923K RH-Firewall-1-INPUT  all  --  *      *       0.0.0.0/0            0.0.0.0/0

事实上它应该在哪里

iptables 输出

使用 set -x 更新

+ echo -e 'Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination         48315 9974K RH-Firewall-1-INPUT  all  --  *      *       0.0.0.0/0            0.0.0.0/0           '
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target     prot opt in     out     source               destination         48315 9974K RH-Firewall-1-INPUT  all  --  *      *       0.0.0.0/0            0.0.0.0/0
外挂

首先,您将 org 声明为一个数组,但在循环结束时尝试输出所有元素 ( [@]) 时仅使用循环中的第一个元素bash 中的第二个 "\n" 是由 2 个字符 '\' 和 'n' 组成的字符串。它没有特别的意义。正如@Thomas 所说, echo -e 将解释\n为换行符。

要添加到数组,请参阅问题SO 1951506

您的明显目标是简单地读取和回显输入中的所有行。使用猫。或者更好,只是iptables -L INPUT -v -n命令的简单输出它会输出你想要的。

@Thomas 的回答会产生正确的输出。但是如果你喜欢比简单地运行已经产生正确输出的命令更复杂的事情,你也可以这样做:

declare -a org
while IFS=$'\n' read line
do
    org+=( "$line" )
done < <( iptables -L INPUT -v -n )
printf '%s\n'  "${org[@]}"

或者这个,如果你想在每个数组元素中嵌入 \n 。

declare -a org
nl=$'\n'
while IFS=$nl read line
do
    org+=( "$line$nl" )
done < <( iptables -L INPUT -v -n )
echo  "${org[@]}"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章