From: Mark Hobley on
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/

From: Mark Hobley on
Ben Finney <ben+unix(a)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.

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

From: Mark Hobley on
Mark Hobley <markhobley(a)hotpop.donottypethisbit.com> wrote:
> Ben Finney <ben+unix(a)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.

Mark.

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