Test for shell interpreter at beginning of script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test for shell interpreter at beginning of script
# 1  
Old 06-24-2009
Test for shell interpreter at beginning of script

What would be the best way or method to determine or test for the shell interpreter at the beginning of a script in the event one shell is not available?

If I use the following:
#!/bin/bash

and /bin/bash is not available, then use I'd like to use /bin/ksh if it is available.

#!/bin/ksh
# 2  
Old 06-24-2009
One thing I have done over the years is create a standard set of libraries that I source near the top of my scripts with something like this:

Code:
[ -f ${LIBDIR}/bashlib.sh ] && . ${LIBDIR}/bashlib.sh

Within that library file I have something like this:
Code:
if [ -z "${BASH}" ]
then
    $printf "${SCRIPT}:${LINENO}: please run this script with the BASH shell\n" >&2
    exit $EXIT_VAL
elif [ ${BASH_VERSINFO} -lt 3 ]
then
    printf "${SCRIPT}:${LINENO}: BASH version 3 or greater is required\n" >&2
    exit $EXIT_VAL
fi

Unfortunately, I don't know of a way to dynamically change the "shebang" line since it is the first line of the script. You could potentially use a wrapper that uses a shell that you know is always available which then calls your other scripts using the preferred and available shell.

I understand not having the same shells available when you have multiple platforms (i.e. Solaris, HP-UX, Linux, etc.), but I would be concerned if you have systems that are the same platform and version but with different installation standards. This is especially true in a large data-center. Also, if you know that systems will not all have the same shells, I would recommend using the least common denominator. So if the all have sh or csh, I would write my scripts using one of those.

Just my .02.

-B
# 3  
Old 06-24-2009
I agree with "bwhited".
If possible, write the script to the lowest common denominator.

Failing that write a controlling script in basic shell which calls named scripts containing a shebang for the preferred shell.

I usually use "uname -s" to identify the environment, but a truly portable script will need to know versions.

IMHO variations on "echo", "find", "awk" (or "nawk"), and "df" (or "bdf") are the biggest pain. You either need a portable script which decides which command suits the environment or a suite of custom scripts for each environment.

BTW. If all your systems are modern this board will seek out the POSIX shell. No problem there.

Others may wish to comment.
# 4  
Old 06-24-2009
If it's sketchy at all, stick with /bin/sh.
# 5  
Old 06-24-2009
Current /bin/sh (POSIX) is quite different from say Berkeley /bin/sh (BSD).
That is the issue.
# 6  
Old 06-25-2009
I think most systems used now have POSIX built in commands, correct?

Interesting point, that Classic Shell Scripting book suggests using printf instead of echo due to this, but I've not really seen this in practice.
# 7  
Old 06-29-2009
I learned heavy way about 1991, don't use system *sh paths or do any rename or ...
In one unix sh was special sh and there was also sh5. I copied sh5 to sh and after that "no boot ...".

After that I have done every system where I execute any scripts:
cp somecorrect_sh /usr/local/bin/mysh
And after that I have used mysh in my script
#!/usr/local/bin/mysh

It's so different to use ksh89, ksh93, posix-sh, original Bourne Shell, bash, ... so often /bin/sh is some of those. It depent which *nix and which release you are using. /bin/sh is some sh ...

Example, if you like to use always ksh93, then download it
software download selections

Same situation/method with bash + awk. Today I know which version I'm using. I have done own copies.


If you like to test, try substr
a="123"
print ${a:1:1}
# result must be 2, if you have posix-sh/ksh93 compatible shell
# if result is something else, then you have old sh: ksh89, old posix-sh or bourne shell
# if you have bash, print give error, bash can use echo and printf, but not print,
# maybe bash 4 include print, I have not tested
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding test to beginning and end of list in a file

Hi all and apologies for the silly question, but I've searched and I can't get this right. I have a list of email addresses in a file that I need to blacklist (spam). the list is quite long and I would need to script a small routine so that I can get the following for each line in the file: db... (4 Replies)
Discussion started by: bm555
4 Replies

2. Solaris

Perl Interpreter Default Shell

Hello. We have recently changed from HP Unix Servers to Solaris. On the HP Severs we were using the POSIX shell. Solaris is using the Bourne sh. We are using ksh as our default shell after login. The problem we are encountering is when something like Perl or Cron uses the shell to execute a... (5 Replies)
Discussion started by: mix123
5 Replies

3. Shell Programming and Scripting

Test shell script (PROBLEM)

Dears , kindly I wanna do test for one KSH script to know how is it working , the problem that I'm facing is whenever put "sh -x ./my_script.sh" the output seems very long & although I tried to to redirect it to files as it shown , but it failed :eek: :- sh -x ./my_script.sh >... (2 Replies)
Discussion started by: arm
2 Replies

4. UNIX for Dummies Questions & Answers

Shell BASIC interpreter(s)...

I am looking for a simple BASIC Interpreter written in a shell scripting language. For me something like this would be a great learning tool... After much goggle eyed Googling I came upon this:- https://gist.github.com/cander/2785819 It is small and I haven't tried it yet as I am at work... (6 Replies)
Discussion started by: wisecracker
6 Replies

5. Shell Programming and Scripting

Help with connectivity test using shell script

I want to test connectivity between different servers with my server using information as IP and port only. I have Name,IP List and port in one file. Please help how i can test connectivity is successful or not? File format will be: Name1,127.0.0.1,80 Name2,127.0.0.2,8080 Output could be ... (1 Reply)
Discussion started by: poweroflinux
1 Replies

6. Shell Programming and Scripting

-a test in shell script

I need clarification in -a test. If say, in test -a left expression is not present but the right expression is present, do the shell will consider the left expression true and evaluate the right expression? For example: if ] then rm -f ${file} fi Is this test condition... (7 Replies)
Discussion started by: jatanig
7 Replies

7. Shell Programming and Scripting

test script to identify SHELL

I am new to BASH and writing a small script to identify the SHELL . #!/bin/bash BASH='/bin/bash' KSH='/bin/ksh' if then echo "it's Bash" else echo "it's not Bash" fi $ bash -x a.sh + BASH=/bin/bash + KSH=/bin/ksh + '' a.sh: line 4: where am I missing . PLease advice . (10 Replies)
Discussion started by: talashil
10 Replies

8. UNIX for Dummies Questions & Answers

Shell and commandline interpreter-definitions

What is the difference between the(a) shell and the (a) command-line interpreter? Here we're talking about the complete dummy question, but could someone point me right. (yes, have written scripts in for instance bash shell, and and grepp-ed my way around ....:eek: (4 Replies)
Discussion started by: amkgw
4 Replies

9. Windows & DOS: Issues & Discussions

rediretion and pipes in DOS shell cmd interpreter

Hello, I am trying to accomplish the following. Send the output of a command to the screen (this happens by default) as well as capture the output of the screen to a log file. How can this be achieved in DOS command interpreter syntax. Any ideas/suggesstions/indicators are greatly... (2 Replies)
Discussion started by: jerardfjay
2 Replies

10. Shell Programming and Scripting

need help with test condition in shell script

I'm new to scripting and I need help with a bourn shell script. What i'm trying to do is a test condition where "if the time is within 2 hours, it's true" and so on. The time is in the following format DATE=`/bin/date +"%Y%m%d%H%S"` for example, 20060907152000. So, what the script first... (9 Replies)
Discussion started by: pieman8080
9 Replies
Login or Register to Ask a Question