6

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.

2
  • so call read another_line.. Commented Dec 7, 2011 at 21:03
  • @Vlad Lazarenko: Thanks. Could you please give me a few more details how I would use that? I searched around read another_line, but could not find any examples. Commented Dec 7, 2011 at 21:17

1 Answer 1

8
#!/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}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for such a complete example. It works perfectly and I have learnt a few more things about Bash scripting :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.