Printing and redirecting files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing and redirecting files
# 1  
Old 01-05-2010
[ SOLVED ] Printing and redirecting files

Hello,

I am curious. If I have a line of code run in bash
Code:
VAR="
It
is
a
nice
shiny
day
"

and I would like to be able to print this with the newlines in tact, is there a way? Are the newlines actually there but stripped by
Code:
echo $VAR

or
Code:
echo -e $VAR

?

In Python you can:
Code:
>>>VAR = """
Hello there
how are you
doing today
with things?
"""

>>>VAR
'Hello there\nHow are you\ndoing today\nwith things?\n'
>>>print (VAR)
Hello there
How are you
doing today
with things?

>>>

Notice that the newlines are actually present when displaying VAR in the interactive shell

I'm mainly using this concept to generate DOC strings for each function I'm writting. Each function adds to this DOC string. Currently, I'm hand-formatting with
Code:
DOC="function [-a]\n\ndoes so and so\n\n-a\tdoes what arg -a is supposed to do

then running
Code:
echo -e $DOC

when I want to print this.

I'd love to be able to:

Code:
DOC="
function [-a]

does so and so

-a(tab goes here but web editor won't let me) does what arg -a is supposed to do)

note: returns 0 if successful returns 1 if not
"
echo $DOC

and have this print this to screen with newlines.

I would like to get for variables what you can get with the below for generating files.

Code:
cat > $SOURCELIST << EOF
$REPO
$SRC
EOF

vs

Code:
echo -e "${REPO}\n${SRC}" > $SOURCELIST

to put a .list file in /etc/apt/sources.list.d for example

For a more complex example, this:

Code:
cat > print_hello_then_ls_a_dir << EOF
#! /bin/bash
#

echo -e "\n\nHello world.\n\nHow are you today?
ls /etc/apt
EOF

is much easier than

Code:
echo -e "#! /bin/bash\n#\n\necho -e \"\\\n\\\nHello world.\\\n\\\nHow are you today?\"\nls /etc/apt" > print_hello_then_ls_a_dir

There's got to be a similar trick for creating variables and echoing them short of making a bunch of /tmp files and then cat'ing them using a similar technique as above (and IMHO makes the code much more readable)

Could someone please point me in the right direction?

Am I just asking too much from the shell?

With thanks,
Narnie

Last edited by Narnie; 02-09-2010 at 08:01 PM..
# 2  
Old 01-05-2010
set the IFS field to newline
Eg:-
OIFS=$IFS
IFS=:

replace : with new line


HTH,
PL
# 3  
Old 01-05-2010
The newlines are in the value of VAR, but after the shell substitutes VAR's value, the resulting string is split into words. The characters in the value of the IFS (internal field separator) variable -- which defaults to space, tab, newline -- determine which characters delimit words. So, your newlines are used to split the string into words which are then passed to echo as its arguments. What you see is the end result is echo doing its job, which is to print each of those words separated by a space and followed by a newline.

Solution: Wrap the variable in double quotes to prevent the word splitting step from occurring.

Code:
echo "$VAR"

Regards,
alister

Last edited by alister; 01-05-2010 at 09:57 PM..
# 4  
Old 01-06-2010
Quote:
Originally Posted by alister
The newlines are in the value of VAR, but after the shell substitutes VAR's value, the resulting string is split into words. The characters in the value of the IFS (internal field separator) variable -- which defaults to space, tab, newline -- determine which characters delimit words. So, your newlines are used to split the string into words which are then passed to echo as its arguments. What you see is the end result is echo doing its job, which is to print each of those words separated by a space and followed by a newline.

Solution: Wrap the variable in double quotes to prevent the word splitting step from occurring.

Code:
echo "$VAR"

Regards,
alister
Wow! I feel pretty stupid now. Why didn't I think about quoting? Works like a charm. Can't wait to use this in new apps!"

Also, I' will try daptal's approach as well.

Thanks both for replying.

Happy New Year,
Narnie
# 5  
Old 01-06-2010
Quote:
Originally Posted by Narnie
I'd love to be able to:

Code:
DOC="
function [-a]

does so and so

-a(tab goes here but web editor won't let me) does what arg -a is supposed to do)

note: returns 0 if successful returns 1 if not
"
echo $DOC

and have this print this to screen with newlines.
By the way, Narnie, unless you want the shell to expand variables and do command substitution insde your python function docstrings, use single quotes to ensure that all characters within it are taken literally. The only caveat being that you need to replace occurrences of ' with '\'' to get them in (this closes the single quoted string, backslash escapes a single quoted string, then reopens the single quoted string ... if you get that then you got shell quoting down Smilie).

Code:
DOC='
function docstring with embedded newlines and
embedded '\''single quotes'\'' works fine without
accidentally expanding something that looks to the shell like
a $var or a command $(rm importantfile)'

echo "$DOC"

Using double quotes for assigning to DOC would have caused the shell to expand $var (if unset, a null string would have taken its place) and remove importantfile. I mention this just in case.

Happy New Year to you as well.

Take care,
alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirecting the results to different output files

Hi All, I am trying a shell script and need your help on taking the results to different output files. I have tried the below code: nawk ' {CNF = (length()-10)/7 printf "%9s", substr ($0, 1, 9) for (i=0; i<=CNF; i++) T = substr ($0, 10+i*7, 7) TMP = 100 - (T + T + T + T + T + T + T + T... (24 Replies)
Discussion started by: am24
24 Replies

2. Post Here to Contact Site Administrators and Moderators

Redirecting grep output to multiple files

Hi All, I am trying to redirect the grep output to multiple files, can you please help with that. Below is the command im using to match my pattern grep \<proxyType\>$PxyType $DIR/EndureFiles.json > File_Name*.json Note : $DIR and $PxyType is already defined in my script Im able... (0 Replies)
Discussion started by: Deena1984
0 Replies

3. Shell Programming and Scripting

Redirecting stdout to variable while printing it

Hi everybody, I am trying to do the thing you see in the title, and I can't simply do a=$(svn up) echo $a because the program (svn) gives output on lots of lines and in the variable the output is stored on only one line (resulting in a horribly formatted text). Any tips? Thanks,... (2 Replies)
Discussion started by: ocirne94
2 Replies

4. Shell Programming and Scripting

Redirecting to different output files with awk.

Well, it didn't take me long to get stumped again. I assure you that I'm not mentally deficient, just new to scripting. So, here's the gist. I want to redirect output from awk based off of which branch of an if-else statement under which it falls. #!/bin/bash #some variables... (2 Replies)
Discussion started by: mikesimone
2 Replies

5. Shell Programming and Scripting

Redirecting columns from multiple files

Hi all, I have two years worth of daily files (e.g. 090107.dat) and I'd like to write a BASH script to extract the same 2 columns from each of these files and copy them all to one separate file. Could anyone please point me in the right direction as I'm new to shell scripting? It would... (3 Replies)
Discussion started by: larrymuli
3 Replies

6. Shell Programming and Scripting

Redirecting echo output to 2 flat files

Hello all, I have the following 2 questions.. 1) I would like to capture the output of an echo command to 2 different files at the same time. This does not seem to work. Any ideas? echo ==== started on `date` ==== >> res1.log res2.log 2) Would it be possible to just get the 5th... (2 Replies)
Discussion started by: luft
2 Replies

7. UNIX for Dummies Questions & Answers

awk and redirecting to files

Hello, I have a set of data (comma seperated) that I want to save to multiple files according to one of the fields in the data set. I can easily do this with the following script: BEGIN { FS = OFS = ","} NF {print $0 >> ($2 "" ".csv")} It works perfectly on a set of dummy data I have set... (8 Replies)
Discussion started by: pfft
8 Replies

8. Programming

execl() + redirecting output to text files

Im currently using execl() to run the ls command and redirect the output to a text file. Unfortunately, when I check the text file, no information has been written to it. I am calling execl() with the ls command like this execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 ); ... (5 Replies)
Discussion started by: JamesGoh
5 Replies

9. Shell Programming and Scripting

Redirecting list of files to cp

I am trying to take a list of filenames and copy those files from one directory into another. Is there a way I can do a 'more' of the file with the filenames and redirect or pipe the output into a 'cp' command. I am using a bash shell. I apologise in advance for what is probably a very simple... (2 Replies)
Discussion started by: markp
2 Replies

10. UNIX for Dummies Questions & Answers

Redirecting output to multiple log files?

If I wanted to redirect output to multiple log files, what would be the best way to do that? echo "Unix is awesome" >>unixgod.log >>unixgod.log (3 Replies)
Discussion started by: darthur
3 Replies
Login or Register to Ask a Question