2015-09-24 66 views
3

運行腳本,而我想讀while循環一樣使用文件裏逐行:而循環不在shell腳本工作從crontab中

while read Line; do 
    echo Inside outer loop 
    while read Line2; do 
      ..... 
      echo Inside inner loop 
      ..... 
    done < /absolute path of file/filename2 
done < /absolute path of file/filename 

獨立運行時,該腳本工作正常。但它從crontab運行時不會進入循環內部。

請建議可能的原因是什麼。

回答

1

第2 while while循環讀取全部輸入(「filename」的第一行除外)。您需要重定向到單獨的文件中描述:

while IFS= read -r -u3 Line; do 
    echo Inside outer loop 
    while IFS= read -r -u4 Line2; do 
      ..... 
      echo Inside inner loop 
      ..... 
    done 4< "/absolute path of file/filename2" 
done 3< "/absolute path of file/filename" 
  • 使用IFS=read -r確保線路從文件逐字宣讀。
  • read -u33< file使用特定的FD
  • 如果你的真實路徑有空間,你需要引用的文件名
+0

謝謝格倫!你的建議解決了我的問題。 –