How to echo the output of a shell command and preserve the newlines

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How to echo the output of a shell command and preserve the newlines

Ploppz
Hi. I have this line:
        echo "$(shell ~/scripts/make/getshadernames src)" | sed 's/\(.*\)/\textern char const * const \1;/g' >> src/shaders.h

getshadernames prints out one name on each line. My problem is that what is send to sed via the pipe, is only one long line - with newlines removed. How can I fix that?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: How to echo the output of a shell command and preserve the newlines

Philip Guenther-2
On Tue, Aug 25, 2015 at 10:28 AM, Ploppz <[hidden email]> wrote:
> Hi. I have this line:
>         echo "$(shell ~/scripts/make/getshadernames src)" | sed 's/\(.*\)/\textern
> char const * const \1;/g' >> src/shaders.h
>
> getshadernames prints out one name on each line. My problem is that what is
> send to sed via the pipe, is only one long line - with newlines removed. How
> can I fix that?

Uh, why are you using echo and $(shell) there instead of just
        ~/scripts/make/getshadernames src | sed 's/\(.*\)/\textern
char const * const \1;/g' >> src/shaders.h

?

Philip Guenther

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: How to echo the output of a shell command and preserve the newlines

Paul Smith-20
In reply to this post by Ploppz
On Tue, 2015-08-25 at 10:28 -0700, Ploppz wrote:
> Hi. I have this line:
>         echo "$(shell ~/scripts/make/getshadernames src)" | sed
> 's/\(.*\)/\textern
> char const * const \1;/g' >> src/shaders.h
>
> getshadernames prints out one name on each line. My problem is that
> what is send to sed via the pipe, is only one long line - with
> newlines removed. How can I fix that?

Why are you using the $(shell ...) function in a recipe?  The recipe is
already running in a shell!

Why not just:

    $$HOME/scripts/make/getshadernames src | sed ... >> src/shaders.h

?


_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: How to echo the output of a shell command and preserve the newlines

Ploppz
Ah, thanks! I was a bit confused in what degree I can rely on bash/shell functionality in make. Is the body of rules just plain sh or something else?

On Tue, Aug 25, 2015 at 7:33 PM, Paul Smith-20 [via Gnu - Make] <[hidden email]> wrote:
On Tue, 2015-08-25 at 10:28 -0700, Ploppz wrote:
> Hi. I have this line:
>         echo "$(shell ~/scripts/make/getshadernames src)" | sed
> 's/\(.*\)/\textern
> char const * const \1;/g' >> src/shaders.h
>
> getshadernames prints out one name on each line. My problem is that
> what is send to sed via the pipe, is only one long line - with
> newlines removed. How can I fix that?

Why are you using the $(shell ...) function in a recipe?  The recipe is
already running in a shell!

Why not just:

    $$HOME/scripts/make/getshadernames src | sed ... >> src/shaders.h

?


_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make



To unsubscribe from How to echo the output of a shell command and preserve the newlines, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: How to echo the output of a shell command and preserve the newlines

Paul Smith-20
On Tue, 2015-08-25 at 10:40 -0700, Ploppz wrote:
> Ah, thanks! I was a bit confused in what degree I can rely on
> bash/shell functionality in make. Is the body of rules just plain sh
> or something else?

All recipes are passed to /bin/sh (not bash, unless you set the SHELL
variable explicitly in your makefile) [*].  Make just starts the shell,
gives it the recipe, and waits to see what the exit code is.


[*] Actually if your recipe is "simple enough" make will invoke it
directly rather than starting a shell, for efficiency, but the results
should be identical.


_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

RE: How to echo the output of a shell command and preserve the newlines

Cook, Malcolm-2

> On Tue, 2015-08-25 at 10:40 -0700, Ploppz wrote:
 > > Ah, thanks! I was a bit confused in what degree I can rely on
 > > bash/shell functionality in make. Is the body of rules just plain sh
 > > or something else?
 >
 > All recipes are passed to /bin/sh (not bash, unless you set the SHELL variable
 > explicitly in your makefile) [*].  Make just starts the shell, gives it the recipe,
 > and waits to see what the exit code is.

Keep in mind when the recipe is multiple lines and you are using parallelism (-j) then each line may be executed in different shells potentially out of order, unless you have as a target

.ONESHELL:

in which case the whole recipe will be sent to a single shell

 >
 >
 > [*] Actually if your recipe is "simple enough" make will invoke it directly
 > rather than starting a shell, for efficiency, but the results should be identical.
 >
 >
 > _______________________________________________
 > Help-make mailing list
 > [hidden email]
 > https://lists.gnu.org/mailman/listinfo/help-make

_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

Re: How to echo the output of a shell command and preserve the newlines

Paul Smith-20
On Tue, 2015-08-25 at 18:35 +0000, Cook, Malcolm wrote:
> Keep in mind when the recipe is multiple lines and you are using
> parallelism (-j) then each line may be executed in different shells

It doesn't matter whether or not you use parallelism, every (logical)
line _WILL DEFINITELY_ be executed in a different shell, unless you
use .ONESHELL.

>  potentially out of order

This is not true.  The second line of a recipe will never be executed
before the first line is completed, regardless of how much or little
parallelism you use or whether or not you specify .ONESHELL.


_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make
Reply | Threaded
Open this post in threaded view
|

RE: How to echo the output of a shell command and preserve the newlines

Cook, Malcolm-2
> On Tue, 2015-08-25 at 18:35 +0000, Cook, Malcolm wrote:
 > > Keep in mind when the recipe is multiple lines and you are using
 > > parallelism (-j) then each line may be executed in different shells
 >
 > It doesn't matter whether or not you use parallelism, every (logical) line
 > _WILL DEFINITELY_ be executed in a different shell, unless you use
 > .ONESHELL.
[Cook, Malcolm]

Glad to be corrected.  
 >
 > >  potentially out of order
 >
 > This is not true.  The second line of a recipe will never be executed before the
 > first line is completed, regardless of how much or little parallelism you use or
 > whether or not you specify .ONESHELL.

[Cook, Malcolm]

again, thanks for correcting my misconception...
_______________________________________________
Help-make mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-make