Question about Shebang line of Bash Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about Shebang line of Bash Script
# 1  
Old 04-04-2014
Question about Shebang line of Bash Script

Hello All,

I was writing a Bash shell script that will be executed on both an AIX server (/usr/bin/ksh) and a SLES server (/bin/bash). The AIX server
has Bash installed at "/usr/bin/bash", which is in a different dir then the SLES server.

So basically I am writing the script on the SLES server using Gedit, really just because the AIX version of vi is so dreadfully painful to
use... Then transferring the file to the AIX server. But when I transfer from the SLES to the AIX server I need to change the Shebang
line depending upon which server the script is being executed on.

Is it possible to do something like this, instead of needing to manually change the location of the interpreter?
Code:
host=$(uname -s)

if [[ $host == "Linux" ]]
 then
    #!/bin/bash
    echo "Setting as Linux..."

elif [[ $host == "AIX" ]]
 then
    #!/usr/bin/bash
    echo "Setting as AIX..."
fi

So is something like that possible? If not its fine... I guess I could just make a symbolic link on one of the servers so I could just keep it
the same, but was curious so I thought I'd ask.

Any thoughts would be much appreciated!

Thanks in Advance,
Matt
# 2  
Old 04-04-2014
I don't think this is possible.
However a little bit of lateral thinking here.

Save the file twice but with differing filenames one containing #!/bin/bash and the other with #!/usr/bin/bash as the shebang...

Write a simple third script with a shebang line as #!/bin/sh to detect which bash and autolaunch your required script with or without a separate terminal...

(If this makes sense.)

Just an idea...
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 04-04-2014
You can use the env command in your shebang line. I write scripts for Solaris, AIX, and Linux and using that command will locate the interpreter on the given system:

Code:
#!/usr/bin/env bash

These 2 Users Gave Thanks to in2nix4life For This Post:
# 4  
Old 04-05-2014
Hey guy's, thanks for the replies. Much appreciated...

Some good ideas, thanks.
I decided to just go simple and create a symbolic link on the AIX server.
Code:
# ln -s /usr/bin/bash  /bin/bash

Seems to have done the trick. Don't know why I didn't think of that before...

Thanks Again,
Matt
# 5  
Old 04-06-2014
good and quick one to use a soft link unless there is an existing binary already and as long as it will not impact other applications/scripts.
This User Gave Thanks to udaylingamaneni For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Question on bash command line

OS : RHEL / Oracle Linux 6.8 In bash shell, how can I replace a character under the cursor with another character ? In the below example , after I typed the following line, I realized that I meant 7013 and not 2013. So I move the cursor to the left and keep it on top of 2 (of 2013) and I want... (7 Replies)
Discussion started by: kraljic
7 Replies

2. Shell Programming and Scripting

C shell script wont terminated if i don't modify the shebang

Hi all, I'm new to shell script i wrote some shell script for my colleague, everyone is fine,except on user we are using VNC viewer to work and there are some script start with shebang #! /bin/csh there is an user will not terminate after running the script even if a hello world i need... (5 Replies)
Discussion started by: pilistar0222
5 Replies

3. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

4. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

5. Shell Programming and Scripting

How to start powershell with shebang from windows/cygwin/bash?

I would like to the the windws8/cygwin/bash shebang feature to start a powershell script. I do a "chmod +x set-sound.ps1" and then at a bash prompt I do ./set-sound.ps1 The first line of ./set-sound.ps1 #!powershell.exe -ExecutionPolicy unrestricted The result is the result: ... (5 Replies)
Discussion started by: siegfried
5 Replies

6. Shell Programming and Scripting

Necessity of shebang line

Hi , I know about the shebang line in shell scripting. Just want to know whether is there any difference in execution of the program by keeping and not keeping the shebang line. Because without shebang line also the script is working. correct me if am wrong. Any help on this will be helpful (5 Replies)
Discussion started by: rogerben
5 Replies

7. Shell Programming and Scripting

Can we use two shebang statements in a single shell script?

Hi, As per my understanding, we can use two shebang statements in a single shell script. Please see below snippet- #!/bin/bash .......## some code A #!/bin/csh .......## some code B exit 0; Here, code A will be executed using bash shell and code B will be executed with c shell. ... (9 Replies)
Discussion started by: tarunmudgal4u
9 Replies

8. Shell Programming and Scripting

Error in shell script when #!/bin/bash is used as shebang

#!/bin/ksh echo -en "\033|||'-')) echo -e "\033 The above script works fine when the interpreter is ksh, but outputs the following error when #!/bin/bash is used as shebang: test.sh: line 5: syntax error near unexpected token `(' test.sh: line 5: `case "$ACTIVATION_KEY" in +(|||'-'))' (2 Replies)
Discussion started by: proactiveaditya
2 Replies

9. Shell Programming and Scripting

Relacing the shebang line of a file

Can any one tell me how to replace a shebang line of a file using sed? Eg: If a file contains the following shebang line #!C:/InstantRails/ruby/bin/ruby I would like to replace it with #!/usr/local/bin/ruby The shebang line of the file can be obtained from the command cat... (3 Replies)
Discussion started by: linuxnewbe
3 Replies

10. Shell Programming and Scripting

Doubt in shebang line!!

Do we need to include the exclamatory mark in the shebang line??:confused: What if we dont include it??:eek: Actually what shebang line implies when we run a script?? shebang line--> #!/bin/ksh :p (6 Replies)
Discussion started by: nohup
6 Replies
Login or Register to Ask a Question