读取文件时Bash用户提示 [英] Bash user prompt while reading file

查看:57
本文介绍了读取文件时Bash用户提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Bash中逐行读取文件时创建用户提示.我的想法是使用Gnuplot一张一张地绘制各种文件.这是我所拥有的:

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

我想输入用户输入/继续?"在"gnuplot plt.gp"命令之前输入"thing",因为此刻它会快速绘制所有内容,然后退出.标准的read -p命令在这里不起作用.我在某处阅读了可能需要使用文件描述符exec 5命令的信息,但我不明白.谢谢.

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}

这篇关于读取文件时Bash用户提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆