繁体   English   中英

如何在shell脚本中添加缩进

[英]How to add indentation in shell script

我写了以下脚本:

while read ligne;
do 
    echo ${ligne} >> /tmp/test.conf; 
    other code lines but it's not our probem.
done  < <(cat file.conf | sed -ne '/toto/,$p');

file.conf包含类似的数据:

test1 {
  var2 {}
}
toto {
  var1 {
    next {}
  }
}

该脚本必须写在文件/tmp/test.conf中

toto {
  var1 {
    next {}
  }
}

与缩进。

今天我到达有这个结果:

toto {
var1 {
next {}
}
}

我尝试通过添加IFS变量来修改脚本,如下所示:

(IFS='\n';
while read ligne;
do 
    echo ${ligne} >> /tmp/test.conf; 
    other code lines but it's not our probem.
done  < <(cat file.conf | sed -ne '/toto/,$p'));

我有缩进,但结果中的所有n字母都已删除。

toto {
  var1 {
    ext {}
  }
}

为什么? 我该如何解决?

您可以在read前将这个循环与IFS=一起使用:

while IFS= read -r ligne; do
   echo "$ligne" >> /tmp/test.conf
done < <(sed -ne '/toto/,$p' file.conf)

否则,请确保内部变量REPLY

while read; do
   echo "$REPLY" >> /tmp/test.conf
done < <(sed -ne '/toto/,$p' file.conf)

IFS必须设置为空字符串,而不是换行符:

(IFS='';
while read ligne;
do 
    echo ${ligne} >> /tmp/test.conf; 
    # other code lines but it's not our probem.
done  < <(cat file.conf | sed -ne '/toto/,$p'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM