How to find out the shell of the shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find out the shell of the shell script?
# 1  
Old 02-28-2012
How to find out the shell of the shell script?

Hello
My question is: How to find out the shell of the shell script which we are running? I am writing a script, say f1.sh, as below:

Code:
#!/bin/ksh

echo "Sample script"

From the first line, we can say this script will run in ksh. But, how can we prove it? Can we print anything inside the script to verify it? echo $SHELL wont help since it will show the login shell which in my case is bash. echo $0 also wont help since $0 in this case will be the name of the script. The ps command does not help. It will show the name of the script in the process list, not the shell as such.

Please advice.

Last edited by guruprasadpr; 02-28-2012 at 02:21 AM..
# 2  
Old 02-28-2012
i created a test.sh with the below contents and just executed it.

Code:
$ cat test.sh      
#!/bin/ksh
echo $SHELL
echo $0
while [ 1 ]
do
    sleep 5;
done

after that, i tried one more terminal and did the below command

Code:
ps -ef | grep test

the output of the above commands shows that the shell script was executed with the ksh

Code:
$ ps -ef | grep test
 kamaraj 20943  4832   0 06:24:44 pts/10      0:00 grep test
 kamaraj 20936 13784   0 06:24:32 pts/11      0:00 /bin/ksh ./test.sh

---------- Post updated at 12:00 PM ---------- Previous update was at 11:59 AM ----------

my default shell was

Code:
 
$ echo $SHELL
/usr/bin/bash

and the echo $SHELL was printed the /bin/bash
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 02-28-2012
Code:
#!/bin/ksh
p="${0/.\//}"
ps -ef|awk -vp="$p" '!/awk/&&/'$p'/{print "Script is in the -> " $(NF-1)}'
sleep 1

Code:
# ./check
Script is in the -> /bin/ksh


Last edited by ygemici; 02-28-2012 at 07:10 AM..
This User Gave Thanks to ygemici For This Post:
# 4  
Old 02-28-2012
The very fact that the first line (#! /bin/ksh) is interpreted without errors proves that it is on ksh.

If you tried to use hello shell (any non-existent shell) - #! /bin/hello, it would throw an error: -bash: ./test.sh: /bin/hello: bad interpreter: No such file or directory, right.
# 5  
Old 02-28-2012
@balajesuri: I think you did not get my question. My question is not whether the shell in the shebang line is a valid ksh or not. The question is say if you are asked in which shell your script is running, how to confirm or prove it?


@ygemici,itkamaraj: thanks..
# 6  
Old 02-28-2012
I understood your question. What I'm saying is, if the shebang line (#! /bin/ksh) is interpreted without errors, isn't that a sufficient proof that it is indeed on ksh?
# 7  
Old 02-28-2012
The full comprehensive and exhaustive answer to this question is on the excellent Mascheck site.

what shell is this
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Help script shell find fichier

Hello, I am looking for a shell script that can 1- take as input a variable, like "server.cpu" 2- do a search for that variable in a directory that contains subdirectories. The search will start at the last subdirectory working up to the top level if I can not find the file 3-... (7 Replies)
Discussion started by: georg2014
7 Replies

3. Shell Programming and Scripting

Find and Replace in Shell script

Friends, I have more than 1000 lines in text file which needs to be converted as UPPERCASE by adding _ com.sun.url=www.sun.com com.ssl.port=808 com.ui.path=/apps/ssi Expected output com.sun.url=_COM.SUN.URL_ com.ssl.port=_COM.SSL.PORT_ com.ui.path=_COM.UI.PATH_ Thanks in... (4 Replies)
Discussion started by: baluchen
4 Replies

4. Programming

Shell script to find Percentage?

Hi all, I havea log of data.log bear,10000,white bear,5000,black chicken,2000,white chicken,4000,yellow chicken,3000,black lion,6000,yellow lion,1000,white How can we have shell script to get the percentage of each animals? Thanks. (8 Replies)
Discussion started by: sabercats
8 Replies

5. Shell Programming and Scripting

Find command in Shell Script

hi I am a newbee in Shell scripting (hardly 7 days) I have to execute a shell script which looks like this #!/bin/sh var1=`date +"%Y%m%d"` echo $var1 find . -name "$var1*" -exec mv {} Delete/ \; the find command in the script is running independently but when kept in this script it is... (24 Replies)
Discussion started by: sweetnsourabh
24 Replies

6. Homework & Coursework Questions

Help with find/whereis C Shell Script

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a C Shell Script called "hunt" that takes a filename as it's single parameter and displays the full pathname of every file name that matches,... (1 Reply)
Discussion started by: new2C
1 Replies

7. UNIX for Dummies Questions & Answers

Shell script 'find' command

I have a script that has the following command: find /home/user -name test.dat The script works as desired when running normally. However, when I run the script preceding it with 'sh', it fails. Is there something I need to account for when preceding the execution of the script with 'sh'? (1 Reply)
Discussion started by: bsavitch
1 Replies

8. Shell Programming and Scripting

c shell script help with find

Okie here is my problem, 1. I have a directory with a ton of files. 2. I want to first get an input on how many days ago the files were created. 3. I will take those files and put it into another file 4. Then I will take the last # from each line and subtract by 1 then diff the line from the... (1 Reply)
Discussion started by: bigboizvince
1 Replies

9. UNIX for Advanced & Expert Users

find command from a shell script

Hi experts, I have a shell script (korn shell on aix) where I am giving find command with file options which are read from a configuration file. For some reason I am getting an error find: 0652-017. I have put set -x in the shell script and the command looks okay. If I cut it and paste it in the... (6 Replies)
Discussion started by: kodermanna
6 Replies

10. Shell Programming and Scripting

shell script to find files

I have a directory which contains files with different kinds of extensions .everyday a file with .log gets added to it .i want to extract the file with .log extension which is created with todays date . thanks in advance (2 Replies)
Discussion started by: naren_samba2005
2 Replies
Login or Register to Ask a Question