Score:0

run command before script exits

au flag
j0h

I have some hardware I'm testing with a bash script.

#!/bin/bash
tshwctl --setdio=65
for i in $(seq 0 1000)
do
 ./adc1
sleep 1
done
tshwctl --clrdio=65

If I press Ctrl+C before it is finished the loop, pin 65 will not be unset. (I can unset it later sure) is there a way to execute a process after pressing Ctrl+C? as in: is there a way for the script to execute tshwctl --clsdio=65 before the script exits when ctrl+C is pressed?

hr flag
Try putting it in a SIGINT trap - see for example [Can't trap (Ctrl^C) in my bash script](https://askubuntu.com/questions/524525/cant-trap-ctrlc-in-my-bash-script)
waltinator avatar
it flag
Read `man bash`, the `trap` subcommand is what you need.
Score:2
tn flag

You can use after the shebang #!/bin/bash:

trap 'tshwctl --clsdio=65 &>/dev/null' 1 2 3 15

or you can remove --clsdio from the bottom script and use:

trap 'tshwctl --clsdio=65 &>/dev/null' EXIT

to run the command in all EXIT use cases

http://mywiki.wooledge.org/SignalTrap

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.