Bash - 迭代 fmt -w 并将行传递给函数

Bash - 迭代 fmt -w 并将行传递给函数

我有一个多行字符串,我希望首先将其格式化为每行的特定长度,然后再将每行传递给函数进行行处理。

例如

description="\
    NOTE: 
    This script should be run on a newly created server. 

    However it can also be re-run selectively even afterwards, to re configure your settings if they were messed up.

    Ready to configure the server for the first time.
"
echo "$description" | fmt -w 80 

我现在想迭代上面的输出并将每一行发送到一个函数。

我的想象是这样的(这是行不通的):

function testme() {
  for var in "$@"
  do
      echo "$var"
  done
}
echo "$description" | fmt -w 80 | testme 

有任何想法吗?

答案1

试试这个方法:

function testme() {
  while IFS='' read line
  do
      echo "$line"
  done
}
echo "$description" | fmt -w 80 | testme

相关内容