Build script for all shells

Hi,

I made a build script for the product I am working on.

The script was made in the /bin/sh shell.

My first line in the script (after the #! /bin/sh and following # lines ) were,

if [ $SHELL != /bin/sh ] ; then
  /bin/sh
fi;

It works well with my sh shell. I run the script as

sh build.sh

Now I ran this script on another machine with $SHELL as /bin/csh

It failed to build. I then tried

./build.sh and it works well.

Why the change in behaviour and whats the difference between sh and ./

Can a script be written in such a manner that it would run in any shell ?

Thanks,
Vino

See this link for an explanation of ./. As for building a script that will run in all shells, I suppose it is possible to do this but I would imagine that it would be difficult to get it right. Some shells, such as csh, are not as full featured as say ksh. The script you are building would have to be built with the lowest common denominator of shell functionality common to all shells.

Here is an article that discusses this topic

instead of checking for $SHELL ... just set the first line of the script to "#! /bin/sh" and run it with either ./script_name or /path/to/script_name ...

checking for $SHELL is unnecessary ... you might check for the correct shell path if you are porting the script to different OS platforms (i.e., /bin/ksh for Solaris and /usr/bin/ksh for HP, etc.) but that doesn't sound like that's what you're trying to do here ...

Um, it doesn't work in csh because "if [ $SHELL != /bin/sh ]; then" is not valid csh syntax.

it doesn't matter what shell you are using for your current environment, the script will use the shell you set it to use through the "#!" directive ...

The OP is attempting to write a shell that runs under all shells (not specific to bourne) and by not specifying a magic number in the script.

Again, the script you are building would have to be built with the lowest common denominator of shell functionality common to all shells. As criglerj pointed out, you must exclusively use syntax that is common to all shells to get this project to work.

this is a shell script that uses the normal "#!" directive and that the user is running by calling "sh scriptname" ... if the OP has a particular requirement to set SHELL, which is an environmental variable, then the code is to check $SHELL is required ... however, the "#!" directive has already set the script's run shell as "/bin/sh" so the run shell --- whether SHELL is reset to a different shell or not --- is technically "/bin/sh" ...

True enough. Should have caught that. That magic number should be removed. In all fairness, I agree with your statement earlier - that is - pick a shell and go with it. I dont think trying to write a generic shell script to run under all shells is worth the effort (just my opinion)

google, thanks for the links.

And thanks to all for the opinions. I guess I will have to stick to one SHELL and build !

Vino