Shell script - insert newline into string

View: New views
8 Messages — Rating Filter:   Alert me  

Shell script - insert newline into string

by Tony Wilson-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I'm kind of embarrassed to ask this but I'm stuck!

I'm writing a shell script and need to insert a line feed into a string. For some reason \n does not work on the NSLU2.
Nor does stuff like:

message="Subject:Test

Body of message here"

(ie newline within string)

Echo is not adding a new line when used to extend the string.

Please help!!!!

Tony


Re: Shell script - insert newline into string

by Kumar Appaiah-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Jun 14, 2009 at 10:37:11AM -0000, Tony Wilson wrote:

>    Hi
>
>    I'm kind of embarrassed to ask this but I'm stuck!
>
>    I'm writing a shell script and need to insert a line feed into a string.
>    For some reason \n does not work on the NSLU2.
>    Nor does stuff like:
>
>    message="Subject:Test
>
>    Body of message here"
>
>    (ie newline within string)
>
>    Echo is not adding a new line when used to extend the string.

Here's one way:

[kumar@bluemoon ~] echo -e "A\nB\nCDE\nF"
A
B
CDE
F

i.e., the use of "-e" recoginized backslash escape sequences.

HTH.

Kumar
--
Kumar Appaiah

Re: Shell script - insert newline into string

by Thomas Reitmayr-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

message="abc
def"
echo "$message"

should work. Did you put the quotes around $message in your echo
command?
-Thomas

Am Sonntag, den 14.06.2009, 10:37 +0000 schrieb Tony Wilson:

>
>
> Hi
>
> I'm kind of embarrassed to ask this but I'm stuck!
>
> I'm writing a shell script and need to insert a line feed into a
> string. For some reason \n does not work on the NSLU2.
> Nor does stuff like:
>
> message="Subject:Test
>
> Body of message here"
>
> (ie newline within string)
>
> Echo is not adding a new line when used to extend the string.
>
> Please help!!!!
>
> Tony
>
>
>
>
>


Re: Shell script - insert newline into string

by Tony Wilson-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In nslu2-linux@..., Kumar Appaiah <a.kumar@...> wrote:

>
> On Sun, Jun 14, 2009 at 10:37:11AM -0000, Tony Wilson wrote:
> >    Hi
> >
> >    I'm kind of embarrassed to ask this but I'm stuck!
> >
> >    I'm writing a shell script and need to insert a line feed into a string.
> >    For some reason \n does not work on the NSLU2.
> >    Nor does stuff like:
> >
> >    message="Subject:Test
> >
> >    Body of message here"
> >
> >    (ie newline within string)
> >
> >    Echo is not adding a new line when used to extend the string.
>
> Here's one way:
>
> [kumar@bluemoon ~] echo -e "A\nB\nCDE\nF"
> A
> B
> CDE
> F
>
> i.e., the use of "-e" recoginized backslash escape sequences.
>
> HTH.
>
> Kumar
> --
> Kumar Appaiah
>


That works from the command line, but not in script!

string=`echo -e "A\nB\nCDE\nF"`
echo $string

output:
A B CDE F






Re: Re: Shell script - insert newline into string

by Thomas Reitmayr-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try:

string="`echo -e 'A\nB\nCDE\nF'`"
echo "$string"

-Thomas

Am Sonntag, den 14.06.2009, 17:53 +0000 schrieb Tony Wilson:

>
>
> --- In nslu2-linux@..., Kumar Appaiah <a.kumar@...> wrote:
> >
> > On Sun, Jun 14, 2009 at 10:37:11AM -0000, Tony Wilson wrote:
> > > Hi
> > >
> > > I'm kind of embarrassed to ask this but I'm stuck!
> > >
> > > I'm writing a shell script and need to insert a line feed into a
> string.
> > > For some reason \n does not work on the NSLU2.
> > > Nor does stuff like:
> > >
> > > message="Subject:Test
> > >
> > > Body of message here"
> > >
> > > (ie newline within string)
> > >
> > > Echo is not adding a new line when used to extend the string.
> >
> > Here's one way:
> >
> > [kumar@bluemoon ~] echo -e "A\nB\nCDE\nF"
> > A
> > B
> > CDE
> > F
> >
> > i.e., the use of "-e" recoginized backslash escape sequences.
> >
> > HTH.
> >
> > Kumar
> > --
> > Kumar Appaiah
> >
>
> That works from the command line, but not in script!
>
> string=`echo -e "A\nB\nCDE\nF"`
> echo $string
>
> output:
> A B CDE F
>
>
>
>
>


Re: Re: Shell script - insert newline into string

by Brendon Oliver-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 15 Jun 2009 03:53:17 am Tony Wilson wrote:

> That works from the command line, but not in script!
>
> string=`echo -e "A\nB\nCDE\nF"`
> echo $string
>
> output:
> A B CDE F

You might be picking up the shell's built-in echo.  Try /opt/bin/echo instead
(using unslung here, so you may need to adjust the path if you have something
different).

- b.

--

 06:17:38 up 3 days, 19:48,  6 users,  load average: 1.12, 0.50, 0.18




------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/nslu2-linux/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/nslu2-linux/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:nslu2-linux-digest@...
    mailto:nslu2-linux-fullfeatured@...

<*> To unsubscribe from this group, send an email to:
    nslu2-linux-unsubscribe@...

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/


Re: Re: Shell script - insert newline into string

by LrdShaper :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Brendon Oliver-2 wrote:
On Mon, 15 Jun 2009 03:53:17 am Tony Wilson wrote:

> That works from the command line, but not in script!
>
> string=`echo -e "A\nB\nCDE\nF"`
> echo $string
>
> output:
> A B CDE F
Why the need to put echo itself in the string? Why not:
string="A\nB\nCDE\tF"
echo -e $string

Cheers!

Re: Shell script - insert newline into string

by Tony Wilson-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for your help everyone!

The problem was that I was doing:

echo ${string}

and I should have done:

echo "${string}"

So, in my actual script:

     message=`echo -e "${subject}\n${from}\n${to}\n\n${content}"`
     
    echo "$message" | msmtp ${to}


Cheers
Tony