I am trying to extend bash with custom code that reuses bash's capability to terminate and suspend forked processes and also bash's expansion of !.
I tried a variation of the code in BASH - using trap ctrl+c that has certain lines commented out from that solution and a few lines added for trapping ctrlz:
#!/bin/bash
# type "finish" to exit
#stty -echoctl # hide ^C
# function called by trap
other_commands() {
#printf "\rSIGINT caught "
#tput setaf 1
#printf "\rSIGINT caught "
#tput sgr0
#sleep 1
#printf "\rType a command >>> "
echo ""
}
trap 'other_commands' SIGINT
trap 'other_commands' TSTP
trap 'other_commands' SIGTSTP
#input="$@"
while true; do
printf "\rType a command >>> "
read input
[[ $input == finish ]] && break
bash -c "$input"
done
CTRL C works nicely, it kills the created process but not the bash extension.
However, CTRLZ kind of works. when I execute ls | more and then CTRLZ, it suspends the process:
dewan 1934441 0.0 0.1 9800 3144 pts/6 S+ 17:00 0:00 bash ctrlc.sh
dewan 1934442 0.0 0.1 9800 3300 pts/6 T+ 17:00 0:00 bash -c ls | more
But I cannot do anything in the bash extension after that:
initx11
j2h
j2h.bat
j2h.jar
j2hlink
jargipc
--More--
^Z
I read here (Shell Script get CTRL+Z with Trap) that sigstop cannot be trapped but sigtstp can (in case there is a difference), and as we see it is allowed by bash. Iwas wondering if there is a workaround. Uncommenting the lines from the original solution did not improve things.
A second issue is that I cannot use ! to reuse history commands in the bash extension. For example, !! does not execute the last command.
jaroeold pt2t.sed uc
jaroewithoutvt r2b
Type a command >>> !ls
bash: !ls: command not found
Type a command >>>
The ! prefix is not expanded by bash. Is there a way to make bash expand it? I can duplicate the functionality but was curious if one can devise a solution that did not require such duplication.
Thanks, and this my first stackoverflow question I believe so pardon any mistakes in formatting and contents.