LinuxQuestions.org
Visit Jeremy's Blog.
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 01-08-2013, 10:35 AM   #1
stvy
LQ Newbie
 
Registered: Mar 2007
Posts: 17

Rep: Reputation: 0
calling lftp from a bash script; howto pass variables into the command syntax


Hi,

I am attempting to call the lftp program from a bash script. I would like to do something like this:

/usr/bin/lftp -c 'open -e "mirror --ignore-time --no-perms ${SRC_DIR} ${DST_DIR}" ${USERNAME}:${PASSWORD}@${IP_ADDRESS}' >> ${REPORT_DIR}/${IP_ADDRESS}-lftp-server.log 2>> ${REPORT_DIR}/${IP_ADDRESS}-lftp-server.err

However the variables are not correctly being replaced with there values which are defined elsewhere. The issue is I beleive related to the requirement for the use of ' and " charachters in the syntax of the lftp command.

Can someone propose a method to solve this problem?

Thanks,
Stvy
 
Old 01-08-2013, 02:30 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by stvy View Post
The issue is I beleive related to the requirement for the use of ' and " charachters in the syntax of the lftp command.
That's correct. Single quotes around the lftp command prevent any shell substitution. I usually do something like this:
Code:
(
 echo open ip_address
 echo user username password
 echo put -O $today $file_gnome_curr
 echo bye
) | lftp -f /dev/stdin >> lftp.log 2>&1
Just to show it echoes the commands in a subshell and pipes the list as standard input to lftp. Hope this helps.
 
1 members found this post helpful.
Old 01-09-2013, 01:43 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Read these three links for the full rundown on how the shell parses arguments. It's a vital concept in scripting, so learn it well!

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Also see the QUOTING section of the bash man page.


To start with, stop thinking about quotes as enclosures, and consider them more like toggle switches. One instance turns escaping on, and the next turns it off.

(There are a few exceptions to this. Quotes inside of subshells, embedded commands, and parameter substitutions, for example, are treated separately, i.e. running in a different parsing environment. But even then they behave normally inside their respective nestings.)

The ', or hard quote, will escape everything after it until it hits another '. The ", or soft quote, will escape everything except $,\, and `, (and "!" when history expansion is enabled) until it hits the next ". This allows variable expansion and embedded commands to continue to work inside them.

Notice too how the two quote types interact. The hard quote is escaped by soft quotes, and vice-versa. Also, since \ is still special inside soft quotes, you can manually escape any of the still-special characters listed above, as well as other "s, if necessary.


Now, in your particular case, what you need to do is format the entire block so that it comes out as a single "word" token, while also allowing the variables to expand inside it.

I think the easiest way is to simply enclose the whole command string in double quotes, then go through and backslash-escape the other double quotes inside it as needed.

I also recommend rearranging your code to separate the command you send (the data) from the command you run (the code), by storing the first one in a variable first. This will help to keep things readable and avoid some of common gotchas.

I would even do this with the output files too, to further shorten up the code for readability. And remove the "{}"'s, too. They're pointless most of the time and just make the code harder to read.

Code:
logfile="$REPORT_DIR/$IP_ADDRESS-lftp-server.log"
errfile="$REPORT_DIR/$IP_ADDRESS-lftp-server.err"

command="open -e \"mirror --ignore-time --no-perms $SRC_DIR $DST_DIR\" \"$USERNAME:$PASSWORD@$IP_ADDRESS\""

/usr/bin/lftp -c "$command" >> "$logfile"  2>> "$errfile"

PS: Since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.

Last edited by David the H.; 01-09-2013 at 01:59 PM. Reason: fixed code, I think
 
  


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] Syntax question for a command within a bash script kaplan71 Linux - Software 11 10-09-2012 07:38 PM
[SOLVED] Expect syntax issue when calling a bash sed command that uses special character '}' YortheHunter Programming 6 08-10-2012 09:31 AM
Bash Script - Passing command string between variables Devcon Programming 13 01-10-2011 10:13 AM
lftp + bash variables sycamorex Programming 4 07-16-2008 05:57 PM
bash syntax when envoking tar command in python script tangle Programming 4 02-04-2006 03:22 PM

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

All times are GMT -5. The time now is 01:21 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