使用Bash逐行读取文件
我正在创建一个bash脚本来逐行读取一个文件,该文件稍后将按名称和日期进行格式化。我不明白为什么这段代码此时不能工作,尽管没有出现任何错误,尽管我已经尝试使用输入和输出文件名变量,使用目录查找器和导出命令使用Bash逐行读取文件,bash,unix,while-loop,line-by-line,Bash,Unix,While Loop,Line By Line,我正在创建一个bash脚本来逐行读取一个文件,该文件稍后将按名称和日期进行格式化。我不明白为什么这段代码此时不能工作,尽管没有出现任何错误,尽管我已经尝试使用输入和输出文件名变量,使用目录查找器和导出命令 export inputfilename="employees.txt" export outputfilename="branch.txt" directoryinput=$(find -name $inputfilename) directoryoutput=$(find -name $o
export inputfilename="employees.txt"
export outputfilename="branch.txt"
directoryinput=$(find -name $inputfilename)
directoryoutput=$(find -name $outputfilename)
n=1
if [[ -f "$directoryinput" ]]; then
while read line; do
echo "$line"
n=$((n+1))
done < "$directoryoutput"
else
echo "Input file does not exist. Please create a employees.txt file"
fi
导出inputfilename=“employees.txt”
导出outputfilename=“branch.txt”
directoryinput=$(查找-名称$inputfilename)
directoryoutput=$(find-name$outputfilename)
n=1
如果[-f“$directoryinput”];然后
读行时;做
回音“$line”
n=$((n+1))
完成<“$directoryoutput”
其他的
echo“输入文件不存在。请创建employees.txt文件”
fi
非常感谢您的帮助,谢谢!
注意:正如人们所注意到的,我忘记在数据传输到文件中添加$sign,但这只是在复制我的代码,我在实际应用程序中有$sign,但仍然没有结果
使用Bash逐行读取文件
逐行读取文件的最佳惯用方法是:
while IFS= read -r line; do
// parse line
printf "%s" "$line"
done < "file"
这里的意图很清楚。这只是:
n=$(<directoryoutput wc -l)
cat "directoryoutput"
done
可能应该是done
(缺少美元符号)。您可以将这些shell脚本放入shellcheck.net中,它可以很好地帮助您发现这样的问题。在本例中,它突出显示了第4行,并注意到变量已加载,但从未使用过。使用find更安全-命名为“$inputfilename”-print-quit
,这里保证只找到一个文件,而不是在变量中将多个名称串联在一起,如果必须使用find
BTW,这里的问题非常混乱。为什么do
会在循环时移回外部?为什么循环的内容没有缩进?我正在冒昧地修改它,以遵循代码流;如果操作过于繁重,请随意回滚,但请避免将循环中的符号缩进到循环前面的缩进级别。(另外,find.-name
中的
出于可移植性的原因,上面建议使用
。find.-name
是一种GNUism,但在更接近于基准POSIX规范的printf“$directoryinput”
版本中不起作用)。printf“$directoryinput”
最好写成printf'%s\n'$directoryinput“
,因此您不会将文件名(包含任意内容)视为格式字符串。(此外,在UNIX上正确终止内容行需要一个尾随换行符,因此不包含尾随换行符的内容将被忽略)。尽管文件存在,但代码不相信它存在,并返回“输入文件不存在。请创建一个“$inputfilename”文件“。我还试着在inputfilename和outputfilename变量的初始化上加上美元符号。不过剩下的看起来不错,谢谢!@SamThompson你说得对,我打错了。在计算结果的数量时,我缺少一个尾随的换行符,所以我们需要按照Charles的建议,printf”%s\n“$…”| wc-l
。如果没有\n
,即使存在一个文件,它也会计数为零:/I忘记了命令替换$(…)
会删除尾随的换行符。我已经编辑了答案。或者,使用bash,您可以$(
n=$(<directoryoutput wc -l)
cat "directoryoutput"
inputfilename="employees.txt"
outputfilename="branch.txt"
directoryinput=$(find . -name "$inputfilename")
directoryinput_cnt=$(printf "%s\n" "$directoryinput" | wc -l)
if [ "$directoryinput_cnt" -eq 0 ]; then
echo "Input file does not exist. Please create a '$inputfilename' file" >&2
exit 1
elif [ "$directoryinput_cnt" -gt 1 ]; then
echo "Multiple file named '$inputfilename' exists in the current path" >&2
exit 1
fi
directoryoutput=$(find . -name "$outputfilename")
directoryoutput_cnt=$(printf "%s\n" "$directoryoutput" | wc -l)
if [ "$directoryoutput_cnt" -eq 0 ]; then
echo "Input file does not exist. Please create a '$outputfilename' file" >&2
exit 1
elif [ "$directoryoutput_cnt" -gt 1 ]; then
echo "Multiple file named '$outputfilename' exists in the current path" >&2
exit 1
fi
cat "$directoryoutput"
n=$(<"$directoryoutput" wc -l)