相关疑难解决方法(0)

如何遍历文件的行?

说我有这个文件:

hello
world
hello world
Run Code Online (Sandbox Code Playgroud)

这个程序

#!/bin/bash

for i in $(cat $1); do
    echo "tester: $i"
done
Run Code Online (Sandbox Code Playgroud)

产出

tester: hello
tester: world
tester: hello
tester: world
Run Code Online (Sandbox Code Playgroud)

我希望for对每一行进行迭代,分别忽略空格,即最后两行应替换为

tester: hello world
Run Code Online (Sandbox Code Playgroud)

使用引号for i in "$(cat $1)";会立即i分配整个文件。我应该改变什么?

shell control-flow

71
推荐指数
3
解决办法
27万
查看次数

标签 统计

control-flow ×1

shell ×1