1

I've got a bash script like this:

test.sh

#!/bin/bash

locks="/var/lock/userlocks"
lock=`basename "$0"`.exclusivelock

(
flock -x -w 5 200 || { echo "exiting. $lock already in use."; exit 1; }
echo "$lock acquired."
#do stuff

) 200>$locks/$lock

I can send all the output to a log file like this:

$ test.sh >> logfile.log 2>&1

But could I put that output redirection into the script like this?

test2.sh

#!/bin/bash

locks="/var/lock/userlocks"
lock=`basename "$0"`.exclusivelock

(
flock -x -w 5 200 || { echo "exiting. $lock already in use."; exit 1; }
echo "$lock acquired."
#do stuff

) 200>$locks/$lock >> logfile.log 2>&1

Now call the script without needing to redirect.

$ test2.sh
1

Yes. (I just ran a simplified version of your script)

Next time try before asking?

The simplified script in question:

#!/bin/bash

lock=exclusivelock

(
flock -x -w 5 200 || { echo "exiting. $lock already in use."; exit 1; }
echo "$lock acquired."

) 200>$$lock >> logfile.log 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.