I want to invoke any command every time the line changes.

Asked 1 years ago, Updated 1 years ago, 72 views

On bash, I would like to press Enter and execute some commands when the line changes. Is there such a hook method?Specifically, it looks like this:

$cat foobar.txt
# running anything command
$ # Enter
# running anything command
$ 

This part of the #run anything command gives the impression that some command is executed.

I forgot to tell you when to run any command. If you have entered the cat foobar.txt, it would be better to run any command after the contents of foobar.txt are displayed.

bash

2022-09-30 17:18

5 Answers

If you assign a processing to the pseudosignal DEBUG with the trap command, the processing is performed before the command you enter is executed.It does not run on empty enterprises.

$trap'echo Yo'DEBUG
$ echo Hello
Yo
Hello


2022-09-30 17:18

PS1 shell variable!! It's not a hook.

"The timing is ""just before the prompt appears every time."""

PS1="$(echo'( ( ̄ ) ̄) n\n')$PS1"

*Please remove \n because some environments may cause unnecessary line breaks.

additional:

If you want to include the command you executed last time,

PS1="$(echo'( ( ̄ )ω)hohh...$(fc-ln-2|tail-n1)\n')$PS1"

*In practical use, I think I will hand it over to my own function to check and trim it.


2022-09-30 17:18

Can I use PROMPT_COMMAND? Runs whenever a prompt (PS1) appears.

$function hoge(){date;}
$ PROMPT_COMMAND=hoge
Thursday, January 15, 2015 20:39:32 JST
$ cat foobar.txt
This is foobar.txt
Thursday, January 15, 2015 20:39:35 JST
$ 
Thursday, January 15, 2015 20:39:40 JST
$ 


2022-09-30 17:18

It's not a hook, it's a script that seems to have a lot of problems, but I think I can fulfill your request by doing the following.
However, since bash almost loses its functionality, I think it's practical...

#!/bin/bash

while read-p "[auto]$" command;do
    $command
    echo auto# write command to run automatically
done


2022-09-30 17:18

As an alternative to while read...
If you can write the commands you want to execute in one line, I think you can use xargs.

cat foo.log | xargs-ish-c "echo'{}' & my-command"

The {} portion replaces the line.


2022-09-30 17:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.