What is the diff. between "sh foo" and "foo"?

What is the diff. between "sh foo" and "foo"?

Post by Terry R. McConne » Sat, 30 May 1998 04:00:00




>The following is the text of a ksh script called "foo":
>#! /bin/ksh
>function x
>{
>  echo "This is Ksh!"
>}

>If I say "sh foo" it produces the following output:

>UX:sh (x): ERROR: function: not found
>This is Ksh!
>This is Ksh!

>What is the difference between the two invocations "foo" and "sh foo"?

In the invocation "foo" the current shell forks and execs foo. In modern
versions of the system the kernel realizes the first line of the file is
a hashbang and it starts up the indicated interpreter to read the file foo.
( On some systems, e.g. Minix, the exec call is unable to recognize hashbangs.
In such cases, and with some shells, it is possible to build this feature in
at the shell level.)

In the invocation "sh foo" the Bourne shell reads foo and interprets the first
line as a comment.

Quote:

>And if I write a ksh script how do I ensure that it works whether users are
>typing "sh foo" or simply "foo"?

There is a standard shell-programming technique for dealing with scripts
intended for other shells than the one actually reading them. For example,
the following line from Pnews

export PATH || (echo "OOPS, this isn't sh. Desperation time. I will feed
myself to sh."; sh $0; kill $$)

aims to prevent the script from being run in csh. Your problem is
somewhat different in that ksh is a superset of sh. I don't know ksh,
but you should be able to replace "export PATH" with some construction
that is guaranteed to fail in sh but succeed in ksh.

--
************************************************************************
Terry R. McConnell   Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150

************************************************************************

 
 
 

What is the diff. between "sh foo" and "foo"?

Post by Akim Demaill » Sat, 30 May 1998 04:00:00


Terry> export PATH || (echo "OOPS, this isn't sh. Desperation time. I
Terry> will feed myself to sh."; sh $0; kill $$)

Why kill instead of exit?

Why not

export PATH || exec sh -c $0

? (This is a true question :)

Akim

--
P-mail: Akim Demaille, 107 rue Bobillot, F-75013 Paris, France

V-mail: +33 1 45 81 78 68

 
 
 

What is the diff. between "sh foo" and "foo"?

Post by Terry R. McConne » Sat, 30 May 1998 04:00:00





>Terry> export PATH || (echo "OOPS, this isn't sh. Desperation time. I
>Terry> will feed myself to sh."; sh $0; kill $$)

>Why kill instead of exit?

>Why not

>export PATH || exec sh -c $0

>? (This is a true question :)

I'm not the author of Pnews from which the above example is taken, so
I can only speculate about the author's intent. The parentheses are
needed in order to group the commands inside, so that a semicolon won't
terminate the conditional command. But grouping commands inside
parentheses also runs those commands in a subshell. Putting an exec or
exit in there will terminate the subshell but not the original one.
Thus, for example, the script

#!/bin/sh

( echo -n "hello "; exit )
echo world

would echo hello world.

Your suggested alternative would be fine if you didn't also want to print
the "Desperation" message.

Ok, so why not

export PATH || { echo "gloom and doom, etc" ; exec sh  $0; }

? This, unfortunately, won't parse in csh.

--
************************************************************************
Terry R. McConnell   Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150

************************************************************************

 
 
 

What is the diff. between "sh foo" and "foo"?

Post by Richard Howlet » Tue, 02 Jun 1998 04:00:00


Quote:> aims to prevent the script from being run in csh. Your problem is
> somewhat different in that ksh is a superset of sh. I don't know ksh,
> but you should be able to replace "export PATH" with some construction
> that is guaranteed to fail in sh but succeed in ksh.

The following will fail in sh but succeed in ksh:

        export DUMMY=dummy

The Korn shell can perform the variable set and the export in one go,
the Bourne shell cannot.

> --
> ************************************************************************
> Terry R. McConnell   Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150

> ************************************************************************

--
     __
    /  /         /
   /__/  .  __  /__
  /  |  / /    /   /
 /   | / /__  /   /

Richard Howlett

Anti-spamming email address above!
Use the one below to reply

 
 
 

1. What is the meaning of "foo" and $foo"?

I am running RH 7.3

I am installing a program called "horde" and one of the steps having me
running this command on all the files in a directory:  #  for foo in *.dist;
do cp $foo 'basename $foo .dis'; done

This is a list of the files in that directory:

horde.php.dist
html.php.dist
lang.php.dist
mime_mapping.php.dist
mime_drivers.php.dist
motd.php.dist
prefs.php.dist
registry.php.dist

What is that command and what does it do?  Thanks for any help.

Lamar

2. newbie net card problem

3. GETSERVBYNAME()????????????????????"""""""""""""

4. /bin/calendar, Soalris 2.6, & NIS??!?

5. why "" in echo "$FOO" ?

6. SLIP-server problem

7. """"""""My SoundBlast 16 pnp isn't up yet""""""""""""

8. Networking a laptop to itself!

9. 'echo "some text" |read foo' results in empty $foo

10. makefiles and "diff foo bar | head -10"

11. Type "(", ")" and "{", "}" in X...

12. kill a "/bin/sh -c foo bar"

13. sh says: test "$1" = "" when $1="-b"