how to assign the result of a function to a variabel

how to assign the result of a function to a variabel

am 06.12.2007 12:48:12 von vs.vivek1

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

Re: how to assign the result of a function to a variabel

am 06.12.2007 13:09:51 von Joachim Schmitz

schrieb im Newsbeitrag
news:bbcc3c24-fbe2-4a2e-9c90-abf5048bb3f1@b40g2000prf.google groups.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

Re: how to assign the result of a function to a variabel

am 06.12.2007 13:39:48 von Stephane CHAZELAS

On Thu, 6 Dec 2007 13:09:51 +0100, Joachim Schmitz wrote:
> schrieb im Newsbeitrag
> news:bbcc3c24-fbe2-4a2e-9c90-abf5048bb3f1@b40g2000prf.google groups.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)

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 .

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=$?
>
>> 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)
[...]

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