Bash 读取文件时脚本无限循环
Bash script infinite loop while reading file
我正在编写一个简单的 bash 脚本来从文本文件中读取文件名并检查它们是否存在于目录中。虽然这工作正常,但在解析完所有文件后,我陷入了无限循环。
#!/bin/bash
if [ "" = "" ]; then
echo "No input file to parse given. Give me an input file and make sure the corresponding .gz files are in the folder."
else
file=
echo "Loaded $file."
while read -r line
currentfile="$line.gz"
do
if [ -f $currentfile ]; then
echo "The file '$currentfile' exists."
else
echo "The file '$currentfile' does not exist."
fi
done < "$file"
fi
这是列出所有文件后的循环输出:
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
etc.
我知道解决方案一定很简单,但这个问题困扰了我一个上午!感谢任何帮助!
作业
currentfile="$line.gz"
不属于 read
和 do
之间,是吗?它使条件始终为真。
将它移到 do
之后。
我正在编写一个简单的 bash 脚本来从文本文件中读取文件名并检查它们是否存在于目录中。虽然这工作正常,但在解析完所有文件后,我陷入了无限循环。
#!/bin/bash
if [ "" = "" ]; then
echo "No input file to parse given. Give me an input file and make sure the corresponding .gz files are in the folder."
else
file=
echo "Loaded $file."
while read -r line
currentfile="$line.gz"
do
if [ -f $currentfile ]; then
echo "The file '$currentfile' exists."
else
echo "The file '$currentfile' does not exist."
fi
done < "$file"
fi
这是列出所有文件后的循环输出:
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
The file '.gz' does not exist.
etc.
我知道解决方案一定很简单,但这个问题困扰了我一个上午!感谢任何帮助!
作业
currentfile="$line.gz"
不属于 read
和 do
之间,是吗?它使条件始终为真。
将它移到 do
之后。