Dynamically choosing the interpreter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamically choosing the interpreter
# 1  
Old 04-16-2012
Dynamically choosing the interpreter

Hi,

Is it possible to choose the inerpreter conditionally.
For example, if
Code:
whereis bash

returns
Code:
/usr/bin/bash

then i need to choose
Code:
#!/usr/bin/bash

else i need to use
Code:
#!/usr/bin/sh

.

Is it possible to achieve in a shell script?

Thanks
# 2  
Old 04-16-2012
That's not possible, I'm afraid, you can use /usr/bin/env as shebang to find the user's preferred command, or rather, the command in the user's environment. But you can't "change" the shebang dynamically.

I'd go for something like this
Code:
#!/usr/bin/env sh

mybash=$(command -v bash 2>/dev/null)
## maybe switch to bash
if test "${0}" != "bash" -a -n "${mybash}"; then
  exec command bash $0 $@
fi

or something along the lines of that.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Linux

Help choosing distro

Hi, I just ordered an Skylake NUC and will run Linux on it. My distro of choice has been Ubuntu but I am fed up with the release cycle and would like more of a rolling release. I would say I am an intermediate level Linux user. It's going to be a HTPC, I want to have the latest kernels... (0 Replies)
Discussion started by: rthorntn
0 Replies

2. Linux

Choosing a UNIX

i have a project in numerical calculus in c language what unix i get better for this? (7 Replies)
Discussion started by: gitac
7 Replies

3. Red Hat

Choosing best location for authorized_keys!

Hello Friends, I am trying to troubleshoot one scenario for the customer. In their server configuration, each vhost has it's own user. The option is to shell access chrooted. The question is where would be the best place to store the authorized_keys file so that we can ssh in from the... (0 Replies)
Discussion started by: user3528
0 Replies

4. Slackware

Help for choosing Slackware

Dear Friends, If I use Slackware for learning whether it will make any confusion in administering/using Redhat and SuSE since I have checked slackware is more like BSD. Thank you. (4 Replies)
Discussion started by: Tlogine
4 Replies

5. Linux

Choosing the best distro.

Hi all Help me find the best distro for the following configurations: Intel pentium IV 1.6 Ghz 128 MB RAM :( 40 GB Hardisk with one very big partition more than 35 gb n another 2 gb partition. windows xp is already installed but has enough free space (26gb). Which linux will be... (0 Replies)
Discussion started by: bbala
0 Replies

6. UNIX for Dummies Questions & Answers

choosing secure and stable OS

I would like to choose an OS which is both very secure and very stable. Which one of these 2 would be the best option: FreeBSD or Solaris 10 ? I plan to use this OS as a very basic desktop pc using: a word processing program, emule/amule/p2p and an email client such as evolution. (1 Reply)
Discussion started by: rivo
1 Replies

7. UNIX for Dummies Questions & Answers

I need help choosing a linux.

There are soo many flavors of linux and i just cant choose/find the right linux for me... I am hopeing for a linux that is a Workhorse Can fit of a seires (or 1) disk(s) Lots of features (admin/mod features) A learning mode or a detailed tutorial Can work side by side with windows... (12 Replies)
Discussion started by: lithuaniaakid
12 Replies

8. UNIX for Dummies Questions & Answers

Choosing a version

I know that the rules say no school questions but I am in advanced topics and am going to go to college for programming and I want to find a easy first OS to start me out, please help, thanks (3 Replies)
Discussion started by: KoKo
3 Replies

9. Programming

Choosing Java or C++

Listen, if you know a bit of programming in C and need to program unix-type operating systems the next transitive stage is for sure C++. However, have in mind that Java is like learning C but 99% object-oriented(o.o.) and with no pointers or memory-management tricks. It would be good for you to see... (5 Replies)
Discussion started by: SolidSnake
5 Replies
Login or Register to Ask a Question