Unix Shell - One-liner for assigning variables

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > One-liner for assigning variables





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 One-liner for assigning variables
James

2006-01-16, 6:05 pm

I wish to assign A=aa, B=bb in a bash one-liner;

echo aa bb | while read A B; do dummy=1; done; echo $A $B
=> does not work.

Any idea?

James

Stachu 'Dozzie' K.

2006-01-16, 6:05 pm

On 16.01.2006, James <hslee@ind.alcatel.com> wrote:
> I wish to assign A=aa, B=bb in a bash one-liner;
>
> echo aa bb | while read A B; do dummy=1; done; echo $A $B
>=> does not work.


echo aa bb | (while read A B; do :; done; echo $A $B)

> Any idea?


If it's bash, then bashism is acceptable:
read A B <<<'aa bb'
But I don't see why do you would like to do so. Two lines form is much
more readable.

--
Feel free to correct my English
Stanislaw Klekot
Chris F.A. Johnson

2006-01-16, 6:05 pm

On 2006-01-16, James wrote:
> I wish to assign A=aa, B=bb in a bash one-liner;


Why? One-liners have only novelty value. For serious code, use
enough lines to make your code readable and maintainable.

> echo aa bb | while read A B; do dummy=1; done; echo $A $B
> => does not work.


See the FAQ: <http://home.comcast.net/~j.p.h/cus-faq-2.html#33>

--
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
James

2006-01-16, 6:05 pm

Thanks.
read A B <<<'aa bb'
works in bash3, but not in bash2.
Something similar in bash2?

James

Stachu 'Dozzie' K. wrote:
> On 16.01.2006, James <hslee@ind.alcatel.com> wrote:
>
> echo aa bb | (while read A B; do :; done; echo $A $B)
>
>
> If it's bash, then bashism is acceptable:
> read A B <<<'aa bb'
> But I don't see why do you would like to do so. Two lines form is much
> more readable.
>
> --
> Feel free to correct my English
> Stanislaw Klekot


Chris F.A. Johnson

2006-01-16, 6:05 pm

On 2006-01-16, James wrote:
> Thanks.
> read A B <<<'aa bb'
> works in bash3, but not in bash2.
> Something similar in bash2?


Read the FAQ: <http://home.comcast.net/~j.p.h/cus-faq-2.html#33>

--
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
Erik Max Francis

2006-01-16, 6:05 pm

James wrote:

> I wish to assign A=aa, B=bb in a bash one-liner;
>
> echo aa bb | while read A B; do dummy=1; done; echo $A $B
> => does not work.
>
> Any idea?


Here you go:

A=aa; B=bb

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
I love Mickey Mouse more than any woman I've ever known.
-- Walt Disney
Michael Tosch

2006-01-16, 6:05 pm

James wrote:
> I wish to assign A=aa, B=bb in a bash one-liner;
>
> echo aa bb | while read A B; do dummy=1; done; echo $A $B
> => does not work.
>
> Any idea?
>
> James
>


echo aa bb | read A B; echo $A $B

works only in ksh.

echo aa bb | (read A B; echo $A $B)

works in all shells, including bash.

Why not

A=aa; B=bb; echo $A $B

?

--
Michael Tosch @ hp : com
James

2006-01-16, 6:05 pm

My real-life problem is to assign the outout of an external program
(prog1) to variables,
so that they may be fed to another external programs (prog2, prog3
....).

Output of prog1 looks like
aa bb

I could do
A=`prog1 | awk '{print $1}'`
B=`prog1 | awk '{print $2}'`
then
prog2 $A $B

But I don't want to run prog1 twice nor save to a temp file.

James

Chris F.A. Johnson wrote:
> On 2006-01-16, James wrote:
>
> Why? One-liners have only novelty value. For serious code, use
> enough lines to make your code readable and maintainable.
>
>
> See the FAQ: <http://home.comcast.net/~j.p.h/cus-faq-2.html#33>
>
> --
> 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


Chris F.A. Johnson

2006-01-16, 6:05 pm

On 2006-01-16, James wrote:
>
> Chris F.A. Johnson wrote:

[please don't top post]
[vbcol=seagreen]
> My real-life problem is to assign the outout of an external program
> (prog1) to variables,
> so that they may be fed to another external programs (prog2, prog3
> ...).
>
> Output of prog1 looks like
> aa bb
>
> I could do
> A=`prog1 | awk '{print $1}'`
> B=`prog1 | awk '{print $2}'`
> then
> prog2 $A $B


eval $(prog1 | awk '{ printf "A=\"%s\" B=\"%s\", $1, $2 }')

Or:

set -- $(prog1)
A=$1
B=$2

Or any of many other methods.

> But I don't want to run prog1 twice nor save to a temp file.


Did you read the FAQ?

--
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
Dan Mercer

2006-01-16, 6:05 pm


"James" <hslee@ind.alcatel.com> wrote in message news:1137442518.475450.59170@g14g2000cwa.googlegroups.com...
: I wish to assign A=aa, B=bb in a bash one-liner;
:
: echo aa bb | while read A B; do dummy=1; done; echo $A $B
: => does not work.
:
: Any idea?
:
: James
:

What's wrong with

A=aa B=bb

?

Dan Mercer


Dan Mercer

2006-01-16, 6:05 pm


"Erik Max Francis" <max@alcyone.com> wrote in message news:09SdnbTKkNTUg1HeRVn-pw@speakeasy.net...
: James wrote:
:
: > I wish to assign A=aa, B=bb in a bash one-liner;
: >
: > echo aa bb | while read A B; do dummy=1; done; echo $A $B
: > => does not work.
: >
: > Any idea?
:
: Here you go:
:
: A=aa; B=bb

The semi-colon is totally unnecessary. You can concatenate as
many variable settings as line length will allow you.

Dan Mercer
:
: --
: Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
: San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
: I love Mickey Mouse more than any woman I've ever known.
: -- Walt Disney


Stephane Chazelas

2006-01-17, 8:03 am

On Mon, 16 Jan 2006 23:39:20 GMT, Dan Mercer wrote:
>
> "Erik Max Francis" <max@alcyone.com> wrote in message news:09SdnbTKkNTUg1HeRVn-pw@speakeasy.net...
> : James wrote:
> :
> : > I wish to assign A=aa, B=bb in a bash one-liner;
> : >
> : > echo aa bb | while read A B; do dummy=1; done; echo $A $B
> : > => does not work.
> : >
> : > Any idea?
> :
> : Here you go:
> :
> : A=aa; B=bb
>
> The semi-colon is totally unnecessary. You can concatenate as
> many variable settings as line length will allow you.

[...]

That's right, unless the order is important.

In

A=aa B=bb

A is not always assigned before B, depending on the shell.

It matters in:

A=a B=$a

for instance. That's something to be aware of.

~$ sh -c 'A=b; A=a B=$A; echo $B'
b
~$ ksh -c 'A=b; A=a B=$A; echo $B'
a
~$ zsh -c 'A=b; A=a B=$A; echo $B'
a

(where "sh" above is a Bourne shell).

I couldn't find anything in SUSv3 that requires assignment to be
done left to right, but I've not checked thoroughly.

--
Stephane
Sven Mascheck

2006-01-17, 8:03 am

Stephane Chazelas wrote:

> I couldn't find anything in SUSv3 that requires assignment to be
> done left to right, but I've not checked thoroughly.


It's seems to be defined in 2.9.1, Simple Commands,

"the following expansions, assignments, and redirections shall all
be performed from the beginning of the command text to the end"

> ~$ sh -c 'A=b; A=a B=$A; echo $B'
> b
> ~$ ksh -c 'A=b; A=a B=$A; echo $B'
> a
> (where "sh" above is a Bourne shell).


....with the exception of the SVR4.2 shell.

All almquist shells behave like the traditional bourne shell.
Stephane Chazelas

2006-01-17, 8:03 am

On Tue, 17 Jan 2006 12:54:37 +0100 (CET), Sven Mascheck wrote:
> Stephane Chazelas wrote:
>
>
> It's seems to be defined in 2.9.1, Simple Commands,
>
> "the following expansions, assignments, and redirections shall all
> be performed from the beginning of the command text to the end"


| 1. The words that are recognized as variable assignments or
| redirections according to Shell Grammar Rules are saved for
| processing in steps 3 and 4.
|
| 2. The words that are not variable assignments or redirections
| shall be expanded. If any fields remain following their
| expansion, the first field shall be considered the command name
| and remaining fields are the arguments for the command.
|
| 3. Redirections shall be performed as described in Redirection.
|
| 4. Each variable assignment shall be expanded for tilde
| expansion, parameter expansion, command substitution, arithmetic
| expansion, and quote removal prior to assigning the value.

So what I understand is that assignments should be done after
the expansion is done in non-assignment tokens, but the order in
which those assignments are made is not clearly specified.

If the "saving" in step one is made with a "stack" (LIFO), then
upon processing that stack in step 4, it could be the reverse
order.

>
>
> ...with the exception of the SVR4.2 shell.
>
> All almquist shells behave like the traditional bourne shell.


Given that the latest versions (the shs on the BSDs or the one
maintained by debian for instance) attempt to be POSIX
conformant, that might as well change in the future (or even
already have changed) if it happens that it breaks SUSv3
conformance.

--
Stephane
James

2006-01-17, 6:04 pm

Thanks a lot.
I need to get used to set command.

The following seems to work as well.
eval $(prog1 | xargs printf "A=%s B=%s")

James

Chris F.A. Johnson wrote:
> On 2006-01-16, James wrote:
>
> [please don't top post]
>
>
> eval $(prog1 | awk '{ printf "A=\"%s\" B=\"%s\", $1, $2 }')
>
> Or:
>
> set -- $(prog1)
> A=$1
> B=$2
>
> Or any of many other methods.
>
>
> Did you read the FAQ?
>
> --
> 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


Stephane CHAZELAS

2006-01-17, 6:04 pm

2006-01-16, 23:30(+01), Michael Tosch:
> James wrote:
>
> echo aa bb | read A B; echo $A $B
>
> works only in ksh.

[...]

And zsh and ksh/zsh based shs.

--
Stéphane
Stephane CHAZELAS

2006-01-17, 6:04 pm

2006-01-16, 23:30(+01), Michael Tosch:
> James wrote:
>
> echo aa bb | read A B; echo $A $B
>
> works only in ksh.

[...]

Not in all implementations. Typically not in the public domain
ksh (pdksh).

--
Stéphane
Michael Tosch

2006-01-17, 6:05 pm

James wrote:
> My real-life problem is to assign the outout of an external program
> (prog1) to variables,
> so that they may be fed to another external programs (prog2, prog3
> ...).
>
> Output of prog1 looks like
> aa bb
>
> I could do
> A=`prog1 | awk '{print $1}'`
> B=`prog1 | awk '{print $2}'`
> then
> prog2 $A $B
>
> But I don't want to run prog1 twice nor save to a temp file.
>


If this is your real problem, then do

prog2 `prog1`


--
Michael Tosch @ hp : com
bsh

2006-01-22, 6:10 pm

James wrote:
> I wish to assign A=aa, B=bb in a bash one-liner ...


Not tested in bash(1), but for ksh(1) this one-liner would be:

read A?'input variable A ' B?'input variable B '

=Brian

Janis Papanagnou

2006-01-22, 6:10 pm

bsh wrote:
> James wrote:
>
>
> Not tested in bash(1), but for ksh(1) this one-liner would be:
>
> read A?'input variable A ' B?'input variable B '


$ read A?'input variable A ' B?'input variable B '
input variable A 12
read: B?input variable B : invalid variable name

^V
Version M 1993-12-28 q

In which versions of ksh does your double read/assignment work?

Janis
bsh

2006-01-23, 2:55 am

Janis Papanagnou wrote:
> bsh wrote:
> $ read A?'input variable A ' B?'input variable B '
> input variable A 12
> read: B?input variable B : invalid variable name
> In which versions of ksh does your double read/assignment work?


Odd. This is a ksh88 feature, appropriate (IIRC) with
multiple variable assignments. Can other people test
this construct in both versions of the kornshell?

=Brian

Chris F.A. Johnson

2006-01-23, 2:55 am

On 2006-01-23, bsh wrote:
> Janis Papanagnou wrote:
>
> Odd. This is a ksh88 feature, appropriate (IIRC) with
> multiple variable assignments. Can other people test
> this construct in both versions of the kornshell?


Why bother? It's not POSIX compliant.

FWIW, it doesn't work in bash or ksh93 or pdksh or ash or..., nor
is there any reason to assume that it would.

--
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
Sven Mascheck

2006-01-23, 6:13 pm

brian_hiles wrote:

> [ $ read A?'text1 ' B?'text2 ' ]
>
> Odd. This is a ksh88 feature, appropriate (IIRC) with
> multiple variable assignments. Can other people test [...]


Here your memory seems to serve wrong. I tried without success
in vendor variants of ksh88c/d/e/f/g/i, and couldn't find more
in the manuals *) or the changelog of ksh88.

*) except "If the first argument contains a ?, the remainder of
this word is used as a prompt [...]".
Janis Papanagnou

2006-01-23, 6:13 pm

bsh wrote:
> Janis Papanagnou wrote:
>
>
> Odd. This is a ksh88 feature,


To my knowledge, it's not.

> appropriate (IIRC) with multiple variable assignments.


But this is a read statement and no (multiple) assignments statement
(like A=1 B=2 C=3). The read statement requires the prompt to be with
the first variable only. Maybe you've confused that with

$ read A?"Give me an A " B C ; print :$A:$B:$C:
Give me an A 123 456 789
:123:456:789:

i.e. with a _single_ prompt string.

Janis
bsh

2006-01-23, 6:13 pm

Janis Papanagnou wrote:
> bsh wrote:

It exists as a feature in ksh88. It doesn't exist. It kinda exists....

Okay, the changelog asserts that the feature applies to just
the first variable, but I distinctly remember one ksh(1) that
would selectively prompt for missing variable assignments.

This is not at all improbable, as virtually all the sh(1) and
ksh(1) variants I have used and perused [the source code,
when I had access] had undocumented small revisions.

Wish I could remember where I found this....
[vbcol=seagreen]
> it's not POSIX...


As for POSIX/non-POSIX extensions -- if you always restricted
yourself to POSIX constructs, you would never use the ksh(1)
"function" syntax: a very limiting proposition indeed.... You are
eliding the fundamental distinction between portable syntax
and syntactic extension: they do not _necessarily_ conflict.

=Brian

Chris F.A. Johnson

2006-01-23, 6:13 pm

On 2006-01-23, bsh wrote:
>
>
> As for POSIX/non-POSIX extensions -- if you always restricted
> yourself to POSIX constructs, you would never use the ksh(1)
> "function" syntax: a very limiting proposition indeed....


I have never used it. I don't fint it the least bit limiting.

> You are eliding the fundamental distinction between portable syntax
> and syntactic extension: they do not _necessarily_ conflict.



--
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
Janis Papanagnou

2006-01-23, 8:49 pm

bsh wrote:
> Janis Papanagnou wrote:
>
> [...]
>

[ Wrong attribution. Chris mentioned that. ]
[vbcol=seagreen]
> As for POSIX/non-POSIX extensions [...]

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com