Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have two servers one AIX with default shell ksh, the other RHEL with default shell bash.

I have a script that is mounted on both that will run similar commands but for either AIX or Linux. This script does not work on the bash servers, is there a way to make this script run on both bash and ksh, or would the best option be to create two different scripts?

#!/usr/bin/ksh
export OS=`uname -s`
echo "OS is "$OS"."
case $OS in
        "AIX")
                #run AIX commands;;
        "Linux")
                #run Linux commands;;
        "*")
                echo "Exiting. The OS type is not found.";;
esac
echo "Done."
exit 0

UPDATE

The commands I need to run are for user accounts on each server. An example of unlocking an account. AIX /usr/bin/chuser account_locked=false $USERNAME

Linux /usr/bin/passwd -u $USERNAME

Through further investigation I have found that the the shells location in AIX are located at /usr/bin/sh, while Redhat's are located at /bin/sh.

Can I define the shebang based on the results of "uname"?

share|improve this question
    
If AIX commands and Linux commands are very similar, it may make sense to put them in the same script with some extra code to handle the differences. If AIX commands and Linux commands are completely different, it may make more sense to have two different versions of the script. –  jw013 Aug 5 at 20:16
2  
I’m curious whether the issue is (1) the difference between AIX commands and Linux commands, or (2) the difference between ksh scripting style and bash scripting style. In case 1, I would agree with @jw013 and encourage you to assess how much commonality there would be the two scripts, and how much specialized stuff, and decide based on that. But if it’s 2, I would suggest that you back out the ksh-isms and “dumb down” the script to follow POSIX shell conventions, so it works in both shells. –  Scott Aug 5 at 22:08

3 Answers 3

up vote 1 down vote accepted

It would probably be easier to have two separate versions of the script. Since it is short it may not be worth adding extra code to handle the differences between the two formats as jw013 suggested. On the other hand if you had a larger script it would probably be easier to have one and make the script execute different commands depending on where it is running from.

share|improve this answer

The easiest way by far is to use the same shell on both systems. Just because one shell is preinstalled (there's no such thing as a “default shell” for scripts, the shell is whatever the shebang line says) doesn't mean that you can't install others. You can install bash on AIX (from the toolbox, for example), or ksh93 for Linux (with your distribution's package manager: install the ksh package).

If you pick ksh, beware that /usr/bin/ksh on AIX is ksh88. There is no version of ksh88 for Linux, only ksh93, which is available as /usr/bin/ksh93 on AIX.

It is easier if the same shell is installed at the same location on both systems (a symbolic link is fine), so that you can use the same path in the shebang line. If having the same location is too difficult, you can use something like #!/usr/bin/env bash, as long as you ensure that bash is in the PATH on both systems. This may be useful if you have to install bash or ksh in your home directory because you don't have root access.

If you really can't install the same shell on both machines — presumably because there's some silly compliance rule that only complicates people's life but is too entrenched to overturn — you have a few possibilities.

  • Use #!/bin/sh as your shebang line and program in the intersection of what your systems offer. All modern unix systems provide a POSIX shell as /bin/sh. On AIX, /bin/sh is ksh88. On Red Hat, /bin/sh is bash (which behaves slightly differently when invoked as sh). Beware that on some other operating systems (for example, on many Linux distributions), /bin/sh is a smaller shell which may not have much more than POSIX features.
  • Start your scripts with some boilerplate code that looks for a better shell and executes it.

    #!/bin/sh
    if [ -n "$BASH" ]; then
      … bash compatibility code …
    elif type whence >/dev/null 2>/dev/null; then
      … ksh compatibility code …
    elif type ksh93 >/dev/null 2>/dev/null; then
      exec ksh93 "$0" "$@"
    elif type ksh >/dev/null 2>/dev/null; then
      exec ksh "$0" "$@"
    elif type mksh >/dev/null 2>/dev/null; then
      exec mksh "$0" "$@"
    elif type bash >/dev/null 2>/dev/null; then
      exec bash "$0" "$@"
    else
      echo 1>&2 "Cannot find ksh or bash, aborting"
      exit 125
    fi
    

In either case, the intersection of ksh88 and bash provides some useful features beyond POSIX, but you may need a little compatibility code. In particular:

  • Array assignment in ksh88 uses set -A. See assignation variable under different ksh environment
  • Local variables are declared by typeset.
  • Ksh88 doesn't have ${VAR/PATTERN/REPLACEMENT}, $'…' or FIGNORE. It does have [[ … ]].
  • To enable @(…) and other ksh extended patterns in bash, run shopt -s extglob.
share|improve this answer

I would put some work into seeing if you can actually have one version with command that work on both systems.

I had this issue with my .bashrc file which I keep in my dotfiles repository. I want it to work on both OSX and Ubuntu and there were a few command I had to modify to do that. In this case it was mostly which switches worked for certain command or how a test command was written. I tackled each issue, finding a way to do whatever command I was trying in a form that would work on both OS's

I tried the 'two different files' and 'two different sections depending on OS' approaches and found them a pain to maintain.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.