Unix Shell - how to assign the result of a function to a variabel

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > how to assign the result of a function to a variabel





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 how to assign the result of a function to a variabel
vs.vivek1@gmail.com

2007-12-06, 7:36 am

i want to assign the result of a function to a variabe for eg

i want to asign

the result of "cut -f1 -d, string" to a variable for eg x

i tried x='echo "cut -f1 -d, string"' but terribly failed

[where the string1 is the string a,b,c
and i should get the value of x =b]

could you please help me to solve

Regards;
Vivek
Joachim Schmitz

2007-12-06, 7:36 am

<vs.vivek1@gmail.com> schrieb im Newsbeitrag
news:bbcc3c24-fbe2-4a2e-9c90-abf5048bb3f1@b40g2000prf.googlegroups.com...
>i want to assign the result of a function to a variabe for eg
>
> i want to asign
>
> the result of "cut -f1 -d, string" to a variable for eg x

What's meant by result, the exit code or the output?
If the output:
x=$(cmd)

If the exitcode:
cmd
x=$?

> i tried x='echo "cut -f1 -d, string"' but terribly failed

now x contains the string 'echo "cut -f1 -d, string"'

> [where the string1 is the string a,b,c
> and i should get the value of x =b]

Ah, so you expect the output:
x=$(echo string | cut -d, -f1)

'cut' reads from a file or from stdin, not some string from it's commandline
Instead of $(cmd) you can also use `cmd` (and in the old Bourne Shell that's
the only option)

Bye, Jojo


Stephane Chazelas

2007-12-06, 7:36 am

On Thu, 6 Dec 2007 13:09:51 +0100, Joachim Schmitz wrote:
> <vs.vivek1@gmail.com> schrieb im Newsbeitrag
> news:bbcc3c24-fbe2-4a2e-9c90-abf5048bb3f1@b40g2000prf.googlegroups.com...
> What's meant by result, the exit code or the output?
> If the output:
> x=$(cmd)


Note that it only stores what cmd outputs on its standard
output, not what it outputs on any other fd (which is fine
here). And with the exception of zsh, that only works if there's
no NUL byte in that output.

It should also be noted that the trailing newline characters are
removed.

echo a

outputs the two characters "a" and <newline>.

x=$(echo a)

only stores the first one in $x. To work around that, you can
do:

x=$(echo a; echo .)
x=${x%.}

> If the exitcode:
> cmd
> x=$?
>
> now x contains the string 'echo "cut -f1 -d, string"'
>
> Ah, so you expect the output:
> x=$(echo string | cut -d, -f1)
>
> 'cut' reads from a file or from stdin, not some string from it's commandline
> Instead of $(cmd) you can also use `cmd` (and in the old Bourne Shell that's
> the only option)

[...]

A common error is to think that:

x=$(echo "$string" | cut -d, -f1)

stores the first field of $string in $x.

First it should be:

x=$(printf '%s\n' "$string" | cut -d, -f1)
as echo (contrary to printf) may perform some transformations on
the text it is passed.

And that command stores the first field of *each line of*
$string into $x which is not the same thing especially if that
first field or any other field may contain a newline character.

If you want the first field, you can do:

x=${string%%,*}

or

x=$(expr "x$string" : "x\([^,]\),")

or

IFS=,
set -f
set -- $string
x=$1

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com