LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2012, 01:51 PM   #1
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Rep: Reputation: Disabled
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.
 
Old 06-06-2012, 03:19 PM   #2
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Original Poster
Rep: Reputation: Disabled
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?
 
Old 06-07-2012, 12:51 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.

Last edited by druuna; 06-07-2012 at 04:53 AM. Reason: fixed typo
 
Old 06-07-2012, 09:41 AM   #4
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] RHEL 6 cannot initiate FTP command: bash: ftp: command not found DavidDiepUSC Linux - Newbie 20 11-21-2012 04:37 AM
[SOLVED] FTP Bash help d3adc0de Programming 4 06-04-2012 10:14 AM
[SOLVED] ftp software on bash ted_chou12 Linux - Newbie 1 03-24-2011 07:48 AM
Read a text file and ftp files using bash w/out leaving the ftp prompt dj_tyr Linux - Newbie 6 10-12-2009 06:46 PM
BASH: FTP backup... elitecodex Programming 2 03-28-2004 11:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration