exec status with no command is 0 but no redirection

2010-05-14 Thread fpo

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu' 
-DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/share/locale' 
-DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include 
-I./lib   -g -O2
uname output: Linux pc9 2.6.31.13-desktop-1mnb #1 SMP Tue Apr 27 
20:37:44 EDT 2010 x86_64 Intel(R) Core(TM)2 Duo CPU E8500  @ 3.16GHz 
GNU/Linux

Machine Type: x86_64-unknown-linux-gnu

Bash Version: 4.1
Patch Level: 0
Release Status: release

Description:
Well, I am not sure it is a bug. I just tried this after reading the 
manual page and the exec built-in command. I was expecting to have the 
iconv command be called on the standard output of the shell script.


The script below outputs 0 as exec return status and the string is yet 
given as ISO-8859-1. One of these is probably not correct though, shall 
we expect a status = 1 if no redirection is done?


Repeat-By:

A simple script, that should be saved in ISO-8859-1 encoding system:

#!/bin/bash

exec | iconv -f ISO_8859-1 -t UTF-8
echo exec status $?

echo " 15) Valérie Leulliot - Mon homme blessé "
exit 0


Thanks a lot and regards.
- fpo





Re: exec status with no command is 0 but no redirection

2010-05-14 Thread Bob Proulx
fpo wrote:
> Well, I am not sure it is a bug. I just tried this after reading the  
> manual page and the exec built-in command. I was expecting to have the  
> iconv command be called on the standard output of the shell script.

Thank you for the report.  But this is not a bug.  You are
misunderstanding how exec is to be used.  You have called it without
any command and without any redirections.  The way you are invoking it
doesn't seem useful to me.

> The script below outputs 0 as exec return status and the string is yet  
> given as ISO-8859-1. One of these is probably not correct though,

> exec | iconv -f ISO_8859-1 -t UTF-8

The 'exec' produces no output but the stdout is piped to 'iconv'.  The
'iconv' command reading no input from stdin produces no output.  The
status is success and $? is set to zero.

> shall we expect a status = 1 if no redirection is done?

No.  That is not an error.  It has its uses.  It would be a change
from traditional behavior and almost certain to break scripts.

exec without either command or redirections is not what you intended
to do here.  In that mode it is the same as 'true'.

  true | iconv -f ISO_8859-1 -t UTF-8

Beyond this I am not sure what you wanted to do with exec.  You may
have wanted this:

  exec iconv -f ISO_8859-1 -t UTF-8

Bob



Re: exec status with no command is 0 but no redirection

2010-05-14 Thread Andreas Schwab
Bob Proulx  writes:

> Beyond this I am not sure what you wanted to do with exec.  You may
> have wanted this:
>
>   exec iconv -f ISO_8859-1 -t UTF-8

I think he rather wants this:

exec > >(iconv -f ISO_8859-1 -t UTF-8)

(See Process Substitution in the bash manual.)

Alternatively, enclose the whole script in { ... } and add the pipe
after that.

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."