Visit Our UNIX and Linux User Community


test script to identify SHELL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test script to identify SHELL
# 1  
Old 08-26-2009
test script to identify SHELL

I am new to BASH and writing a small script to identify the SHELL .

Code:
#!/bin/bash
BASH='/bin/bash'
KSH='/bin/ksh'
if [$SHELL == "$BASH"]
then
echo "it's Bash"
else
echo "it's not Bash"
fi


Code:
$ bash -x a.sh
+ BASH=/bin/bash
+ KSH=/bin/ksh
+ '[/bin/bash' == '/bin/bash]'
a.sh: line 4: [/bin/bash: No such file or directory
+ echo 'it'\''s not Bash'
it's not Bash

where am I missing . PLease advice .

Thanks
# 2  
Old 08-26-2009
have you tried:
Code:
echo $0

the $SHELL variable refers to the user's default shell (in /etc/passwd) - it may not reflect the shell that the user is currently running.

Last edited by avronius; 08-26-2009 at 01:21 PM.. Reason: added comment
# 3  
Old 08-26-2009
Quote:
Originally Posted by talashil
I am new to BASH and writing a small script to identify the SHELL .

Code:
#!/bin/bash
BASH='/bin/bash'
KSH='/bin/ksh'
if [$SHELL == "$BASH"]
then
echo "it's Bash"
else
echo "it's not Bash"
fi


Code:
$ bash -x a.sh
+ BASH=/bin/bash
+ KSH=/bin/ksh
+ '[/bin/bash' == '/bin/bash]'
a.sh: line 4: [/bin/bash: No such file or directory
+ echo 'it'\''s not Bash'
it's not Bash

where am I missing .

Read the error message. What command is not found? Is that the command you are trying to execute?


You are missing spaces before and after brackets:

Code:
if [ "$SHELL" = "$BASH" ]

Note that $SHELL doesn't necessarily contain the shell you are currently running; it contains your default shell.
# 4  
Old 08-26-2009
You need a blank between both variables and the square brackets, otherwise neither shell can parse it correctly. And it would be advisable to put the $SHELL variable in quotes too.
# 5  
Old 08-26-2009
Thankyou .

if you put "echo $0" inside the script , it will give the script name .

Thanks
# 6  
Old 08-26-2009
Having said "echo $0", this won't work inside the shell script.
Calling $0 inside a sh/bash/ksh script will recall the name of the script.
You can pass this TO your script at the command line.
For example:
Code:
 cat vartest.sh
#!/bin/sh

usage()
{
   echo "Usage:"
   echo "      $0 \$0"
}

if [ $# -lt 1 ] ; then
   echo "Don't forget to send the shell information"
   usage
   exit 1
fi

echo "the shell is $1"
exit 0

Code:
./vartest.sh
Don't forget to send the shell information
Usage:
      ./vartest.sh $0

Code:
./vartest.sh $0
the shell is -bash

# 7  
Old 08-26-2009
Thankyou .

My problem is it's not seperate script . I have to include it in another script .

can you think of any other logic to identify the shell inside another script ..

Code:
#!/bin/bash

if [ "$SHELL"  == "/bin/bash" ]
then
    echo "blah"
else
    echo "aaa"
fi


Thanks

Previous Thread | Next Thread
Test Your Knowledge in Computers #781
Difficulty: Medium
At 17, Bill Gates formed a venture with Steve Ballmer called Traf-O-Data to make traffic counters based on the Intel 8008 processor.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with Shell Script to identify lines in file1 and write them to file2

Hi, I am running my pipeline and capturing all stout from multiple programs to a .txt file. I want to go into that .txt file and search for specific lines, and finally print those lines in a second .txt file. I can do this using grep, awk, or sed for each line, but have not been able to get... (2 Replies)
Discussion started by: hmortens
2 Replies

2. UNIX for Dummies Questions & Answers

Shell Script to test telnet connection using port

Hello, I need to test telnet connections using port number for few hosts. Could you please help me? Thanks !! (1 Reply)
Discussion started by: skhichi
1 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. Shell Programming and Scripting

Identify variables used in the shell script

Hi, Is there any simple way to get/identify the variables that are used in a file and print those variable names. If I have something like this in a file, /$var/temp_dir/${var2}${var3}.log I want to display the variables 'var', 'var2' and 'var3' from that file. I tried something like... (6 Replies)
Discussion started by: pvamsikr
6 Replies

5. UNIX for Dummies Questions & Answers

Test: argument expected error in shell script

Hi, I am trying to write a small script that validates if there exist files that start with a pattern in a given directory. Below is the piece of my script: #!/usr/bin/ksh BTFDIR=/opt/ships/temp if then echo 'found' else echo 'not found' fi When I run this... (2 Replies)
Discussion started by: snvniranjanrao
2 Replies

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

7. Shell Programming and Scripting

Shell script to identify the number of files and to append data

Hi I am having a question where I have to 1) Identify the number of files in a directory with a specific format and if the count is >1 we need to concatenate those two files into one file and remember that in the second file the header should not be copied. it should be form first file.... (4 Replies)
Discussion started by: pradkumar
4 Replies

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

9. Shell Programming and Scripting

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 (8 Replies)
Discussion started by: nck
8 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

Featured Tech Videos