Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh.

#!/bin/bash
#
# Write Date to cron.log
#
echo "Begin SSI Load $(date +%d%b%y_%T)"
#
# Get the latest rates file for processing.
#
d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1)
filename=$d
export filename
echo "The file name is $filename."

When I execute ./test.sh from the command line, the variables are set and the file name is displayed.

However, when I have the script run through cron, the file name comes back blank. This gives me an indication that the directory listing statement isn't run, possibly. Is this a true observation? What would cause the variables to be set during a standalone execution and not when executed through cron? :wall:

"doesn't work in cron" is such a common question it's in our FAQ. Make sure you have a sane PATH set, so your script can find all the utilities it needs, like grep and tail. You can do this by running . /etc/profile or by just setting PATH manually.

Turns out that a slight adjustment was required. After setting the PATH and sourcing /etc/profile, I culled through some other scripts to see how ls was used. Turns out I had to prefix the ls and tail with /usr/local/bin/.

#!/bin/bash
#
# Write Date to cron.log
#
echo "Begin SSI Load $(date +%d%b%y_%T)"
#
# Get the latest rates file for processing.
#
cd /rms/ssi/data
d=$(/usr/local/bin/ls -tr *.csv | /usr/local/bin/tail -n 1)
filename=$d
export filename
echo "The file name is $filename."

This puzzle solved, until the next one. Thanks.

Or you could have put /usr/local/bin/ in your PATH, too.