简体   繁体   中英

Issue with reading file content using linux

I want to read contents of a file using a linux shell script. The contents of file_list.txt is:

abc
def
ghi

And the script to read this is 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: 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. Bash will take this literally and try to execute it as a command. To print the text on the screen, you can use echo or printf.

Echo example

echo "processing" "$file"

Printf example

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? )

Notice the way I did the quotes, this prevents problems with filenames that contain special characters like stars and spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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