LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-04-2012, 09:19 AM   #1
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Rep: Reputation: Disabled
FTP Bash help


Hi. Basically I am working on a bash script that will pull a list of "products" from the database, and FTP the "sell sheets" to a wholesaler. We have all these "sell sheets" in a folder on our database, so it's only a matter of pulling up the "product id" to identify which ones to FTP over and sending the file. I keep getting errors when executing; and would love some assistance in trying to figure out where I'm going wrong. My knowledge in BASH is rudimentary; so I may ask for an explanation. I have searched multiple forums, and tried several things before asking for help.

Code:
#!/usr/bin/ksh -xv
#Retrieve variable values

#ftp Variables
ftpAddr=$1
ftpLogin=$2
ftpPass=$3
ftpDir=$4
Company=$5

#Query variables
USER=$6
PASS=$7
HOST=$8
DB=$9
CMD=$10

#Start Query
pArray=($(mysql -N -u$USER -p$PASS -h$HOST -D$DB -e $CMD))

#Start FTP
exec 4>&1
ftp -nv >&4 2>&4|&
#Getting an error here where it says
#syntax error near unexpected token `&'
#on line: `ftp -nv >&4 2>&4|&'
#when I escape the & sign, I get the following error:
#line 28: &: command not found

print -p open $ftpAddr
print -p user $ftpLogin $ftpPass
print -p binary
print -p mkdir $ftpDir
print -p cd $ftpDir

for item in ${pArray[@]}
do
        print -p mput /dir/to/$item
        echo "&nbsp;&raquo;&nbsp;<b>$item</b><br />"
done
print -p quit
exit 0
 
Old 06-04-2012, 09:30 AM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
The ampersand is a metacharacter. When placed at the end of a line, it tells the shell to run the command in the background, ie: do not wait for the command to complete before continuing.

The line:
Code:
ftp -nv >&4 2>&4|&
mean to pipe the output of the ftp command to the command &, but & is not a command. That's why you're getting command not found error when you escape the &.
Try:
Code:
ftp -nv >&4 2>&4 &
if you want to run the ftp in the background or simply leave out the ampersand and wait for the ftp to complete before continuing on to the next line of code.

NOTE: The shell uses all uppercase for it's var names. To prevent confusion, you should try a different naming convention for you script var such as what you've defined for your ftp vars.

Last edited by towheedm; 06-04-2012 at 09:33 AM.
 
1 members found this post helpful.
Old 06-04-2012, 09:51 AM   #3
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by towheedm View Post
The ampersand is a metacharacter. When placed at the end of a line, it tells the shell to run the command in the background, ie: do not wait for the command to complete before continuing.

The line:
Code:
ftp -nv >&4 2>&4|&
mean to pipe the output of the ftp command to the command &, but & is not a command. That's why you're getting command not found error when you escape the &.
Try:
Code:
ftp -nv >&4 2>&4 &
if you want to run the ftp in the background or simply leave out the ampersand and wait for the ftp to complete before continuing on to the next line of code.

NOTE: The shell uses all uppercase for it's var names. To prevent confusion, you should try a different naming convention for you script var such as what you've defined for your ftp vars.
Thank you! this actually worked; however I'm getting a new error, perhaps you could assist me with? (The lines that are throwing the errors will be highlighted in red)

This is the error I'm getting: print: command not found


Code:
#!/usr/bin/ksh -xv
#Retrieve variable values

#ftp Variables
FTPHOST=$1
FTPUSER=$2
FTPPASS=$3
FTPDIR=$4

COMPANY=$5

#Query variables
QUSER=$6
QPASS=$7
QHOST=$8
QDB=$9
QCMD=$10

#Start Query
PARRAY=($(mysql -N -u$QUSER -p$QPASS -h$QHOST -D$QDB -e $QCMD))

#Start FTP
exec 4>&1
ftp -nv >&4 2>&4 &

print -p open $FTPHOST
print -p user $FTPUSER $FTPPASS
print -p binary
print -p mkdir $FTPDIR
print -p cd $FTPDIR

#loop is not working; but that's another issue entirely
for ITEM in ${PARRAY[@]}
do
        print -p mput /dir/to/$ITEM
        echo "&nbsp;&raquo;&nbsp;<b>$ITEM</b><br />"
done
print -p quit
exit 0
 
Old 06-04-2012, 10:11 AM   #4
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Stretch
Posts: 612

Rep: Reputation: 125Reputation: 125
You shebank shows you're using ksh:
Code:
#/usr/bin/ksh -xv
I've never used ksh, so can't say if 'print' is a valid command. What does:
Code:
which print
return?

Again, it is BAD practice to use all uppercase for your script variables.
 
1 members found this post helpful.
Old 06-04-2012, 10:14 AM   #5
d3adc0de
LQ Newbie
 
Registered: Apr 2012
Location: Cyberspace
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by towheedm View Post
You shebank shows you're using ksh:
Code:
#/usr/bin/ksh -xv
I've never used ksh, so can't say if 'print' is a valid command. What does:
Code:
which print
return?

Again, it is BAD practice to use all uppercase for your script variables.
I'll fix the capitalization next edit. It appears I do not have "print" in that directory. I'll have to configure the server. THANK YOU.

I love learning something new!
 
  


Reply



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
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
FTP Bash script Elguapo Programming 2 06-07-2007 01:26 PM
how to while in a ftp bash script dezeque Programming 3 06-12-2004 04:13 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:11 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