Grupos de Google ya no admite nuevas publicaciones ni suscripciones de Usenet. El contenido anterior sigue siendo visible.

sh script

Visto 15 veces
Saltar al primer mensaje no leído

ewa...@gmail.com

no leída,
7 feb 2006, 10:37:047/2/06
a
Hello,

Can anyone help me with my current problem. How do you redirect output
from a command into a varable?


Thanks
Ed

Jean-Louis Liagre

no leída,
7 feb 2006, 10:44:137/2/06
a

var="`script`"

or better

var="$(script)"

and perhaps, if you want stderr too:

var="$(script 2>&1)"

ewa...@gmail.com

no leída,
7 feb 2006, 10:51:147/2/06
a
Sorry I should of been more precise.

Here is what I am trying to redirect.

$ISQL -U${SYBASEUSER} -P${SYBASEPASSWD} -w99999999 <<-!
select 1
go
!

Any Ideas?

Sashi

no leída,
7 feb 2006, 10:58:107/2/06
a

All, what is the difference between the var="`script`" and
var="$(script)"? In what situations might one be preferable over the
other?
Thanks,
Sashi

Jean-Louis Liagre

no leída,
7 feb 2006, 11:09:547/2/06
a

sure:

var="$($ISQL -U${SYBASEUSER} -P${SYBASEPASSWD} -w99999999 <<-!
select 1
go
!)"

Jean-Louis Liagre

no leída,
7 feb 2006, 11:12:017/2/06
a
Sashi wrote:
> [...]

>
> All, what is the difference between the var="`script`" and
> var="$(script)"?

None, beyond syntax.

> In what situations might one be preferable over the
> other?

$() allow easy nesting, while `` become a nightmare should the
inner command get complex, and include itself back-quotes.

Jean-Louis Liagre

no leída,
7 feb 2006, 11:14:247/2/06
a
Jean-Louis Liagre wrote:
> Sashi wrote:
>> [...]
>>
>> All, what is the difference between the var="`script`" and
>> var="$(script)"?
>
> None, beyond syntax.

Forgot to mention old shells, non POSIX, and exotic ones may
not support the latter, while the former is generally always
available.

Popular shells like bash and ksh do support $().

ewa...@gmail.com

no leída,
7 feb 2006, 11:14:367/2/06
a
Getting close I think.... inside the variable var now contains the full
extracted command and not the results. The command doesn't get
executed and the actual command is stored in variable not the results.

Jean-Louis Liagre

no leída,
7 feb 2006, 11:26:357/2/06
a

The variable var *is* containing the command's result.
Did you try running what I sent ?

Sven Mascheck

no leída,
7 feb 2006, 11:35:477/2/06
a
Jean-Louis Liagre wrote:

> [ `` vs $() ]

> Forgot to mention old shells, non POSIX, and exotic ones may
> not support the latter, while the former is generally always
> available.
>
> Popular shells like bash and ksh do support $().


However, their parser is not very robust here.
bash-2 and -3, ksh88 and zsh fail for these:

echo $(
case x in x) echo x;; esac
)

echo $(
echo comment # with )
)

echo $(
cat <<\eof
' # or a single back- or doublequote
eof
)

You have to use "case x in (x)" or escape the problematic character,
respectively. Ash variants and ksh93 on the other hand come with a
robust parser.


Thus, IMHO it's just a matter of taste (and need for portability).

Sven Mascheck

no leída,
7 feb 2006, 11:36:587/2/06
a
Jean-Louis Liagre wrote:

> [ `` vs $() ]

> Forgot to mention old shells, non POSIX, and exotic ones may


> not support the latter, while the former is generally always
> available.
>
> Popular shells like bash and ksh do support $().

However, their parser is not very robust here.
bash-2 and -3, ksh88 and zsh fail for these:

echo $(
case x in x) echo x;; esac
)

echo $(
echo comment # with )
)

echo $(
cat <<\eof
here-doc with )
eof

Jean-Louis Liagre

no leída,
7 feb 2006, 12:17:337/2/06
a

Indeed, while the first example isn't really a bug, but a lack
of upward compatibility, the last two are clearly bugs that should
be fixed in bash.

> Thus, IMHO it's just a matter of taste (and need for portability).

Beyond the fact that nesting is far clearer with it, I like the
parenthesis syntax because under vi, I can easily jump from the
beginning to the end with the "%" command.

Sven Mascheck

no leída,
8 feb 2006, 5:30:328/2/06
a
Jean-Louis Liagre wrote:
> Sven Mascheck wrote:

>> However, their parser is not very robust here.
>> bash-2 and -3, ksh88 and zsh fail for these:
>>
>> echo $(
>> case x in x) echo x;; esac
>> )

> [...] isn't really a bug, but a lack of upward compatibility

I am not sure what you mean here?

Jean-Louis Liagre

no leída,
8 feb 2006, 15:54:088/2/06
a

I mean that the new case syntax with balanced braces become
mandatory here, breaking compatibility with the previous one.

Sven Mascheck

no leída,
9 feb 2006, 9:02:279/2/06
a

I guess you mean it _should_ become mandatory, in new shell releases?

(Currently, it's optional in both SUSv3 and implementations.)

ksh88 for example accepts "case x in (x)" but even does not document it.
Thus it's a bug - with an existing workaround.

Jean-Louis Liagre

no leída,
9 feb 2006, 16:05:049/2/06
a

Yes, a documentation bug. The new syntax is de facto mandatory, but
nothing tells it in the manual pages.

0 mensajes nuevos