[英]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.