2012-02-15 82 views
0

我有一個腳本,用於檢查文件是否存在或不使用ls命令。如果沒有文件,我會詢問用戶是否想繼續使用該腳本。在無限循環中使用讀命令的怪異行爲

我在找的是,除了從終端輸入而不是鍵盤輸入的讀命令?

這裏是我的腳本:

function isfileThere(){ 
    output=$(ls ${1} 2>&1 >/dev/null) 
    case $output in 
    *"No such file or directory"*) 
     echo "DS not found: $output"; 
     option_exitprog; $output >> DSNotFound.txt ;; 
    *) echo "DS found: $output";; 
    esac 
} 

function option_exitprog(){ 
    while : 
    do 
    echo -n "Would you like to continue (y/n)?" 
    read Answer 
    #read -n1 -p "Would you like to continue (y/n)?" Answer                                                     

    if [ ! -z "$Answer" ] ; then 
     if [ "$Answer" == "y" ] ; then 
      echo "Exiting script. Goodbye" 
      exit 1 
     elif [ "$Answer" == "n" ] ; then 
      echo "Continue With Program" 
      break 
     else 
      echo "We only accept (y/n)" 
     fi 
    else 
     echo "You have entered a null string. We only accept (y/n)" 
    fi 
    done 
} 

function get_TotalEventEntries(){ 

cat<<EOF                                                                 

####################################                                                          
#         #                                                          
#         #                                                          
#  get Total Entries   #                                                          
#         #                                                          
#         #                                                          
####################################                                                          

EOF                                                                   

    while read LINE 
    do 
    let total_DSNumber=total_DSNumber+1 

    #Check if files exist                                                             
    isfileThere ${FileDir}/*${LINE}*/*.root* 

    #print to file                                                               
    #printf "${LINE}=" >> ${Filename}                                                          
    #getEntries ${LINE} >> ${Filename}                                                          
    done < ${DSWildCardFile} 

    echo "Finished running over $total_DSNumber file(s)" 
} 

get_TotalEventEntries 
+0

其中是您的var DSWildCardFile定義?此外,如果您編輯帖子以指示預期行爲,可能會有所幫助。你看到任何錯誤消息?你的意思是接受還是期待(而不是除外)?您是否嘗試過通過在腳本頂部附近添加'set -vx'來進行調試?祝你好運。 – shellter 2012-02-15 20:06:30

+0

同上,'while'循環的輸入是'$ DSWildCardFile'擴展到的任何內容。 – tripleee 2012-02-15 20:22:40

+2

您應該更改檢查文件是否存在的方法。使用:'if [[-f $ file]]'。一般來說,如果你在腳本中使用'ls',那麼你做錯了。 – jordanm 2012-02-15 20:33:25

回答

2

的問題是在這條線:done < ${DSWildCardFile}。您無法讀取此文件中的行,並通過read和簡單重定向同時讀取用戶。要修復它,請使用更復​​雜的重定向和新的文件描述符:

while read -u 3 LINE 
do 
    ... 
done 3< ${DSWildCardFile}