Variable problem in script when using crontab

Hi, this is my first shell script. Anyway i am trying to ftp files from one machine to another. The name of the script is ifsftp_orabkp. The code is as follows:

HOST="122.122.122.22"
USER="ftp_ftp"
PASSWD="ftp.222222"
Y1=`(perl -e '@y=localtime(time());printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')`
Y2=`(perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')`
D1=$(printf "$Y1" | cut -c 3-8)
D2=$(printf "$Y2" | cut -c 3-8)
cd /exports
ftp -nv $HOST << END_SCRIPT > /home/oracle/oracle_logs/ftpout_ora.txt
quote USER $USER
quote PASS $PASSWD
prompt
cd /erdbvol1/IFS_BKPS/ora_bkp
mput *$D1*
mput *$D2*
END_SCRIPT

When I try to run it using the following it works without any problems:
./ifsftp_orabkp

However when I use crontab :
45 10 * * * /home/oracle/oracle_scripts/ifsftp_orabkp >> /home/oracle/oracle_logs/ftpora.log 2>&1

ftpora.log shows the following errors:
$ cat ftpora.log
sh: /home/oracle_scripts/ifsftp_orabkp: not found
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 6: `D1=$' unexpected
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 6: `D1=$' unexpected
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 1: `$' unexpected
/home/oracle/oracle_scripts/ifsftp_orabkp: syntax error at line 6: `D1=$' unexpected

Please help!

This script is relying on the environment being set. However this is not the case if it is run from crontab. In this case probably only the PATH variable will need to be set somewhere at the beginning. It should contain the paths to all the external commands that are bing called. I do not know where your perl binary is located. Possibly this is enough:

PATH=/bin:/usr/bin

Hi, thanks for responding, I tried that but it didnt work. I also modified the script a bit to simplify:

HOST="133.133.133.33"
USER="ttt123"
PASSWD="tt22222"
PATH=/bin:/usr/bin
Y1="20091114"
D1=$(printf "$Y1" | cut -c 3-8)
cd /exports
ftp -nv $HOST << END_SCRIPT > /home/oracle/oracle_logs/ftpout_ora.txt
quote USER $USER
quote PASS $PASSWD
prompt
cd /erdbvol1/IFS_BKPS/ora_bkp
mput *$D1*
END_SCRIPT

./ifsftp_orabkp is working, however from crontab there is still the error:

syntax error at line 7: `D1=$' unexpected

Is the syntax of the D1 variable correctly

Better put a shebang (#!) with the shell name and its full path on your first line too. It may be that the default shell is not the same as the shell this script needs to be run in. It could be that it then uses a very classic Bourne shell that does not know the $(...) construct.

Hi, on the first line I added:
#!/bin/ksh

but im still getting :
syntax error at line 7: `D1=$' unexpected..

Echo your env in your script and have it run from cron.

So put this at the beginning of your script

env > /tmp/env

Have that run from cron, and then post the results. This is a simple way to trouble shoot enviornment variable problems. You can compare to how its running from cron, from when you're executing it yourself.

The cron does NOT automatically source your profile. You have to source it in your script, which is a good practice anyway.

#!/usr/bin/ksh

#
# Note this is (dot)space(tilde)/.profile
#
. ~/.profile (or whichever profile is appropriate for your default shell)

This should solve your problem.

Hope it helps. Good luck.

You can source your environment and get it over with like others suggest. But what seems strange is that printf is a built-in, so it seems to me the only thing in this statement that could trigger the error is the cut statement that is somehow not in /bin or in /usr/bin. What happens when you enter

which cut

on the command line?

A best practice for cron jobs is to always use the full path for each command and utility unless it is a builtin. For example:

Y1=`(/usr/bin/perl -e '@y=localtime(time());printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')`
Y2=`(/usr/bin/perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')`
D1=$(printf "$Y1" | /usr/bin/cut -c 3-8)
D2=$(printf "$Y2" | /usr/bin/cut -c 3-8)

I do not agree with the statement above - since it is always a bad thing to hard-code something.
Instead you might create variables holding the name of your executable or update the path. Example:

#!/bin/ksh
export PATH=${PATH}:/something
which cut
typeset BIN_CUT=/something/cut
"${BIN_CUT}" parameters

Hi everyone, my script seems to be working now! Thanks for all or you for your help! It seems that I needed the #!/bin/ksh at the beginning of the script. When I enter �$ which cut� I get �/bin/cut�. Syndex, I was just wondering, where does the output go to when I include �env > /tmp/env� in the script?

I have one more question.. I am using pearl to get today�s date. The files that I am ftp-ing have today�s date as part of the filename. I was just wondering.. is there a way to :
Select all files that were created on date = today�s Date and yesterday
(Im also considering files that do not have the date as part of the file name)

Thanks so much for your help!

You might want to use `find`.
You might want to use file creation time.
You might want to use file modification time.
You might assume that the files have some pre-defined name and then guess what files could exist.
You might try filtering the file list by something - if the file time/date is in the filename.
You should avoid gathering all the data in single huge directory.
You might consider collecting all the data into some database like Oracle.
You should know that filesystem is as well a some type of database.

Also - please read ISO-8601 (or at least most important parts from it). This should tell you how to use the date format... How would you know if the files are ex. log_2009-12-31.txt or maybe log_31-20-12-09-EDT.txt ? Avoid any assumptions. If you need to hard-code some date/time format then use the ISO-8601 preferred one (ex. 2009-12-31T23:59:59.012345-05:00:00)