The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[C] fgets problem with SIGINT singlal!!! hurricane86 High Level Programming 1 01-02-2009 12:04 PM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
Problem with handling SIGINT JamesGoh High Level Programming 3 02-24-2008 10:39 PM
Cannot catch SIGINT while serial break condition occurs gzz High Level Programming 13 11-23-2007 08:06 AM
AIX intercepting system calls ramkumar.pvs AIX 2 09-29-2006 03:17 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2009
Starcast Starcast is offline
Registered User
  
 

Join Date: Jan 2009
Location: Minneapolis, MN
Posts: 13
Intercepting SIGINT in a bash script

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:


Code:
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
  #2 (permalink)  
Old 07-01-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,802

Code:
#!/bin/bash
trap 'echo "Control-C disabled." ' 2
... your code here.

  #3 (permalink)  
Old 07-01-2009
Starcast Starcast is offline
Registered User
  
 

Join Date: Jan 2009
Location: Minneapolis, MN
Posts: 13
I ended up making it look like this, But you gave what I needed to get there, thanks.


Code:
#!/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
  #4 (permalink)  
Old 07-01-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,802

Code:
trap "some command goes here" 0

executes on shell script exit
  #5 (permalink)  
Old 07-01-2009
Starcast Starcast is offline
Registered User
  
 

Join Date: Jan 2009
Location: Minneapolis, MN
Posts: 13
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:


Code:
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
  #6 (permalink)  
Old 07-06-2009
Starcast Starcast is offline
Registered User
  
 

Join Date: Jan 2009
Location: Minneapolis, MN
Posts: 13
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

Code:
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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0