简体   繁体   中英

How to monitor bash/zsh prompt input?

I need to view the user's prompt input in a background script. (in both zsh and bash).

For example if the user has typed

[3:30pm@home]> ssh user@23.23.23.23 ls -alh

My background process needs the string "ssh user@23.23.23.23 ls -alh" before they press enter.

$PS1 has the prompt string, but not the user input.

Is this possible?

For background, the overall goal is to take user's partial input and translate shell to english. For example, if I were to type ls -al , my $PS1 or $PS2 would change to say "list all files in a long format in the current directory". So, if someone didn't exactly remember if -X or -x enabled X11 forwarding with ssh , they can type it and see if the plain language english translation matches their expectations.

I want it to be a zsh plugin, or a script that people toss into the rc files, so there can't be anything too major done.

Do you need the input in real time ? or at least just before the line is accepted ?

For the last option, in zsh there is a widget accept-line that is triggered when the input line is accepted. You can override the default widget with you own, like this :

function accept-line
{
    #here, send the input to your script
    yourscript "$BUFFER"

    # ... do something here if you want

    # trigger the real widget to accept the line and run the input
    zle .accept-line
}
# register your new accept-line widget to replace the builtin one
zle -N accept-line

Here I used the name accept-line but if you want to name your override function my_superOverride you can override the default widget at the end with :

zle -N accept-line my_superOverride

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM