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

标签 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

为什么打印“找不到命令”?

最佳答案

你写了 "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/

相关文章:

linux - 将带有类库的 .Net Core MVC 应用程序部署到 Ubuntu 16.04 :

python - 为什么 Gtk.Entry() 不是由 set-width-chars() 设置的

python - 为什么 dbus 服务无法正常运行?

linux - 如何查找文件中的选项卡数?

bash while 循环 : No such file or directory

linux - 无法使用 Paramiko 的 sudo 命令的交互式远程密码输入

linux - 如何读取位于 Ubuntu 终端本地的 json 文件的属性值

linux - 如何创建putty快捷方式直接跳转到ssh链?

linux - 从模板创建多个文件并即时替换内容

bash - 在 case 语句中检查 '?' 的正确方法是什么