Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 28 Sep 2007 15:09:52 -0600 (MDT)
From:      Warren Block <wblock@wonkity.com>
To:        jhall@vandaliamo.net
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Adding CR/LF
Message-ID:  <20070928140159.M29117@wonkity.com>
In-Reply-To: <21079.67.171.53.31.1191004462.squirrel@admintool.trueband.net>
References:  <21079.67.171.53.31.1191004462.squirrel@admintool.trueband.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 28 Sep 2007, jhall@vandaliamo.net wrote:

> I know this should be easy, but I cannot get it to work right.  Basically,
> I have a list of items, and I need to place each one on a separate line.
>
> Here is the script I am using.
> #!/bin/sh
> FILENAMES="test1 test2 test3"
> FILELIST=""
> for filename in ${FILENAMES}
> do
>        FILELIST="${FILELIST}${filename}"$'\n\r'
>        echo ${FILELIST}
> done
>
> And, here is the output I am getting.
> test1$\n\r
> test1$\n\rtest2$\n\r
> test1$\n\rtest2$\n\rtest3$\n\r
>
> The output I would like to see is:
> test1
> test2
> test3

It took me a bit to realize that what you're trying to do is go from a 
variable with a space-separated list of filenames to a variable with a 
newline-separated list.

If you don't really need that second variable but just want to show 
those names on the screen, just echo ${filename} in the loop.  echo 
appends a linefeed.  Or use printf, which can understand standard 
character escapes.

If you really want a new variable, echo the FILENAMES variable into tr 
to replace spaces with newlines.  (\r is not needed.)

String manipulation in sh is painful at best.  Any of the scripting 
languages are better at this.

-Warren Block * Rapid City, South Dakota USA



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20070928140159.M29117>