From bash to csh and zsh

... Am I glad to find this forum (and vBulletin too, nice!)..

OK, here's my issue. I have been handballed a bash script, not pretty but functional. I need to change to csh and zsh. For the csh I have the basics (e.g., such as change if/fi to if/endif, quote the variables, and bracket commands). For zsh (ahem), I have no idea and googling for resources hasn't been helpful.

So rather than bash (pun not intended) my head against a screen I decided to see if anyone here is more familiar with this fairly simple conversion process..

Anyway here's the bash.. I need csh and zsh to do the same:

#!/bin/bash
if echo $HOSTNAME | egrep -q '^somebox0' ; then
        if [ ! $PUBLIC_ENVIRONMENT ];
                then if [ $USER != "root" ];
                        then if grep -sq $USER /usr/local/etc/localusers.$HOSTNAME ;
                                then echo ' *** Interactive access permitted - be nice to others!'
                        else
                                echo ' *** Interactive logins are not permitted on this machine - use PUBLIC instead!'
                                exec /bin/false
                        fi;
                fi;
        fi;
fi;

Out of interest, why?

If you stick to "sh" basics then both ZSH and BASH can run it.

Also systems with csh normally have sh anyway. It sounds like an exercise in generating work and then making more things to maintain. Stick to one common version that works with sh and you will save yourself effort in the long run.

Because some users (boffins) want it. More than one person will use this script, and some are pretty tied to their preferred shell, and ne'er reason nor rhyme will convince them otherwise.

I thoroughly agree with you btw (script in sh, whenever possible), but that doesn't help for the people whom I work for!

The shell that a script uses is irrelevant to the person calling it. That is the purpose of the "#!/bin/sh" or similar at the start. This is in the same manner that you don't care if a compiled program is written in C, C++, Pascal or raw assembler.

What you will end up with is a nightmare for maintaining the script as you will have three separate versions.

Not in this case.

Which is what I requested in the first post.

Write one script and then have

#!/bin/csh
commonScript
#!/bin/bash
commonScript
#!/bin/sh
commonScript
#!/bin/zsh
commonScript

:rolleyes:

You know that doesn't work. bash/sh use if/fi constructs whilst csh uses if/endif for starters. As I said in the first post :wink:

Or rather you think that won't work, however if the common script starts with

#!/bin/sh
....

then the common code will be executed by /bin/sh.

If you want to deal with error checking you could do

#!/bin/whatevershellexceptcsh
commonCode
if test "$?" != "0"; then exit 1; fi

and for csh

#!/bin/csh
commonCode
if ( $status != 0 ) then
     exit 1
endif

then put all the fancy logic once in commonCode executed by /bin/sh as follows

#!/bin/sh
...do my checking....

Thanks for nothing.

How does that not solve the problem? You end up with one place in which you change your logic and can be called from different script engines.