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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can we use two shebang statements in a single shell script?
# 1  
Old 12-06-2010
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.

Please let me know, in case, if I'm wrong.

Also, please tell me how to know the current running shell in shell script.



Regards,
Tarun0
# 2  
Old 12-06-2010
no you cannot use it.
# 3  
Old 12-06-2010
The shebang works only when
1. it is on the first line
2. it starts in column one

If you need to run some code in another shell -
Code:
#!/bin/bash
....
...
/bin/ksh -c ' your command goes here'  # can't type - removed shebang

This executes one line using ksh. Otherwise you have to invoke the other shell in a separate script.
Code:
#!/bin/bash
/path/to/script.csh
/path/to/anotherscript.ksh

I added .csh and .ksh extensions to show what shell they use, the .ksh extension, for example, has no effect on actual shell choice in the script.

Last edited by jim mcnamara; 12-06-2010 at 08:56 AM..
# 4  
Old 12-06-2010
@jim

Quote:
Originally Posted by jim mcnamara
[..]
If you need to run some code in another shell -
Code:
#!/bin/bash
....
...
#!/bin/ksh -c ' your command goes here'

This executes one line using ksh. Otherwise you have to invoke the other shell in a separate script.
[..]
Without the shebang then, no?
Code:
/bin/ksh -c ' your command goes here'

# 5  
Old 12-06-2010
scrutinizer - thanks, I fixed it. My bad.
# 6  
Old 12-07-2010
Thanks guys !

Still there is one doubt!

what happens when we source a script having shebang statement of C shell(for instance) into the script having shebang statement of K shell?

In this case, does the sourced script(with C shell shebang) execute in C shell(child process) and return the output in running script with K shell(parent shell) shebang.

See below snippet-

running script is -
#!/bin/ksh
...some code...
source <sourced script name>

exit 0;

where the sourced script is -
#!/bin/csh
...some code;
exit 0;
# 7  
Old 12-07-2010
You cannot source the csh script inside the ksh script, you need to execute it so that it runs in a separate (c-)shell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple expect/send statements not working in shell script

Hi I am trying the following in my bash script which logs into my machine and runs a command. Trying to solve this using expect. The first expect statement is hit and it enters the address "10.10.0.10" but when the second expect statement is hit it exits #!/bin/bash expect -c ' spawn... (2 Replies)
Discussion started by: skorada
2 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. Programming

Shell script - if statements dont work

hi all, i have made a shell script and it runs until it reaches the if statement, doesn't the ! mean only if the command fails it will echo me that message and then exit can anyone please help me what is wrong with my code? many thanks, rob #!/bin/bash echo "is this archive... (10 Replies)
Discussion started by: robertkwild
10 Replies

4. Shell Programming and Scripting

Can we convert 3 awk statements in a single statement

Hi, Can we use 3 statements convert in a single statement. First statement output using the second statement and the second statement output using the third statement please let me know the syntax so that I can able to merge all the three statement. (2 Replies)
Discussion started by: Priti2277
2 Replies

5. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: mrm5102
4 Replies

6. Shell Programming and Scripting

Run a single script in different shell

Hi All, I am new in UNIX.I have a situation here.:confused: I have a script which have to be run in every shell (like KSH,BASH,CSH). Script may be any thing. But i don't know how to do it.Because the syntax of every shell may be different. Please advise.. Thanks for your kind advise in... (5 Replies)
Discussion started by: jdash.ps
5 Replies

7. 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

8. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

9. Shell Programming and Scripting

Running remote shell script containing sql statements

I have a shell script which resides on three SCO machines containing some simple sqlplus statments. I need to run these scripts remotely. Currently, I am trying to use rsh to do so: rsh hostname myscript args The problem is that the arguments to the sqlplus statements in the remote shell... (4 Replies)
Discussion started by: Madbreaks
4 Replies

10. Shell Programming and Scripting

single input shell script?

hey, i'm trying to write a shell script which accepts: operand operator operand then, the script would see which operator it is (using case) and calculate it... but i dont know how to do it correctly with $1 $2 $3... (eliminating accepting separate inputs) (1 Reply)
Discussion started by: quipy
1 Replies
Login or Register to Ask a Question