Register
It is currently Wed Oct 22, 2014 6:14 am

echo and newlines


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Thu Apr 05, 2012 11:50 am   

Joined: Thu Apr 05, 2012 11:16 am
Posts: 2
For diverse reasons I'd like sometimes to have a string with newlines in a shell variable. For example, I could keep some short scripts (like FTP commands) built during the runtime of my script and simply feed them to the "ftp" command like:
Code:
echo "$script" | ftp -n ftp.myhost.pl

That is the theory, the practical problem is that executing "echo $script" strips the $script variable of all the newlines.

Of course I could use "\n"-s and execute "echo" with an "-e" parameter in this case. Sometimes however I'd like to do some more processing on the variable but and one execution of "echo" feeds it correctly but also strips it of all the "\n"-s and the next time becomes a bit tricky.

Is there some alternative to "echo" I could use to pass variables to streams that would preserve the original newlines?


Top
 Profile  
 PostPosted: Fri Apr 06, 2012 4:18 am   

Joined: Mon Feb 16, 2009 10:07 am
Posts: 19
Location: Paris - France
Quote:
"echo $script"
does convert newlines,
Code:
echo "$script"
does not
afaik ftp uses the ~/.netrc file to programmatically operate transfers


Top
 Profile ICQ YIM  
 PostPosted: Fri Apr 06, 2012 6:18 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

you may use here-doc
Code:
ftp ... <<anyWord
   ftp_command
   ftp_command
anyWord
the last 'anyWord' has to be in the first column, don't indent it.


Top
 Profile  
 PostPosted: Fri Apr 06, 2012 7:01 am   

Joined: Thu Apr 05, 2012 11:16 am
Posts: 2
@cfeard

Thanks! That's the thing I needed!

As for ~/.netrc, I can automate logging in and write some simple FTP macros here, that's not always enough. Not to mention, I prefer to keep my projects in single places and not to spread them all over the server. And anyway, the FTP here was only an example; I encountered the need to keep a text with newlines in a variable several times in pretty diverse conditions.

@Watael

Thanks, I know the heredoc syntax and use it a lot, but that's not always a solution. Sometimes you need to build an FTP script or a MySQL script or something line by line during the execution of a bash script and every line depends on different conditions. Keeping the script beeing built in a variable seems reasonable in such a case. But having solved the newline problem, the only thing that puzzle me as a shell programmer now is what are the limits of the text I can keep in a variable... ;)


Top
 Profile  
 PostPosted: Fri Apr 06, 2012 2:25 pm   

Joined: Mon Feb 16, 2009 10:07 am
Posts: 19
Location: Paris - France
@kalmar
Code:
$ getconf ARG_MAX
2621440

gives you a fairly high limit

@watael
Code:
<<- anyWord

(using a dash after the angle brackets) allows you to indent anyWord :)


Top
 Profile ICQ YIM  
 PostPosted: Sat Apr 07, 2012 5:36 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Quote:
(using a dash after the angle brackets) allows you to indent anyWord
using tabs only.
as editors may change tabs to spaces I usually don't mention.


Top
 Profile  
 PostPosted: Sat Apr 07, 2012 7:23 am   

Joined: Mon Feb 16, 2009 10:07 am
Posts: 19
Location: Paris - France
Sorry! You better be careful !
Personally I only use vi(m) as editor for bash scripting and came not across this problem up to now.
To be honest, I seldom use here-documents. But when I do, I prefer them to be nicely integrated into the script.


Top
 Profile ICQ YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], uhelp and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP