当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

网友答案:
#!/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}
分享给朋友:
您可能感兴趣的文章:
随机阅读: