LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-05-2003, 07:11 PM   #1
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Rep: Reputation: 58
Bash question


I have a very simple bash script that
1. Prepares a wav.lock file
2. Starts an external program to record some audio into wav until CTRL-C is hit
3. Should delete the wav.lock file

My problem concerns step 3:
When CTRL-C is hit not only the audio recorder is stopped but the script itself, too, so step 3 is not executed and thus the lockfile is never deleted.

Is there an easy way to have the script execute a command on exit, or an other trick?

Last edited by J_Szucs; 01-05-2003 at 07:15 PM.
 
Old 01-05-2003, 07:16 PM   #2
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
man bash
search for trap
 
Old 01-06-2003, 11:20 AM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
#!/bin/sh

SigInt()
{
    echo "Caught SigInt"
}

trap "SigInt 2" INT

# Start script here
echo hello
 
Old 01-06-2003, 05:23 PM   #4
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Thank you all,

Trap works
 
Old 01-12-2003, 08:40 PM   #5
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Not that it was a great problem since my script basically works now, but I found something that I do not understand with trap:

I simply used a
trap 'rm -f lockfile' 2
line following the lock file creation.

I read somewhere in the bash manual that in this case bash traps the signal, executes the command and exits.
But it is not the case: bash seems to continue the script after execution of the command on the trap line. (So I had to change step 3 to a conditional command to avoid errors)
Did I miss something in the bash manual?
 
Old 01-12-2003, 08:59 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,953
Blog Entries: 54

Rep: Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732Reputation: 2732
No, you have to disable trap.
Here you get a msg on CTRL+C, the next CTRL+C stops the script:
SigInt() { echo "Caught SigInt"; trap INT; }
 
Old 01-12-2003, 09:04 PM   #7
moses
Senior Member
 
Registered: Sep 2002
Location: Arizona, US, Earth
Distribution: Slackware, (Non-Linux: Solaris 7,8,9; OSX; BeOS)
Posts: 1,152

Rep: Reputation: 46
It depends on the signal, and whether you are in the process of
executing a command or not. . .
Quote:
When bash receives a signal for which a trap has been set
while waiting for a command to complete, the trap will not
be executed until the command completes. When bash is
waiting for an asynchronous command via the wait builtin,
the reception of a signal for which a trap has been set
will cause the wait builtin to return immediately with an
exit status greater than 128, immediately after which the
trap is executed.
Under the "trap" section header in the man page, it basically
says that if the signal is an exit, the shell executes the trap
command and then exits, but if it's something else, it behaves
differently.
Your signal is an exit, so it should wait for a command to
execute (if necessary), then execute the given command, then
exit. There must be some minor syntax error in your code, but if
you've got it working, then. . .
 
Old 01-12-2003, 09:34 PM   #8
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Here is the script:

CEL=/home/szucs/media/zene/szetszedni
echo "" > $CEL/$1.tapeq.wav.lock
trap 'rm $CEL/$1.tapeq.wav.lock' 2
wavrec -t4000 -s 44100 -S "$CEL"/$1.tapeq.wav
if [ -f $CEL/$1.tapeq.wav.lock ]; then {
rm $CEL/$1.tapeq.wav.lock;
}
fi

File creation takes no time, so CTRL+C always stops wavrec.
Then removes the lockfile, then it tried to remove it again, until I inserted the second rm command in the if...fi block.
I do not think there is a syntax error in it, but who knows...
And yes, it basically works (it only could be two lines shorter).

Last edited by J_Szucs; 01-12-2003 at 09:36 PM.
 
Old 01-12-2003, 10:29 PM   #9
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Your script looks like it will do the job. One question though. Who checks for the existance of the lock file?? wavrec? I modifed your script to exit if it finds a lock file with the same name and made it a bit easier to maintain.
Code:
BASE_PATH="/home/szucs/media/zene/szetszedni"
WAV_FILE="${BASE_PATH}/$1.tapeq.wav"
LOCK_FILE="${WAV_FILE}.lock"

SigInt()
{
   echo "Caught signal, removing lock file"
   rm $LOCK_FILE
}
 
if [ -f ${LOCK_FILE} ]; then
   echo "Lock file [${LOCK_FILE}] exists exiting."
else
   echo "" > ${LOCK_FILE}
fi

trap "SigInt 2" INT

wavrec -t4000 -s 44100 -S ${WAV_FILE}

if [ -f ${LOCK_FILE} ]; then
   echo "application exited, removing lock file"
   rm ${LOCK_FILE}
fi
 
Old 01-13-2003, 03:19 AM   #10
J_Szucs
Senior Member
 
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126

Original Poster
Rep: Reputation: 58
Thank you crabboy, your code really looks much smarter. I just tend to be rather lazy when writing code...

As for the lockfile: it is used to avoid conflicts with my 'batch-mode' mp3 creating script (which uses command line utilities to automatically cut several albums into tracks, remove trailing silence, normalize volume and encode in tape or cd quality).
The lockfile ensures that encoding of an album cannot start while it is being recorded.
Other conflicts are unlikely since only one wav can be recorded at a time (having only one sound card and one line input).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about BASH operand Linux - Newbie 2 11-27-2005 09:49 PM
Bash Question mbjunior99 Programming 5 08-31-2005 04:33 AM
a bash question about while jiawj Linux - Newbie 3 07-14-2005 11:45 AM
bash question? shanenin Linux - Software 1 11-26-2004 12:22 PM
BASH question tpe Linux - Software 6 11-17-2004 08:59 PM


All times are GMT -5. The time now is 04:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration