【发布时间】:2021-01-28 03:56:25
【问题描述】:
我有以下output.txt 它只包含 2 列来演示:
Test1 Test1-IS-OK
Test2 Test2-IS-NOT
Test3 Test3-IS-OK
Test4 Test4-IS-OK
Test5 Test5-IS-NOT
那么我的 bash 脚本有以下代码:
#!/bin/bash
output="output.txt"
a=$(awk '{ print $1 }' $output)
b=$(awk '{ print $2 }' $output)
while IFS=" " read -r $a $b
do
echo "LOG: $a and $b"
done < "$output"
我收到以下错误:
./test.sh: line 13: read: `Test1-IS-OK': not a valid identifier
我需要这样的输出
LOG: Test1 and Test1-IS-OK
LOG: Test2 and Test2-IS-NOT
LOG: Test3 and Test3-IS-OK
LOG: Test4 and Test4-IS-OK
LOG: Test5 and Test5-IS-NOT
但代码不起作用。从文件中循环这 2 列的最佳方法是什么?有没有更简单的方法?
【问题讨论】:
-
FWIW 我投了赞成票,因为您有输入、输出、代码和问题陈述,但与 bash 标记相关的文本(将鼠标悬停在其上)明确表示
For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here.,如果您这样做了那么 shellcheck 会回答你的问题,而不是你必须在这里发布它,这样可能会导致你得到一些反对票。
标签: bash loops awk while-loop do-while