修改bash脚本里面$ READLINE_LINE和$ READLINE_POINT值 [英] Modify $READLINE_LINE and $READLINE_POINT values inside bash script

查看:131
本文介绍了修改bash脚本里面$ READLINE_LINE和$ READLINE_POINT值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回答问题时<一个href=\"http://stackoverflow.com/questions/25076611/bash-expand-cd-with-shortcuts-like-zsh/25096279#25096279\">this问题我用了一个很凌乱绑定函数根据我的 automplete.sh 脚本。

When answering this question I used a very messy bind function to modify the current line according to the result of my automplete.sh script.

当我结束了使用这个脚本我私人使用的,我试图简化绑定,并尝试直接完成所有的阅读和修改脚本中。

As I ended up using this script for my personal use, I tried to simplify the bind and try to do all the reading and modification directly inside the script.

到目前为止,我可以访问 $ READLINE_LINE $ READLINE_POINT 并提取我需要的信息,但我可以T替换当前值。 Quotting 绑定普通话页,这应该工作,虽然:

So far, I can access $READLINE_LINE and $READLINE_POINT and extract the informations I need, but I can't replace the current value. Quotting bind mand page, this should work though :

当执行壳命令,壳设置READLINE_LINE变量readline的线缓冲器和READLINE_POINT可变插入点的当前位置的内容。如果执行的命令更改READLINE_LINE或READLINE_POINT的价值,这些新值将反映在编辑状态

When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the readline line buffer and the READLINE_POINT variable to the current location of the insertion point. If the executed command changes the value of READLINE_LINE or READLINE_POINT, those new values will be reflected in the editing state

我必将我素文字与绑定-x'\\ t的:autocomplete.sh ,也确实是这样的:

I bound my scipt with bind -x '"\t":autocomplete.sh', and did something like this :

#!/bin/bash
#autocomplete.sh
BASE="${READLINE_LINE% *} "
LAST_CMD="${READLINE_LINE##* }"
EXPANSION=($(magical_autocomplete $LAST_CMD))
#we store the desired value for the line in ${EXPANSION[0]}
[[ ${#EXPANSION[@]} -gt 1 ]] && echo ${EXPANSION[@]:1} #we echo the match if there are more than 1

#Doesn't work, even with simple values like READLINE_LINE="test" or READLINE_POINT=0
READLINE_LINE=${EXPANSION[0]}
READLINE_POINT=${#READLINE_LINE}
#echo READLINE_LINE READLINE_POINT echoes the correct value here

在脚本执行后,将显示正确的匹配,但目前的换行不更新。由于我回声ING一些信息,我不能只是重定向我在绑定部分脚本的ouptut。为什么可以从变量读不能写进去?

After execution of the script, the correct matches are displayed, but the current line feed is not updated. As I am echo-ing some informations, I can't just redirect the ouptut of my script in the bind part. Why can I read from the variables but not write into it ?

推荐答案

就由同一个原因,这是行不通的:

Just by the same reason why this won’t work:

$ export a=1
$ bash -c 'echo $a; let a++'
1
$ echo $a
1

环境变量是的遗传的,不共享。由于 autocomplete.sh 作为一个新的子进程执行的,它可以读取所有父母的变量,但不能推新值了。

Environment variables are heritable, not shareable. Since autocomplete.sh is executed as a new child process, it can read all parent’s variables, but can‘t push new values back.

要修改 READLINE_LINE READLINE_POINT 你在同一个进程来执行您自动完成 - 和功能会帮助你。

To modify READLINE_LINE and READLINE_POINT you have to execute your autocomplete in the same process – source and functions will help you.

# autocomplete.sh
# should be sourced from ~/.bashrc or something

autocomplete() {
    echo $READLINE_LINE $READLINE_POINT
    EXPANSION=($(magical_autocomplete $READLINE_LINE))
    #we store the desired value for the line in ${EXPANSION[0]}
    [[ ${#EXPANSION[@]} -gt 1 ]] && echo ${EXPANSION[@]:1}

    READLINE_LINE=${EXPANSION[0]}
    READLINE_POINT=${#READLINE_LINE}
}

绑定:

if [[ -s "$HOME/.bashrc.d/autocomplete.sh" ]]; then
    source "$HOME/.bashrc.d/autocomplete.sh" 
    bind -x '"\t" : autocomplete'
fi

这篇关于修改bash脚本里面$ READLINE_LINE和$ READLINE_POINT值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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