I've written a bash script which captures video with DVgrab. Because of the nature of the tapes that I am digitizing, sometimes I want to quit capturing before the time that I set for DVgrab. When this is the case I press Ctrl-c and DVgrab exits cleanly, my problem is that there is additional information that the script prints in the terminal with 'echo' when the script runs till it's set time. When I Ctrl-c none of this information prints...
Is there a way to intercept the SIGINT ( I think this is what I want, but I could be wrong) and have it operate exactly as it does by default, but additionally echo a few varibles in the terminal?
Preferably I would like to do this just in my script... and not effect the way Ctrl-c functions system wide.
Not sure if it matters, but based on my reading prior to this post I wasn't sure, but I run this script in a virtual terminal in gnome on an Ubuntu (8.10) Linux machine
The bit that I wanted to add when Ctrl-c is pressed looks like this:
echo -e "\E[31;40m++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "+ Warning - Capture Ended Early +"
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\E[30;47m++ Format: $FORMAT ++ Min Remaing on $destination : $minremain "
echo "++ Deck: $deck_number ++ Customer: $customer "
echo "++ Rec Time: $tape_length ++ Tape: $tapename"
Thanks in advance!
-Starcast
#!/bin/bash
trap 'echo "Control-C disabled." ' 2
... your code here.
I ended up making it look like this, But you gave what I needed to get there, thanks.
#!/bin/bash
trap 'echo -e "\E[31;40m\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n
+ WARNING - Early Exit +\n
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\E[30;47m\n
++ Format: $FORMAT ++ Min Remaing on $destination : $minremain \n
++ Deck: $deck_number ++ Customer: $customer \n
++ Rec Time: $tape_length ++ Tape: $tapename"' 0
-Starcast
trap "some command goes here" 0
executes on shell script exit
Yes, I see that.... I just finished a 2 hr tape and my warning poped up after it ran for the full allotted time and exited normally...
I changed the -2- to -0- just guessing and i thought it was working.... ( I can't for the life of me find a listing of 'trap options' explaining what each number here would do?) I switched it because the -2- option isn't ending the script? It prints my inserted code, but doesn't bring back the prompt, it just continues the script?
---------- Post updated at 04:50 PM ---------- Previous update was at 04:27 PM ----------
Ok, I spoke too soon. the -2- option does stop DVgrab (it doesn't exit the script when i press Ctrl-c before the point in the script when DVgrab starts.... Which is fine except, I also have a function that runs a countdown in my script, and this countdown continues after Ctrl-c... The countdown function looks like this:
function countdown
{
local OLD_IFS="${IFS}"
IFS=":"
local ARR=( $1 )
local SECONDS=$(( (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2] ))
local START=$(date +%s)
local END=$((START + SECONDS))
local CUR=$START
while [[ $CUR -lt $END ]]
do
CUR=$(date +%s)
LEFT=$((END-CUR))
printf "\r%02d:%02d:%02d" \
$((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))
sleep 1
done
IFS="${OLD_IFS}"
echo " "
}
Is there something that I could include in my -trap- code that would kill this function and print my warning?
Thanks
-Starcast
Ok, so I turned my brain on and figured out my solution... I hate it when I catch myself getting lazy on here...
For anyone following the thread, this does what I described needing
trap 'CUR=$END
sleep 1
echo -e "\E[31;40m\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\n+ WARNING - Early Exit +
\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\E[30;47m\n++ Format: $FORMAT ++ Min Remaing on $destination : $minremain
\n++ Deck: $deck_number ++ Customer: $customer
\n++ Rec Time: $tape_length ++ Tape: $tapename"
exit ' 2
Oh, I added the 'sleep' to account for DVgrab's exit info that it prints... otherwise I was getting a few lines printed after the warning...
And I added 'exit' at the end so that the script didn't continue after killing the countdown function
-Starcast