如何删除在bash字符读-d多行文本输入? [英] How to delete a character in bash read -d multiline input?

查看:149
本文介绍了如何删除在bash字符读-d多行文本输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写将使用bash脚本的的readline 输入查询的同时命令捕捉用户的多行输入(包列表),但我无法弄清楚如何删除(反斜杠)字符的文本(让我们假设我犯了一个错误)。下面是code我暗示的一部分。

I'm writing a bash script that will use readline command to catch user's multiline input (list of packages), but I can't figure out how to delete (backslash) characters while inputing text (let's suppose I made a mistake). Below is the part of the code I'm implying.

read -d `echo -e "\e"` -p $'Give me a list:\n' PACKAGES

\\ e是转义字符退出多行文本输入。在这里,我得到了什么,当我尝试删除字符(我尝试了所有我知道的方法:ALT +反斜杠,Ctrl + W;按Ctrl + U):

\e is the escape character to exit multiline input. Here what I got when I try to delete a character (I did tried all the methods I know: alt+backslash,ctrl+w;ctrl+u):

# read -d `echo -e "\e"` -p $'Give me a list:\n' vPACKAGES
Give me a list:
line one
line two
line 3^H^W^U^H^

正如你所看到的^ H反斜线怎么PTED,^ W = Ctrl + W不及物动词$ P $,所以我无法删除任何字符只的逃跑的逃脱。我也试图与读标志玩,-e让我删除字符,但输入的变得不再是线路断路器。

As you can see ^H is how backslash was intrpreted, ^W =ctrl+w, so I'm unable to delete the any character only escape with escape. I did tried to play with read flags, -e letting me delete characters but enter becomes no longer a line breaker.

感谢您提前为您的建议和帮助。

Thank you in advance for your advices and help.

推荐答案

我不会做这这样的。我只想提示输入值在一个循环,直到你得到一个空项或其他一些分隔符。

I wouldn't do this this way at all. I would just prompt for values in a loop until you get an empty entry or some other delimiter.

像这样(未经):

pkgs=()
printf 'Give me a list:\n'
while read -e pkg; [ -n "$pkg" ]; do
    # Validate package, etc., etc.
    pkgs+=("$pkg")
done
# Use `${pkgs[@]}` ...

您可以使用任何其他的测试,而不是 [-n$ PKG] 如果你有一些其他终端进入。 (例如: [$ PKG=。] 来测试一个等)

You can use any other test instead of [ -n "$pkg" ] if you have some other terminating entry. (e.g. [ "$pkg" = "." ] to test for a ., etc.)

或者,正如格伦·杰克曼正确添加,使用bash的更新(4.0+)版本,新的 readarray 内置这里可以使用这样的:

Or, as glenn jackman correctly adds, with newer (4.0+) versions of bash the new readarray builtin can be used here something like this:

printf 'Enter package names, one per line: hit Ctrl-D on a blank line to stop\n'
readarray -t pkgs

这篇关于如何删除在bash字符读-d多行文本输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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