我有一个bash脚本,如果提示用户而不是只是要求input,是否提供了一个用户可以编辑的行(但是全文编辑器会过度杀伤,这只是一行)
什么工具提供这个?
dialog
几乎是正确的,但我宁愿它不画整个屏幕。
内置的“读取”可以使用Readline库进行编辑,如果你给它“-e”标志:
read -e foo
在zsh中,你已经vared
:
% foo=wibble % vared foo wibble
然后光标在wibble的末尾,你可以随意编辑,当你按下enter时,你已经定义了foo
的新值。
如果你不介意使用perl,你可以使用Term :: Readline
在C中还有一个readline()实现; 并可能在其他语言。 我不知道bash的readline接口
perldoc Term :: Readline
转载如下,注意示例脚本
> Term::ReadLine(3) User Contributed > Perl Documentation > Term::ReadLine(3) > > > > NAME > Term::ReadLine - Perl interface to various "readline" packages. If no > real package is found, substitutes stubs instead of basic > functions. > > SYNOPSIS > use Term::ReadLine; > my $term = new Term::ReadLine 'Simple Perl calc'; > my $prompt = "Enter your arithmetic expression: "; > my $OUT = $term->OUT || \*STDOUT; > while ( defined ($_ = $term->readline($prompt)) ) { > my $res = eval($_); > warn $@ if $@; > print $OUT $res, "\n" unless $@; > $term->addhistory($_) if /\S/; > } > > DESCRIPTION ...
在bash中,
input=“编辑此”
echo -en“$ input \ r”; 阅读答复;
echo $ reply $ {input:$ {#reply}}
注意echo中的\ r(回车)。 最后一行的括号voodoo打印回复加上你没有擦除的input的剩余部分。
它不是理想的 – 你基本上覆盖inputvariables,你不真的编辑它。
$ 2c,* -pike