Using Input Parameter fiel in Cron

I am trying to use Cron to call a script. The problem is that I want to pass the script an input parameter file. Whenever I use an input file I get errors depending on how I try to direct the file. This is what the crontab file looks like:
      55 * * 3 * /home/test/cron2.scp input.txt

If I put the input paramters in the crontab file it works, but I want to be able to change the parameters easily.

I have tried to direct the file like this:
55 * * 3 * /home/test/cron2.scp /home/input.txt
55 * * 3 * /home/test/cron2.scp @/home/input.txt
55 * * 3 * /home/test/cron2.scp /home/@input.txt
55 * * 3 * /home/test/cron2.scp </home/input.txt
55 * * 3 * /home/test/cron2.scp >/home/input.txt

The errors I get are; If I use '@' to try and signify that it is a file I get a TNS error from Orcale saying that it is an invalid login/password.
 If I use >, < or just the path and file name, Unix uses the whole string as input (pathname & filename) instead of using the contents of the file.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 What errors do you get?  What do you mean by 'direct the file'?  Is it possible to post a copy of the script, an example 'input.txt' file?  What Un*x flavor are you using?
1817Author Commented:
Edited text of question
Can you post a copy of the script?  It looks like you are trying to pass the 'input.txt' file as a parameter to the script.  If so, is the script able to find the input file?  Are you trying to pass in the _contents_ of the unput file, or the file itself?  If you want to send the _contents_ of the file, then you could try something along the lines of:

55 * * 3 * /home/test/cron2.scp < `cat /home/input.txt`

(No guarantees on the quoting rules!)
If you really want to pass the name of the input file, and have the script read it, then it could be pathing/environment problems.
  So, I guess I need to know whether you want file contents, or the file itself, a look at the script itself (or at least relevant parts), and the flavor of Unix you're using (can be found by doing a 'uname -a' on the command line).  All of these things have a bearing on whether a cron job works correctly.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

1817Author Commented:
1.I want the file contents.

2. Unix Flavor: SMP_DC.OSx otis 1.1-94c079 dcosx MIServer-ES 2/256 r3000

3. Unix Script:
# cron2.scp -- cron scheduler to run SQR   #
#            files for ProAnalyst          #
#                                          #

# Assign input args to variables #

# Set up the paths required to run SQR reports #
cb:    ;export PATH

# Set up ORACLE environment variables #
ORACLE_HOME="/home/oracle/product/current"              ;export ORACLE_HOME
ORACLE_BASE="/home/oracle"                              ;export ORACLE_BASE
ORACLE_TERM="vt100"                                     ;export ORACLE_TERM
ORACLE_SID="phi1"                                       ;export ORACLE_SID
TWO_TASK="phi1.otis"                                    ;export TWO_TASK

# set up SQR environment variables #
SQRDIR="/home/sqrs/sqr_install/ora/workbench/bin"       ;export SQRDIR
SQRTOOLS=$SQRDIR                                        ;export SQRTOOLS
ESQRDIR=$SQRDIR                                         ;export ESQRDIR
ESQREDITOR=vi                                           ;export ESQREDITOR
PATH=$SQRDIR:$PATH                                      ;export PATH
ESQRTEMP=$HOME/temp                                     ;export ESQRTEMP
ESQRSTARTUP=$SQRDIR/defaults.suf                        ;export ESQRSTARTUP
ESQRUSESQR=NO                                           ;export ESQRUSESQR
VVTERM=vt220                                            ;export VVTERM
VVTERMCAP=$SQRDIR/vvtermcap                             ;export VVTERMCAP
SQRFLAGS="-s -e -i/usr/sqr/include/"                    ;export SQLFLAGS

# Execute the SQR command #
sqr $SQRDIR/pbm_reports/test/cron.sqr $db_login $path_list $path_out $db_login

The first three variables (db_login,path_list, path_out) are te variables that I want passed to it from the input file. Right now , as you can see I have them hard coded. I want the contents of the input.txt file passed to it. The contents can be passed when it is run from the command line, but not when run by cron.

 I would try a couple of things, if you haven't already.
1) One possibility is to change how you get the contents into the script.
You could make the _first_ line of your script be something like:
  Whatever you make this, it should have the following characteristics: 1. Point to the shell in which you would like the script to be run.  2. Be the first line of the file.  The '#!' portion of this line is sort of a 'magic cookie' - it is not a comment like you would normally think.  Most shells know that if the first line starts with '#!' followed by a path to a shell, then it should invoke the script using that shell, and execute from there.
  Then, in your script, open the 'input.txt' file, and read it's contents (using the commands corresponding to the shell you decide to use) into the appropriate variables.  This would negate the need to pass any parameters in at all.

2) When you say 'contents can be passed when it is run from the command line', it sounds like you are doing something like:
% /home/test/cron2.scp <db_login> <path_list> <path_out>
  Have you tried doing something like:
% /home/test/cron2.scp < `cat input.txt`
  By 'cat'ting the file, and redirecting the output of the 'cat' command, you should get the same results as if you had typed in each parameter separately on the command line.

Please let me know the results of of 2) here - I think that's really what you'd like to do.

1817Author Commented:
When I run it from the command line I do it like this:
% /home/test/cron2.scp /home/test/@input.txt

What do you mean when you say 'Open th e input file and read it's contents'. I do not think that I know how to open the file inside the script.
1817Author Commented:
When I run it from the command line I do it like this:
% /home/test/cron2.scp /home/test/@input.txt

What do you mean when you say 'Open the input file and read it's contents'. I do not think that I know how to open the file inside the script.
55 * * 3 * "/home/test/cron2.scp >/home/input.txt"
1817Author Commented:
Sigma's anwser doesn't work. I get an error saying:
sh (sh): home/test/cron2.scp >/home/input.txt: Not found
  Have you tried using 'cat' to get the contents of 'input.txt' yet?  I'd make sure it works on the command line, then try it in the crontab entry.  Again, you may have to play around with quoting rules, but I think backticks should do it (depending on your shell).
Command line:
/home/test/cron2.scp < `cat input.txt`
55 * * 3 * /home/test/cron2.scp < `cat /home/input.txt`

still curious,
1817Author Commented:
I hace tried the 'cat' with a variety of different quoting tries and Unix still thinks that the whole line is an input, instead of trying to read the contents of the file.
 Ok - back to square one (for me anyway).  You try:
% /home/test/cron2.scp /home/test/@input.txt
on the command line, and it works just fine.
  Presumably you have also tried:
% /home/test/cron2.scp < /home/test/input.txt
and it gives the same results.  Right?  Wrong?
  Then you have tried (on the command line):
% /home/testcron2.scp `cat /home/input.txt`
And that works fine also, right?  Wrong? (Oh, by the way - forgive my being an idiot.  I was telling you to use the '<' operator along with the 'cat', and I'm not sure what I was thinking...  Sorry!)

  So the question is, Why does it not work via cron?  cron uses the Bourne shell, and attempts to run all scripts in it.  When cron executes a command, it supplies a *default* set of environment variables, and runs from there.  I'm wondering how you assign input to your variables.  Is it:
db_login = $1
path_list = $2
path_out = $3

and if you print them out immediately afterword, what do you get if you're running the script via cron?  This _should_ work fine with the crontab entry:
55 * * 3 * /home/test/cron2.scp < /home/input.txt
but you get what error message (again)?
And you also tried running the script with:
as the very first line? (Actually, the path should be the full path to wherever 'sh' is located.)

Instead of trying to feed the file on the cron commandline, just get it inside the script. Here is a code byte (partially taken from the getopt man page) that will process options from a file:

# replace "opts" with /home/input.txt
ARGLIST=`cat opts`

# replace "abo:" with you list of options
set -- `getopt abo: $ARGLIST`
if [ $? != 0 ]
  echo $USAGE
  exit 2
for i in $*
  echo "Processing ARG $i"
  case $i in
    -a | -b)  echo "Found $i"; FLAG=$i; shift;;
    -o)       echo "Found $i, ARG=$2"; OARG=$2; shift 2;;
    --)       echo "DONE"; shift; break;;

You can modify this as you need, and stop struggling with the vagaries of cron which works differently than most unix tools in that it _doesn't_ feed what comes after your command to the command as an argument. Since you have to type the name of your input file into a file somewhere, put it into your script.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.