Omar
Omar

Reputation: 220

Bash user prompt while reading file

I am trying to create a user prompt while reading a file a line by line in Bash. The idea is for me to plot various files one-by-one using Gnuplot. Here is what I have:

#!/bin/bash
echo "Enter filename that contains the filenames:"
read fname
xr="[1e8:1e20]"
yr="[1:1e13]"

while read line
do
  echo -e "reset\nset log\nset xrange$xr\nset yrange$yr\nset xlabel \"Frequency [Hz]\"\nset ylabel \"F_{/Symbol n} [Jy Hz]\"\nset key top left\nplot \"$line.dat\" u 3:(\$3*\$4)*1e26 w l ti \"$line^o\" \n"> plt.gp
gnuplot plt.gp
done < $fname

I would like to enter a user input/"continue?" type thing before "gnuplot plt.gp" command, because at the moment it just plots everything rapidly and then exits. The standard read -p command does not work here. I read somewhere I may need to use file descriptor exec 5 command, but I don't understand. Thanks.

Upvotes: 6

Views: 1791

Answers (1)

user405725
user405725

Reputation:

#!/bin/bash

read -p 'Enter filename that contains the filenames: ' fname

xr="[1e8:1e20]"
yr="[1:1e13]"

while read line
do
    echo -e "reset\nset log\nset xrange$xr\nset yrange$yr\nset xlabel \"Frequency [Hz]\"\nset ylabel \"F_{/Symbol n} [Jy Hz]\"\nset key top left\nplot \"$line.dat\" u 3:(\$3*\$4)*1e26 w l ti \"$line^o\" \n"> plt.gp
    gnuplot plt.gp

    read -p 'Do you want to continue? [Y/n]: ' want_to_continue </dev/tty
    case "${want_to_continue}" in
    Y|y)
        continue
        ;;
    *)
        echo "OK. Bye!"
        break
        ;;
    esac
done < ${fname}

Upvotes: 8

Related Questions