Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirecting to a stream number held in a variable

11 views
Skip to first unread message

Mark Hobley

unread,
Dec 22, 2009, 9:08:02 PM12/22/09
to
I have a function that is being generated by autoconf as follows:

as_fn_error ()
{
...
$as_echo "$as_me:${as_lineno-$LINENUM}: error: $1" >&$3
...
}

This basically expands to give:

echo "foobar" >&$3

where the $3 parameter contains the stream number for the log file.

The echo command with the redirection causes a syntax error on some shells.
How do I restructure it to enable the redirection to go to the stream
number held in the $3 parameter?

A portable Bourne compatible shell syntax is required here.

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

mop2

unread,
Dec 22, 2009, 10:10:44 PM12/22/09
to
On Wed, 23 Dec 2009 00:08:02 -0200, Mark Hobley <markh...@hotpop.donottypethisbit.com> wrote:

> I have a function that is being generated by autoconf as follows:
>
> as_fn_error ()
> {
> ...
> $as_echo "$as_me:${as_lineno-$LINENUM}: error: $1" >&$3
> ...
> }
>
> This basically expands to give:
>
> echo "foobar" >&$3
>
> where the $3 parameter contains the stream number for the log file.
>
> The echo command with the redirection causes a syntax error on some shells.
> How do I restructure it to enable the redirection to go to the stream
> number held in the $3 parameter?
>
> A portable Bourne compatible shell syntax is required here.
>
> Mark.
>

You can try:

eval "echo \"foobar\" >&$3"

Ben Finney

unread,
Dec 22, 2009, 10:47:51 PM12/22/09
to
markh...@hotpop.donottypethisbit.com (Mark Hobley) writes:

> echo "foobar" >&$3
>
> where the $3 parameter contains the stream number for the log file.
>
> The echo command with the redirection causes a syntax error on some
> shells.

Which shells, and what is the text of the error?

> How do I restructure it to enable the redirection to go to the stream
> number held in the $3 parameter?
>
> A portable Bourne compatible shell syntax is required here.

I don't know about Bourne compatible, generally it's best to aim for
POSIX compatibility at a minimum. The reference for this syntax is in
the IEEE Std 1003.1-2008 document, the “Redirection” section
<URL:http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07>.

Bear in mind that “portable syntax” may not necessarily address “causes
syntax error in some shells”. Some shells may simply be non-conformant.

--
\ “It is not enough to have a good mind. The main thing is to use |
`\ it well.” —Rene Descartes |
_o__) |
Ben Finney

Mark Hobley

unread,
Dec 23, 2009, 5:08:02 AM12/23/09
to
Ben Finney <ben+...@benfinney.id.au> wrote:
> Which shells, and what is the text of the error?
echo "foobar" >&$3

ash:
Syntax error: Bad fd number

hsh: (I think this one works)
foobar

dash:
dash: Syntax error: Bad fd number

ksh: (pkdsh)
ksh: >& : illegal file descriptor name

posh:
posh: >& : illegal file descriptor name

bash:
bash: $3: ambiguous redirect

> I don't know about Bourne compatible, generally it's best to aim for
> POSIX compatibility at a minimum.

I cannot guarantee that. There are various POSIX standards, and they
add extensions which break compatibility with existing shells.

Portable Bourne Shell Syntax syntax is basically a subset of POSIX, which
works across all exiting Bourne compatible shells.

I have a syntax scanner for this:

ftp://markhobley.yi.org/devtools/checkbashisms/

Mark Hobley

unread,
Dec 30, 2009, 5:08:02 PM12/30/09
to
Mark Hobley <markh...@hotpop.donottypethisbit.com> wrote:
> Ben Finney <ben+...@benfinney.id.au> wrote:
>> Which shells, and what is the text of the error?
> echo "foobar" >&$3
>
> ash:
> Syntax error: Bad fd number
>
> hsh: (I think this one works)
> foobar
>
> dash:
> dash: Syntax error: Bad fd number
>
> ksh: (pkdsh)
> ksh: >& : illegal file descriptor name
>
> posh:
> posh: >& : illegal file descriptor name
>
> bash:
> bash: $3: ambiguous redirect

I think what is happening here is that the shells are tripping up on the >&$3
syntax **before** the substitution to a value is being made.

Examining the autoconf output:

In an empty directory create a configure.ac file as follows:

# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.64])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])

# Checks for programs.

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

Now run:

autoconf

Using an editor examine the file configure and search for &$

The problematic line appears in the function as_fn_error as follows:

as_fn_error ()
{
as_status=$?; test $as_status -eq 0 && as_status=1
if test "$3"; then
as_lineno=${as_lineno-"$2"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
$as_echo "$as_me:${as_lineno-$LINENO}: error: $1" >&$3
fi
$as_echo "$as_me: error: $1" >&2
as_fn_exit $as_status
} # as_fn_error


I found this snipped of code elsewhere in the configure file:

# Handling of arguments.
for ac_config_target in $ac_config_targets
do
case $ac_config_target in

*) as_fn_error "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
esac
done

Note the number 5. I reckon that is where $3 gets its value of 5 from.

0 new messages