Search code examples
bashshellkeyinteraction

Bash script that seems to press unwanted return key


I wrote a bash script that first do operations on text files, and then execute another script from within, repeating this operation in a while loop. The script that is executed from within at a certain time ask the user

'press to stop'

and wait 5 sec, if the user doesn't press return goes further. If I execute this script alone, it works fine, but if I execute it from within the other, it seems that the return key is pressed automatically and stops the execution.

How can I avoid it?

Here is an example of the script:

#!/bin/bash

pMatrixFile='file.csv'
templateFile='out.txt'



nSim=0 
while read line
  do
  ((nSim++))

# ***************Read the input file*****************************************

  scale1=$(echo $line | cut -f1 -d\;)
  scale2=$(echo $line | cut -f2 -d\;)

# ***************Write the file to be runned*********************************

  sed -e "/double Scale_EX2  = / s|scale_DOE|$scale1|g" \
      -e "/double Scale_EX6  = / s|scale_DOE|$scale2|g" \
      -e "/double Scale_EX7  = / s|scale_DOE|$scale8|g" <$templateFile >$fileName      

# ***************Launch the simulation on server*****************************
  sed -e "s|simFile|$simFile|g" <$submitTemplateFile >$submitFile      
  sed -i "s|simVisName|$simVisName|g"  $submitFile


# *************At this line we have the issue!***********
  chmod a+x $submitFile
  . ./$submitFile |tee  log 
# *******************************************************

# ***************Clean up the temporary files********************************
  rm $simFile $fileName $submitFile

  done<$pMatrixFile

$submitFile is my external script.

Thanks for the help!


Solution

  • Use a file descriptor that's different from stdin (0). Example:

    while read -u 4 line; do
        ...
    done 4< your_file.txt
    

    This would help prevent some parts of your while block to eat input from your_file.txt everytime they ask for one.