简体   繁体   English

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

[英]Issue with reading file content using linux

I want to read contents of a file using a linux shell script. 我想使用Linux Shell脚本读取文件的内容。 The contents of file_list.txt is: file_list.txt的内容为:

abc
def
ghi

And the script to read this is read_file_content.sh : 读取此脚本的是read_file_content.sh

#!/bin/bash

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

When I run the command as ./read_file_content.sh, I get the following error: 当我以./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

Why does this print 'command not found'? 为什么打印此“找不到命令”?

You wrote "processing "$file without any command in front of it. 您在前面没有任何命令的情况下编写了"processing "$file Bash will take this literally and try to execute it as a command. Bash会从字面上理解这一点,并尝试将其作为命令执行。 To print the text on the screen, you can use echo or printf. 要在屏幕上打印文本,可以使用echo或printf。

Echo example 回声示例

echo "processing" "$file"

Printf example Printf示例

printf "%s\n" "$file"

(This is the recommend way if you're going to process weird filenames that contain - and space characters. See Why is printf better than echo? ) (如果要处理包含-和空格字符的怪异文件名,这是推荐的方法。请参见为什么printf比echo好?

Notice the way I did the quotes, this prevents problems with filenames that contain special characters like stars and spaces. 注意我做引号的方式,这可以防止包含特殊字符(例如星号和空格)的文件名出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM