![]() |
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? |
man bash
search for trap |
Code:
#!/bin/sh |
Thank you all,
Trap works |
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? |
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; } |
It depends on the signal, and whether you are in the process of
executing a command or not. . . Quote:
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. . . |
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). |
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" |
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). |
| All times are GMT -5. The time now is 04:59 AM. |