2017-02-17 72 views
1

我正在寫一個簡單的bash腳本來從文本文件中讀取文件名並檢查它們是否存在於目錄中。雖然這工作正常,所有文件解析後,我陷入了無限循環。閱讀文件時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

分配

currentfile="$line.gz" 

readdo之間不屬於,不是嗎?它使情況總是如此。

移動它後do

+0

非常感謝,先生! – ScubaChris

+0

@ScubaChris:只要upvote /接受答案:) – choroba