我想使用 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
为什么打印“找不到命令”?
最佳答案
你写了 "processing "$file
,前面没有任何命令。
Bash 会从字面上理解它并尝试将其作为命令执行。
要在屏幕上打印文本,可以使用 echo 或 printf。
echo 示例
echo "processing" "$file"
打印示例
printf "%s\n" "$file"
(如果您要处理包含 - 和空格字符的奇怪文件名,这是推荐的方法。参见 Why is printf better than echo?)
请注意我使用引号的方式,这可以防止包含星号和空格等特殊字符的文件名出现问题。
关于linux - 使用 linux 读取文件内容的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34353146/