LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting problems (ARG!!!) (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-problems-arg-701635/)

abharsair 02-02-2009 08:40 AM

Scripting problems (ARG!!!)
 
Hi guys, please can one of you help?

I've written a script (with some of your help ;) ), that should scan a directory for files that end in the .dat suffix and then create a .dat.lck blank file with the same name as the .dat file.

Once this has happened the script then scans the directory for the .dat files and uses a pearl ftp script to send the files to a remote server.

Now my script work if there is only one file in the directory, however if there are more than one it says there are two many arguements, can you please tell me what I am doing wrong?

Thanks in advance guys

#!/bin/bash

MAILTO=email address

FTPSCRIPT=/usr/local/bin/autoftp.pl
LOGFTP=/var/log/autoftp.log
INCOMING=/shares/billops/test/out

function file_received()
{
local WHO=$1
local FILETO=$2

echo `date` $WHO": "$TOFILE >>$LOGFTP

mail -s "C&W Test Script - $WHO: $FILETO received" $MAILTO <<!
This message is for information only, please do not reply!
!
}

function send_file()
{
local status=1
local count=2
local WHO=$1
local FILEFROM=$2
local FILETO=$3
local DESTFTP=$4
local USER=$5
local PASS=$6

# Send mail informing file has arrived for onward FTP

file_received $WHO $FILETO

# Try 3 times to send file with 5 minute interval

while [ $status -gt 0 -a $count -gt 0 ]
do
/usr/bin/perl $FTPSCRIPT $DESTFTP $USER $PASS $FILEFROM $FILETO >>$LOGFTP 2>>$LOGFTP
status=$?
count=`expr $count - 1`

if [ $status -ne 0 ]
then
sleep 300
fi
done

# Report on status of sending file

if [ $status -eq 0 ]
then
MESSAGE="sent OK"
else
MESSAGE="send FAILED!"
fi
mail -s "Test Script - $WHO: $FILETO $MESSAGE" $MAILTO <<!
This message is for information only, please do not reply!
!
}

# this next section reads and filename that ends in .dat and creates a duplicate with the suffix .dat.lck

for file in /shares/billops/test/out/*.dat; do
touch "${file%.dat}.dat.lck"
done

# this should send the .dat file to the ftp destination.

if [ -f $INCOMING/*dat ]
then
chown nobody:nobody $INCOMING/*dat
send_file $INCOMING/*dat *dat "fqdn" host user password remotedir
fi

# this should sent the .lck file to the ftp destination.

if [ -f $INCOMING/*lck ]
then
chown nobody:nobody $INCOMING/*lck
send_file $INCOMING/*lck *lck "fqdn" host user password remotedir
fi

colucix 02-02-2009 09:04 AM

Code:

if [ -f $INCOMING/*dat ]
I think this is the wrong part, because when the wildcard is expanded the statement becomes
Code:

if [ -f /path/to/some/dir/file1.dat /path/to/some/dir/file2.dat /path/to/some/dir/file3.dat ]
but you cannot test for the existence of multiple files at once. Hence, a better way is to use a for loop over the .dat file, e.g. something like
Code:

for file in $INCOMING/*.dat
do
  chown nobody:nobody $file
  send_file $file $(basename $file) "fqdn" host user password remotedir
done



All times are GMT -5. The time now is 08:30 AM.