From joek@mail.flyingcroc.net  Wed Jun 12 11:08:28 2002
Return-Path: <joek@mail.flyingcroc.net>
Received: from gilliam.users.flyingcroc.net (gilliam.users.flyingcroc.net [207.246.128.2])
	by hub.freebsd.org (Postfix) with ESMTP id 5982337B401
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 12 Jun 2002 11:08:28 -0700 (PDT)
Received: from unx48.staff.flyingcroc.net (unx48.staff.flyingcroc.net [207.246.150.48])
	by gilliam.users.flyingcroc.net (8.9.3/8.9.3) with SMTP id LAA58614
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 12 Jun 2002 11:08:28 -0700 (PDT)
Received: (qmail 82492 invoked by uid 1001); 12 Jun 2002 18:08:27 -0000
Message-Id: <20020612180827.82491.qmail@unx48.staff.flyingcroc.net>
Date: 12 Jun 2002 18:08:27 -0000
From: Joe Kelsey <joek@mail.flyingcroc.net>
Reply-To: Joe Kelsey <joek@mail.flyingcroc.net>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: sh aborts on variables with periods
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         39198
>Category:       bin
>Synopsis:       sh aborts on variables with periods
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jun 12 11:10:01 PDT 2002
>Closed-Date:    Sat Jul 24 12:15:12 GMT 2004
>Last-Modified:  Sat Jul 24 12:15:12 GMT 2004
>Originator:     Joe Kelsey
>Release:        FreeBSD 4.6-RC i386
>Organization:
>Environment:
System: FreeBSD unx48.staff.flyingcroc.net 4.6-RC FreeBSD 4.6-RC #11: Wed Jun 12 08:37:42 PDT 2002 joek@unx48.staff.flyingcroc.net:/usr/obj/usr/src/sys/UNX48 i386

>Description:
	/bin/sh does not gracefully accept variables with periods.

	ksh93 has a new feature of structured variables.  These are
	variables that behave like C structures.  Along with this new
	feature, where sub-variables are separated by periods, ksh also
	allows variables to begin with periods.

	This causes a problem in compatibility.  If your login shell is
	ksh93, and you set ENV=~/.ksh93, then when you su, /bin/sh will
	try to process your ENV file and issue the error message:
Syntax error: Bad substitution.
	when it tries to process the following line:

	if [ -n "$KSH_VERSION"] || [ -n "${.sh.version}" ]
	then
	  # ksh-only stuff
	fi

	Either /bin/sh has to allow parameters to contain periods or it
	has to provide a reliable method of detecting /bin/sh versus
	ksh.

>How-To-Repeat:
	See above
>Fix:

	Change the is_name and is_in_name macros to allow periods in
	addition to underscores.



>Release-Note:
>Audit-Trail:

From: Peter Pentchev <roam@ringlet.net>
To: Joe Kelsey <joek@mail.flyingcroc.net>
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/39198: sh aborts on variables with periods
Date: Wed, 12 Jun 2002 21:16:19 +0300

 On Wed, Jun 12, 2002 at 06:08:27PM -0000, Joe Kelsey wrote:
 > 
 > >Number:         39198
 > >Category:       bin
 > >Synopsis:       sh aborts on variables with periods
 > >Originator:     Joe Kelsey
 > 
 > >Description:
 > 	/bin/sh does not gracefully accept variables with periods.
 [snip]
 > 	if [ -n "$KSH_VERSION"] || [ -n "${.sh.version}" ]
 > 	then
 > 	  # ksh-only stuff
 > 	fi
 > 
 > 	Either /bin/sh has to allow parameters to contain periods or it
 > 	has to provide a reliable method of detecting /bin/sh versus
 > 	ksh.
 
 Does ksh93 set $SHELL to 'sh' and, for login shells, '-sh', or does it
 (as it should, IMHO) set it to 'ksh' and '-ksh', respectively?
 
 G'luck,
 Peter
 
 -- 
 Peter Pentchev	roam@ringlet.net	roam@FreeBSD.org
 PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
 Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
 This sentence contradicts itself - or rather - well, no, actually it doesn't!

From: Joe Kelsey <joek@mail.flyingcroc.net>
To: Peter Pentchev <roam@ringlet.net>
Cc: Joe Kelsey <joek@mail.flyingcroc.net>, bug-followup@FreeBSD.org
Subject: Re: bin/39198: sh aborts on variables with periods
Date: Wed, 12 Jun 2002 11:24:01 -0700

 Peter Pentchev wrote:
 
 >On Wed, Jun 12, 2002 at 06:08:27PM -0000, Joe Kelsey wrote:
 >  
 >
 >>>Number:         39198
 >>>Category:       bin
 >>>Synopsis:       sh aborts on variables with periods
 >>>Originator:     Joe Kelsey
 >>>      
 >>>
 >>>Description:
 >>>      
 >>>
 >>	/bin/sh does not gracefully accept variables with periods.
 >>    
 >>
 >[snip]
 >  
 >
 >>	if [ -n "$KSH_VERSION"] || [ -n "${.sh.version}" ]
 >>	then
 >>	  # ksh-only stuff
 >>	fi
 >>
 >>	Either /bin/sh has to allow parameters to contain periods or it
 >>	has to provide a reliable method of detecting /bin/sh versus
 >>	ksh.
 >>    
 >>
 >
 >Does ksh93 set $SHELL to 'sh' and, for login shells, '-sh', or does it
 >(as it should, IMHO) set it to 'ksh' and '-ksh', respectively?
 >  
 >
 ksh sets it to /bin/ksh.  Here is an example of the problem:
 unx48.staff.flyingcroc.net$ su
 Password:
 unx48# ps
   PID  TT  STAT      TIME COMMAND
 82521  p0  S      0:00.03 _su (csh)
 82523  p0  R+     0:00.00 ps
   175  v0  Is     0:00.02 login -p root
   341  v0  I      0:00.03 -csh (csh)
   343  v0  S+     0:07.30 top
   176  v1  Is+    0:00.00 /usr/libexec/getty Pc ttyv1
   177  v2  Is+    0:00.00 /usr/libexec/getty Pc ttyv2
   178  v3  Is+    0:00.00 /usr/libexec/getty Pc ttyv3
   179  v4  Is+    0:00.00 /usr/libexec/getty Pc ttyv4
   180  v5  Is+    0:00.00 /usr/libexec/getty Pc ttyv5
   181  v6  Is+    0:00.00 /usr/libexec/getty Pc ttyv6
   182  v7  Is+    0:00.00 /usr/libexec/getty Pc ttyv7
   139 con- I+     0:00.01 /bin/sh /usr/local/bin/safe_mysqld --user=mysql
   148 con- I+     0:00.00 qmail-lspawn |dot-forward .forward\n|preline 
 -f /usr/
 unx48# echo $SHELL
 /bin/csh
 unx48# sh
 Syntax error: Bad substitution
 # echo $SHELL
 /bin/csh
 # exit
 unx48# exit
 exit
 /home/joek
 unx48.staff.flyingcroc.net$ echo $SHELL
 /bin/ksh
 
 If there was a reliable way to detect /bin/sh I could use that instead 
 of trying to detect ksh.  Alternatively, if /bin/sh accepted periods in 
 variable names, it would pass the
  [ -n "${.sh.version}" ]
 test simply because it would think the parameter .sh.version was null.
 
 not sure if this helps...
 /Joe
 
 

From: Matthias Buelow <mkb@informatik.uni-wuerzburg.de>
To: Joe Kelsey <joek@mail.flyingcroc.net>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/39198: sh aborts on variables with periods
Date: Wed, 12 Jun 2002 21:40:49 +0200

 Joe Kelsey wrote:
 
 > 	/bin/sh does not gracefully accept variables with periods.
 
 Neither does ksh88, the direct predecessor to ksh93, which is used
 as "sh" and "ksh" on most Unix systems, so you have to modify your
 startup script in any case.  It answers with almost exactly the
 same message as fbsd /bin/sh.
 
 > 	Either /bin/sh has to allow parameters to contain periods or it
 > 	has to provide a reliable method of detecting /bin/sh versus
 > 	ksh.
 
 Try to check for RANDOM, which does not exist on sh.  It probably
 exists on bash (haven't checked) but you can check that through
 the existance of BASH_VERSION.  Similarly probably for zsh.
 Then try to check for KSH_VERSION to draw the line between ksh and
 pdksh.  Then you might do a strings ${0##-}|grep "@(#)Version"
 (or use what(1)) and check for "M 1993" which is a bit wasteful but
 if you really want to know then that'll work.  Then you probably want
 to differentiate further since some earlier version of the '93 edition
 did not support everything that is described in the Kornshell book
 (if you're really pedantic.)
 
 --mkb
 

From: Joe Kelsey <joek@mail.flyingcroc.net>
To: Matthias Buelow <mkb@informatik.uni-wuerzburg.de>
Cc: Joe Kelsey <joek@mail.flyingcroc.net>,
	FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/39198: sh aborts on variables with periods
Date: Wed, 12 Jun 2002 13:05:24 -0700

 Matthias Buelow wrote:
 
 > Joe Kelsey wrote:
 >
 >>     /bin/sh does not gracefully accept variables with periods.
 >
 >
 > Neither does ksh88, the direct predecessor to ksh93, which is used
 > as "sh" and "ksh" on most Unix systems, so you have to modify your
 > startup script in any case.  It answers with almost exactly the
 > same message as fbsd /bin/sh.
 >
 >>     Either /bin/sh has to allow parameters to contain periods or it
 >>     has to provide a reliable method of detecting /bin/sh versus
 >>     ksh.
 >
 >
 > Try to check for RANDOM, which does not exist on sh.  It probably
 > exists on bash (haven't checked) but you can check that through
 > the existance of BASH_VERSION.  Similarly probably for zsh.
 > Then try to check for KSH_VERSION to draw the line between ksh and
 > pdksh.  Then you might do a strings ${0##-}|grep "@(#)Version"
 > (or use what(1)) and check for "M 1993" which is a bit wasteful but
 > if you really want to know then that'll work.  Then you probably want
 > to differentiate further since some earlier version of the '93 edition
 > did not support everything that is described in the Kornshell book
 > (if you're really pedantic.)
 >
 Thanks for the suggestions.  So far I am not concerned about bash or 
 zsh, but I will keep it in mind.  I just want to separate generic ksh 
 from /bin/sh, and RANDOM should do that.  The check for KSH_VERSION 
 fails in ksh93 since they went to the .sh.version parameter and dropped 
 KSH_VERSION completely, a real killer.
 
 /Joe
 
 
 

From: Peter Pentchev <roam@ringlet.net>
To: Joe Kelsey <joek@mail.flyingcroc.net>
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/39198: sh aborts on variables with periods
Date: Fri, 14 Jun 2002 10:47:09 +0300

 On Wed, Jun 12, 2002 at 11:24:01AM -0700, Joe Kelsey wrote:
 > Peter Pentchev wrote:
 > 
 > >On Wed, Jun 12, 2002 at 06:08:27PM -0000, Joe Kelsey wrote:
 > >>>
 > >>	/bin/sh does not gracefully accept variables with periods.
 > >
 > >Does ksh93 set $SHELL to 'sh' and, for login shells, '-sh', or does it
 > >(as it should, IMHO) set it to 'ksh' and '-ksh', respectively?
 > >  
 > >
 > ksh sets it to /bin/ksh.
 
 Well, /bin/sh doesn't :)
 
 [snip]
 > If there was a reliable way to detect /bin/sh I could use that instead 
 > of trying to detect ksh.
 
 You mean, like, testing $SHELL for being "/bin/ksh"? :)
 
 if [ "$SHELL" = "/bin/ksh" ]; then
 	ksh-specific code
 else
 	plain ol' Bourne shell
 fi
 
 ..or maybe even..
 
 if expr "$SHELL" : '.*ksh$'; then
 	ksh-specific code
 else
 	plain ol' Bourne shell
 fi
 
 ..which would match 'ksh', '-ksh', '/bin/ksh', '/usr/local/bin/ksh',
 and other variations.
 
 HTH.
 
 G'luck,
 Peter
 
 -- 
 Peter Pentchev	roam@ringlet.net	roam@FreeBSD.org
 PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
 Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
 Do you think anybody has ever had *precisely this thought* before?

From: Giorgos Keramidas <keramida@FreeBSD.org>
To: parv <parv@pair.com>
Cc: Frank v Waveren <fvw@var.cx>, bug-followup@FreeBSD.org
Subject: Re: bin/39198: sh aborts on variables with periods
Date: Fri, 14 Jun 2002 23:21:43 +0300

 On 2002-06-12 20:20 -0400, parv wrote:
 > in message <1023918173HKC.fvw@yendor.var.cx>,
 > wrote Frank v Waveren thusly...
 > > On Wed, Jun 12, 2002 at 05:41:11PM -0400, parv wrote:
 > > >     if test $RANDOM = $RANDOM
 > >
 > > omg, you wicked wicked man. That will mysteriously misdetect on ksh/bash
 > > once in 32767 times. Please don't.
 >
 > oh, come on, i am merely conveying what i got in the mail!
 > anyway, i tried...
 
 [snipetry of 32767 tests of a RNG ]
 
 You're just too damn lucky that this didn't get you the same random
 number.  Your test of $RANDOM = $RANDOM will very possibly fail if
 your RNG allows multiple occurences of the same number to appear in
 the stream of random numbers, but will only fail very rarely, and will
 go unnoticed until it's very late :P
 
 If you absolutely *must* use $RANDOM for making this test, you should
 probably use something like:
 
 	if [ X"${RANDOM}" = X"" ]; then
 		# $RANDOM failed
 	else
 		if echo "${RANDOM}" | \
 		    grep '^[[:digit:]]\+$' >/dev/null 2>&1 ;then
 			# # $RANDOM exists and works as expected
 		else
 			# $RANDOM isn't null, but is not a number
 			# either.  Oops!
 		fi
 	fi
 
 - Giorgos
 
State-Changed-From-To: open->closed 
State-Changed-By: schweikh 
State-Changed-When: Sat Jul 24 12:13:13 GMT 2004 
State-Changed-Why:  
The discussion has settled on using a different test method 
to detect the shell incarnation. No need to change /bin/sh. 

BTW, this is what I use for years and has never let me down: 
if test -n "${ZSH_VERSION}"; then 
_SHELL=zsh 
elif test -n "${BASH_VERSION}"; then 
_SHELL=bash 
elif test -n "${FCEDIT}"; then 
_SHELL=ksh 
elif test "${PS3}" = ""; then 
_SHELL=sh 
else 
_SHELL=unknown 
fi 

http://www.freebsd.org/cgi/query-pr.cgi?pr=39198 
>Unformatted:
