LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   FTP Help (with BASH) (https://www.linuxquestions.org/questions/programming-9/ftp-help-with-bash-948863/)

d3adc0de 06-06-2012 01:51 PM

FTP Help (with BASH)
 
Hello again. :)

I've gotten most of my script working; however I am getting the following error:
Code:

Couldnt get a file descriptor referring to the console
I would very much appreciate any assistance in understanding what I am doing wrong, I searched the interwebz to no avail, so now I am appealing to you wonderful, intelligent, helpful people once again. :) Here is the script:

Code:

#!/bin/bash -xvd
host=$1
user=$2
pass=$3
dir=$4

company=$5

quser=$6
qpass=$7
qhost=$8
qdbase=$9

declare -a myarray
let count=0

#Query
myarray=($(mysql -N -u$quser -p$qpass -h$qhost -D$qdbase -e "SELECT id FROM table;"))

#start FTP
for id in ${myarray[@]}
do
        ftp -n 2>&1
        open $host << ENDFTP
        {
                print user $user $pass
                print mkdir $dir
                print cd $dir
                print binary
                print mput /dir/to/${id}/
                print echo "&nbsp;&raquo;&nbsp;<b>${id}</b><br />"
                print quit
        }
ENDFTP
done
exit 0

The query just pulls the ID out, which has folders named after the ID and we're FTPing over the folder. I get that error before it even makes it to the mkdir. It might not even be getting to the here; but I'm not 100% sure.

d3adc0de 06-06-2012 03:19 PM

It looks like it's unable to read the "open" command; which I don't understand seeing as I'm in the "FTP" mode when it sends that message. Any thoughts? Perhaps the configuration of the remote FTP server?

druuna 06-07-2012 12:51 AM

The ftp part doesn't look correct to me, try the following instead:
Code:

#start FTP
for id in ${myarray[@]}
do
  /usr/bin/ftp -in $host 2>&1 <<ENDFTP
      user $user $pass
      mkdir $dir
      cd $dir
      binary
      mput /dir/to/${id}/
      echo "&nbsp;&raquo;&nbsp;<b>${id}</b><br />"
      bye
ENDFTP
done

No need for the curly braces. I removed the print parts as well, don't think they are needed.

d3adc0de 06-07-2012 09:41 AM

I actually fixed my issue. For anyone who needs help like this in the future, here's the sample code:

Code:

#!/bin/bash/ -xvd
# ^ This sets the xtrace, verbose, and debug on.

#The following pulls in the variable sent along with the command

host=$1
user=$2
pass=$3
dir=$4

company=$5
adimgs=$6

#sample variables for MySQL query
quser='anonymous'
qpass='pass123'
qhost='myhost'
qdbase='database1'

#Declare array and set count to 0
declare -a myarray
let count=0

#remove any previously created file with this name,
#to prevent appending data prior to new ftp script generation.

rm tmp_$company.txt

#Query, to pull in product ids into array.
#Product IDs are then used to identify folders and image files.

myarray=($(mysql -N -u$quser -p$qpass -h$qhost -D$qdbase -e "SELECT id FROM table;"))

#Start FTP; generating the FTP commands, and sending the output to a text file,
#which will contain all the files to be sent via FTP.

echo "open $host" >> tmp_$company.txt
echo "user $user $pass" >> tmp_$company.txt
echo "mkdir $dir" >> tmp_$company.txt
echo "cd $dir" >> tmp_$company.txt
echo "lcd /products/" >> tmp_$company.txt

#for loop used to loop through the array, identify all the files
#to be sent via ftp, and generate the ftp commands

for id in ${myarray[@]}
do
        echo "Mirror -R /products/${id}" >> tmp_$company.txt
        #if statement, a flag used to determine whether to send the
        #product advertisement images to the company. If yes, the images are sent.

        if [ $adimgs=="yes" ]; then
                echo "cd /dir/to/${id}/" >> tmp_$company.txt
                echo "mput /product_ads/${id}*.jpg" >> tmp_$company.txt
                echo "cd $dir" >> tmp_$company.txt
        fi
done

lftp -f tmp_$company.txt

exit



All times are GMT -5. The time now is 02:53 AM.