在无限循环中使用read命令的奇怪行为

时间:2012-02-15 19:29:31

标签: bash

我有一个脚本,使用ls命令检查文件是否存在。如果没有文件,我会询问用户是否要继续使用该脚本。

我发现的是,read命令除了终端输入而不是键盘输入?

这是我的剧本:

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

1 个答案:

答案 0 :(得分:2)

问题出在这一行:done < ${DSWildCardFile}。您无法通过read和简单重定向同时读取此文件中的行并读取用户。要修复它,请使用更复​​杂的重定向和新的文件描述符:

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