principal-ideal-domain principal-ideal-domain - 4 months ago 19
Bash Question

At job asking for time

#!/bin/bash

at now +1 minutes -f ./test.sh

logFile="/home/.../testLog.txt"

time1="114000"
time2="153000"
currentTime=`date +"%H%M%S"`

echo "" >> "$logFile"
date >> "$logFile"
echo "$currentTime" >> "$logFile"
echo "" >> "$logFile"


if [[ "$currentTime" < "$time1" || "$currentTime" > "$time2" ]]
then
echo "case1" >> "$logFile"
else
echo "case2" >> "$logFile"
fi


This script is saved in a file called
test.sh
. When I execute it at for example 5 pm it writes
case1
into my log file. But for each subsequent
at
call (which takes place every minute) the script writes
case2
into my log file. Can you explain why?

Answer

It's failing because at runs its jobs with sh while your script requires bash.

You can confirm this by looking at the errors you're currently discarding. You'd find that the if condition gives something like this:

sh: cannot open 114000: No such file
sh: [[: not found
sh: 111405: not found

and due to this failure it will write case2 whenever it's being scheduled by at instead of executed directly.

Use a loop or cron instead.