首页 文章

读取文件时Bash脚本无限循环

提问于
浏览
1

我正在编写一个简单的bash脚本来从文本文件中读取文件名,并检查它们是否存在于目录中 . 虽然这很好,但在解析了所有文件后,我陷入了无限循环 .

#!/bin/bash
if [ "$1" = "" ]; 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=$1
        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.

我知道解决方案必须非常简单,但是在早上的大部分时间里我一直在烦恼!任何帮助赞赏!

1 回答

  • 1

    分配

    currentfile="$line.gz"
    

    不属于 readdo ,是吗?它使条件始终如一 .

    do 之后移动它 .

相关问题