使用linux读取文件内容的问题

时间:2015-12-18 10:13:40

标签: linux bash

我想使用linux shell脚本读取文件的内容。 file_list.txt的内容是:

abc
def
ghi

阅读此内容的脚本是read_file_content.sh

#!/bin/bash

for file in $(cat file_list.txt)
do
 "processing "$file
done

当我以./read_file_content.sh运行命令时,出现以下错误:

./read_file_content.sh: line 6: processing abc: command not found
./read_file_content.sh: line 6: processing def: command not found
./read_file_content.sh: line 6: processing ghi: command not found

为什么找不到此打印命令'?

1 个答案:

答案 0 :(得分:4)

你写了"processing "$file,没有任何命令。 Bash将从字面上理解并尝试将其作为命令执行。 要在屏幕上打印文本,可以使用echo或printf。

回声示例

echo "processing" "$file"

Printf示例

printf "%s\n" "$file"

(如果你要处理包含 - 和空格字符的奇怪文件名,这是推荐的方式。请参阅Why is printf better than echo?

请注意我使用引号的方式,这可以防止包含星号和空格等特殊字符的文件名出现问题。

相关问题