使用read而不在终端上触发换行操作 [英] Using read without triggering a newline action on terminal

查看:105
本文介绍了使用read而不在终端上触发换行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有这个

$PROMPT=">"
while read -p "${PROMPT}" line; do
  echo -en "\r"
  some_info_printout($line)
  echo -en "\n${PROMPT}"
done

给出这样的输出

>typed input
INFO OUT ["typed input"]
>more text
INFO OUT ["more text"]
>

我想做的是read并忽略换行符操作,以便使前面的文本可以覆盖现有行

what I would like is to do a read and ignore the newline action such that preciding text can overwrite the existing line

INFO OUT ["typed input"]
INFO OUT ["more text"]
>

任何帮助将不胜感激.

推荐答案

导致read返回的 Enter 必须将光标移动到下一行.您需要使用终端转义符将其返回到上一行. 而且脚本的其余部分仍然存在一些问题.这是可行的,它应该为您提供一个更好的起点:

The Enter that causes read to return necessarily moves the cursor to the next line. You need to use terminal escapes to get it back to the previous line. And the rest of your script has some problems anyway. Here's something that works, it should give you a better starting point:

#!/bin/bash -e

PROMPT=">"
while read -p "${PROMPT}" line; do
        echo -en "\033[1A\033[2K"
        echo "You typed: $line"
done  

\033 Esc \033[1A将光标移至上一行,\033[2K删除其上的所有内容.

\033 is an Esc; the \033[1A moves the cursor to the previous line, \033[2K erases whatever was on it.

这篇关于使用read而不在终端上触发换行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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