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.
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
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.
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.
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
$
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
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.
© 2024 OneMinuteCode. All rights reserved.