Unix Programming - `command`

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > `command`





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author `command`
John Smith

2006-04-02, 7:42 pm

i have the following in a script script1:
`script2 "param1" "param2" "param3"`

which generates no output.

removing the `` as in
script2 "param1" "param2" "param3"

gives output.

why the difference? I thought the `` is supposed to assign a command
to a variable. but in this case, i am not even assigning the line
`script2 "param1" "param2" "param3"`
to any variable.

thanks.

Barry Margolin

2006-04-02, 7:42 pm

In article <1143764818.604293.180860@e56g2000cwe.googlegroups.com>,
"John Smith" <wleung7@gmail.com> wrote:

> i have the following in a script script1:
> `script2 "param1" "param2" "param3"`
>
> which generates no output.
>
> removing the `` as in
> script2 "param1" "param2" "param3"
>
> gives output.
>
> why the difference? I thought the `` is supposed to assign a command
> to a variable. but in this case, i am not even assigning the line
> `script2 "param1" "param2" "param3"`
> to any variable.
>
> thanks.


`command` gets replaced with the output of the command, much the same
way that $variable is replaced with the value of the variable. It only
assigns to a variable if it appears as part of an assignment, e.g.

var=`script2 param1 param2`

but it can also appear anyplace else, e.g.

command1 `command2 param1 param2`

In this case, the output of command2 will become the parameters to
command1.

If you just do

`script2 param1 param2 param3`

then the first word of the output will be taken to be a command to run,
and the remaining words will be its parameters. E.g.

`echo more $HOME/.profile`

will display your .profile.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
John Smith

2006-04-11, 9:59 am

Barry Margolin wrote:

> `echo more $HOME/.profile`
>
> will display your .profile.


follow up question:

I have
USE_DATE=`date`
sed 's/<foo>/<foo>$USE_DATE/g' filename.txt

intended output:
<foo>20060403

current output:
<foo>$USE_DATE

please advice.

Robert Harris

2006-04-11, 9:59 am

John Smith wrote:
> Barry Margolin wrote:
>
>
> follow up question:
>
> I have
> USE_DATE=`date`


USE_DATE=`date +%Y%m%d`

> sed 's/<foo>/<foo>$USE_DATE/g' filename.txt


sed 's/<foo>/<foo>'$USE_DATE/g filename.txt

to unquote the variable $USE_DATE

>
> intended output:
> <foo>20060403
>
> current output:
> <foo>$USE_DATE
>
> please advice.
>

Burton Samograd

2006-04-11, 9:59 am

"John Smith" <wleung7@gmail.com> writes:

> USE_DATE=`date`
> sed 's/<foo>/<foo>$USE_DATE/g' filename.txt


when you use single quotes word expansion does not occure, so you
should write the above line like:

sed "s/<foo>/<foo>`date`/g" filename.txt

double quotes allow for word expansion using $ and command
substitution using ``.

--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com
Burton Samograd

2006-04-11, 9:59 am

Burton Samograd <kruhftREMOVE@gmail.com> writes:

> "John Smith" <wleung7@gmail.com> writes:
> sed "s/<foo>/<foo>`date`/g" filename.txt


plus you missed the -e so it should be:

sed -e "s/<foo>/<foo>`date`/g" filename.txt

--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com
Micah Cowan

2006-04-11, 9:59 am

"John Smith" <wleung7@gmail.com> writes:

> Barry Margolin wrote:
>
>
> follow up question:
>
> I have
> USE_DATE=`date`
> sed 's/<foo>/<foo>$USE_DATE/g' filename.txt
>
> intended output:
> <foo>20060403
>
> current output:
> <foo>$USE_DATE
>
> please advice.


shell vars don't get expanded within single-quotes. Use double-quotes.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Chris F.A. Johnson

2006-04-11, 9:59 am

On 2006-04-04, Burton Samograd wrote:
> Burton Samograd <kruhftREMOVE@gmail.com> writes:
>
>
> plus you missed the -e so it should be:
>
> sed -e "s/<foo>/<foo>`date`/g" filename.txt


That's not necessary when there's only one command.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Micah Cowan

2006-04-11, 9:59 am

Burton Samograd <kruhftREMOVE@gmail.com> writes:

> Burton Samograd <kruhftREMOVE@gmail.com> writes:
>
>
> plus you missed the -e so it should be:
>
> sed -e "s/<foo>/<foo>`date`/g" filename.txt


The -e is completely unnecessary. It's mostly useful for intermixing
with -f.

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/
Burton Samograd

2006-04-11, 9:59 am

"Chris F.A. Johnson" <cfajohnson@gmail.com> writes:

> On 2006-04-04, Burton Samograd wrote:
>
> That's not necessary when there's only one command.


oops, i stand corrected. i guess i've always just used the -e...

--
burton samograd kruhft .at. gmail
kruhft.blogspot.com www.myspace.com/kruhft metashell.blogspot.com
Chris F.A. Johnson

2006-04-11, 9:59 am

On 2006-04-03, Micah Cowan wrote:
> Burton Samograd <kruhftREMOVE@gmail.com> writes:
>
>
> The -e is completely unnecessary. It's mostly useful for intermixing
> with -f.


Or for putting multiple commands on the line, e.g.:

sed -e "s/<foo>/<foo>`date`/g" -e 's/<bar>/<bat>/g' filename.txt

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com