Issue
I'm writting a bash wrapper to learn some scripting concepts. The idea is to write a script in bash and set it as a user's shell at login.
I made a while loop that read
s and eval
s user's input, and then noticed that, whenever user typed CTRL + C
, the script aborted so the user session ends.
To avoid this, I trapped SIGINT
, doing nothing in the trap.
Now, the problem is that when you type CTRL + C
at half of a command, it doesn't get cancelled as one would do on bash - it just ignores CTRL + C
.
So, if I type ping stockoverf^Cping stackoverflow.com
, I get ping stockoverfping stackoverflow.com
instead of the ping stackoverflow.com
that I wanted.
Is there any way to do that?
#!/bin/bash
# let's trap SIGINT (CTRL + C)
trap "" SIGINT
while true
do
read -e -p "$USER - SHIELD: `pwd`> " command
history -s $command
eval $command
done
Solution
You could use a tool like xdotool to send Ctrl-A (begin-of-line) Ctrl-K (delete-to-end-of-line) Return (to cleanup the line)
#!/bin/bash
trap "xdotool key Ctrl+A Ctrl+k Return" SIGINT;
unset command
while [ "$command" != "quit" ] ;do
eval $command
read -e -p "$USER - SHIELD: `pwd`> " command
done
trap SIGINT
The please have a look a bash's manual page, searching for ``debug'' keyword...
man -Pless\ +/debug bash
Answered By - F. Hauri - Give Up GitHub Answer Checked By - Clifford M. (WPSolving Volunteer)