bourne and korn shell incompatibilities

bourne and korn shell incompatibilities

Post by Marnix van Amme » Tue, 02 Jan 1990 06:01:00



Here's an incompatibility between ksh and sh.  With ksh, this fails
on both the UNIX-PC and the 3B20 if there's more than one *.c file.
Works OK with the bourne shell.

    if [ -f *.c ];then
      echo "There's a C file in the current directory."
    else
      echo "There are no C files in the current directory."
    fi

Apparently the bourne shell permits a test such as

    if [ -f abc.c def.c xyz.c ]

probably by throwing away the def.c and xyz.c .  The korn
shell calls it a syntax error and fails the test even if
the first file exists.

I realize that the bourne shell never advertised that multiple
arguments were allowed, but it worked, I used it, and now I have a
handful of fairly large and often used scripts which I'd like to
get working under the ksh.

Does anyone have any recommendations as to the best way to
make these scripts palatable to the korn shell?

--
Marnix (ain't unix!) A.  van\ Ammers            Work: (415) 545-8334
Home: (707) 644-9781                            CEO: MAVANAMMERS:UNIX
WORK UUCP: {ihnp4|ptsfa}!pttesac!vanam          CIS: 70027,70
HOME UUCP: pttesac!Marnix!vanam

 
 
 

bourne and korn shell incompatibilities

Post by Dave Lam » Tue, 02 Jan 1990 18:44:00



Quote:

>Here's an incompatibility between ksh and sh.  With ksh, this fails
>on both the UNIX-PC and the 3B20 if there's more than one *.c file.
>Works OK with the bourne shell.
>    if [ -f *.c ];then
>Apparently the bourne shell permits a test such as
>    if [ -f abc.c def.c xyz.c ]
>probably by throwing away the def.c and xyz.c .  The korn
>shell calls it a syntax error and fails the test even if
>the first file exists.
>I realize that the bourne shell never advertised that multiple
>arguments were allowed, but it worked, I used it, ...

First this is not an undocumented feature, it is a bug in the test
builtin in sh. The documentation specificly says "-f file", the
second filename is indeed a syntax error since it is not a valid flag.
But to solve your problem, you will have to change
        if [ -f *.c ]
to
        if [ -f `ls *.c | tail -1` ]
That way test will only see 1 argument.

Dave Lampe
{ihnp4 | ames | lll-tis}!ptsfa!pacbell!dplace!djl
(415) 455-1571

 
 
 

bourne and korn shell incompatibilities

Post by David Ellio » Fri, 02 Feb 1990 17:43:00



>First this is not an undocumented feature, it is a bug in the test
>builtin in sh. The documentation specificly says "-f file", the
>second filename is indeed a syntax error since it is not a valid flag.

Very true.  "Undocumented features", whether they are bugs or true
features without documentation (like the old string operations in
BSD expr) should not be expected to work.

Quote:>But to solve your problem, you will have to change
>    if [ -f *.c ]
>to
>    if [ -f `ls *.c | tail -1` ]
>That way test will only see 1 argument.

The following will also work, and it only uses builtin functions[*], so
there is no fork cost:

        case "`echo *.c`" in
                '*.c')
                        echo "no files"
                        ;;
                *)
                        echo "files found"
                        ;;
        esac

[*] In BSD sh, echo is not a builtin.  Then again, it doesn't have test ([),
either, so the above still beats the ls/tail method by 2 forks.

--

 
 
 

bourne and korn shell incompatibilities

Post by Wolf Pa » Fri, 02 Feb 1990 22:51:00



Quote:}
}Here's an incompatibility between ksh and sh.  With ksh, this fails
}on both the UNIX-PC and the 3B20 if there's more than one *.c file.
}Works OK with the bourne shell.
}
}    if [ -f *.c ];then
}      echo "There's a C file in the current directory."
}    else
}      echo "There are no C files in the current directory."
}    fi
}
}Apparently the bourne shell permits a test such as
}
}    if [ -f abc.c def.c xyz.c ]
}
}probably by throwing away the def.c and xyz.c .  The korn
}shell calls it a syntax error and fails the test even if
}the first file exists.
}
}Does anyone have any recommendations as to the best way to
}make these scripts palatable to the korn shell?

Try this one:

STRING=`echo *.c`
if [  "$STRING" != "*.c" ] ; then
        echo "There is a C program in this dir"
else
        echo "there is NO C program in this dir"
fi

I tried it both under /bin/sh and /bin/ksh and it works identically.

Wolf Paul
ihnp4!killer!wnp
ihnp4!killer!dcs!wnp

 
 
 

bourne and korn shell incompatibilities

Post by Chris Tor » Sat, 03 Feb 1990 00:26:00



>The following will also work....

>    case "`echo *.c`" in
>            '*.c')
>                    echo "no files"
>                    ;;
>            *)
>                    echo "files found"
>                    ;;
>    esac

Actually, if you have exactly one file, called `*.c', this will
claim there are no files.  To fix this you could use

        case "`echo *.c`" in
                "*.c")
                        if [ -f "*.c" ]; then
                                echo files found
                        else
                                echo no files
                        fi;;
                *)
                        echo files found;;
        esac

although this seems like overkill, in general.

Thinking about a shell with built-in exception handling,
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)

 
 
 

bourne and korn shell incompatibilities

Post by Wolf Pa » Fri, 02 Feb 1990 16:55:00




>> ...
>Actually, if you have exactly one file, called `*.c', this will
>claim there are no files.  To fix this you could use

Actually, if you have exactly one file, called `*.c', you should
quickly rename it to something more intelligent. You should also practise
your typing skills so you don't create silly file names like this in
the future.

Wolf Paul
ihnp4!killer!wnp

 
 
 

bourne and korn shell incompatibilities

Post by Lars Henrik Mathies » Fri, 02 Feb 1990 11:18:00




>Actually, if you have exactly one file, called `*.c', this will
>claim there are no files.  To fix this you could use
>    case "`echo *.c`" in
>            "*.c")
>                    if [ -f "*.c" ]; then
>                            echo files found
>                    else
>                            echo no files
>                    fi;;
>            *)
>                    echo files found;;
>    esac
>although this seems like overkill, in general.

I just had to invent the same thing and came up with this:

        if (set *.c; exec test -f $1); then
                echo files found
        else
                echo no files
        fi

This works for 4.3BSD Bourne shell; on this system "test -f *.c" gives a
`too many arguments' error for two or more files! I don't see why it shouldn't
work with Korn shell.
  Note that this uses exactly one fork and one exec, and that it doesn't
clobber the arguments because the "set" is used in a subshell.
--
Lars Mathiesen, DIKU, U of Copenhagen, Denmark      [uunet!]mcvax!diku!thorinn
Institute of Datalogy -- we're scientists, not engineers.

 
 
 

bourne and korn shell incompatibilities

Post by XMRP50000[jcm]-a.v.re » Fri, 02 Feb 1990 20:22:00



Quote:> Works OK with the bourne shell.
>     if [ -f *.c ];then
> Does anyone have any recommendations as to the best way to
> make these scripts palatable to the korn shell?

      if [ -f `echo *.c|cut -f1 -d' '` ];then

                                                Adam Reed (mtgzz!avr)

 
 
 

1. Trying to run nohup from Korn shell but getting Bourne shell errors

Help! Duh!
I would like to run a process in background with nohup on a
SOLARIS 2.3. How can I get the korn shell script detectsetup
to be interpreted under ksh.

In Korn shell I attempted...

$ /bin/ksh -c "/usr/bin/nohup /export/home/setupin/detectsetup&"

$ [1]   15276

$ Sending output to nohup.out

Checking the process I find....

USER   PID    PPID COMMAND
pagein 15414     1 sh /export/home/setupin/detectsetup
                            ^
                           | Why not Korn shell

My nohup.out file errors indicate Bourne shell is interpreting Korn shell
script (doesn't understand integer or [[).      

2. change permissions of all files

3. Telling Bourne shell from Korn shell at start-up

4. Perl missing sh-like "|" functionality? Psudo example included ...

5. Bourne shell $@ incompatibility

6. combinet isdn card

7. Light Speed Bourne Shell! (was: Bourne shell tricks)

8. Controlling the Contrast of A Monitor

9. best korn shell resources and is there a korn shell faq

10. Korn/Bourne shell return code precision

11. How to pipe stderr to a command in Bourne or Korn shell

12. BOURNE/KORN Shell Compiler

13. Looking for simple Bourne/Korn shell FIFO queue mechanism :-|