I've coded as follows, as I don't know the name of my file (only the prefix), and also there may be mulitple files. I need to check all the files for '70' before I continue processing. I get the error at the bottom:
Code
icofile=`ls -l /Invoices |grep DAAS | awk '{print $9}'`
if [[ $icofile != "" ]]
then
for files in $icofile
do
if [ `tail -1 $files | cut -c7-8` -eq "70" ]
then
echo "File correct format, has record 70..." >>$logfile
else
echo "File incorrect format, missing record 70..." >>$logfile
exit 2
fi
done
echo "Sending $icofile to Server" >>$logfile
else
echo "No files to send across..." >> $logfile
exit 3
fi
Error
"./Transfer.DAAS[25]: test: 0403-004 Specify a parameter with this command."
The test command (aka [ -- really) is picky about its syntax. If the output from the backticks is the empty string, it will not see any argument at all where it expects one before the -eq. Either properly double-quote the backticks, or use a construct which is less brittle. Perhaps something like this:
if awk 'FNR == 1 { if (prevfile && prev !~ /^.{6}70/) { print prevfile; rc=1; } }
{ prevfile=FILENAME; prev=
END { if (prevfile && prev !~ /^/.{6}70/) { print prevfile; rc=1 } exit rc; }' $icofile; then
: ... send to server
fi
Have to admit to not following what you've coded above, so have stuck to the original, trying to "properly double-quote the backticks" - as follows.
for files in $icofile
do
if [ "tail -1 $icofiles | cut -c7-8" = "70" ]
then
echo "File correct format, has trailer..." >>$logfile
else
echo "File incorrect format, missing trailer..." >>$logfile
exit 2
fi
done
echo "Sending $icofile to AS400 ODIN" >>$logfile
Even though the file has '70' in positions 7/8, the script is dropping into the first 'else' (missing trailer).
Your loop variable is "file" and the list of files it loops over is $icofile, yet you use $icofiles inside the loop; is this really correct?
You need both double quotes and backticks; you can break it up into a variable assignment with backticks and a comparison of the variable, like vbe suggests, or combine them:
if [ "`tail -1 $file | cut -c7-8`" = "70" ]; then ...
(assuming you really ought to be using $file rather than $icofiles in the command).
Nice point era, I edited and corrrected also... didnt realize the typo change between the last and its previous post of daveaasmith...and I copied/pasted the last...
definitely $file (better than $files which is confusing whe taliking of one record...)
Nice one folks. I have added the missing backticks (didn't realise these were also required, rather than the double quotes replacing them). I had indeed cut/pasted into this thread incorrectly (oops), but am now running the following, successfully:-
for files in $icofile
do
if [ "`tail -1 $files | cut -c7-8`" = "70" ]
then
echo "File correct format, has trailer..." >>$logfile
else
echo "File incorrect format, missing trailer..." >>$logfile
exit 2
fi
done
echo "Sending $icofile to AS400 ODIN" >>$logfile