[英]Issue with reading file content using linux
我想使用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 "processing" "$file"
Printf示例
printf "%s\n" "$file"
(如果要处理包含-和空格字符的怪异文件名,这是推荐的方法。请参见为什么printf比echo好? )
注意我做引号的方式,这可以防止包含特殊字符(例如星号和空格)的文件名出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.