LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-22-2006, 03:43 PM   #1
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Rep: Reputation: 34
Figuring out which shell?


Hi.

Is there a shell script command hat detects the type of shell it is run? I need to write a script that executes a certain command if the shell is, say, bash. It should look roughly like this:
Code:
#!/bin/sh
if [ /* shell is bash */ ]; then
    alias ch='source ${COMMAND_FOR_BASH};
else
    /* do something else */
fi
I need to fill in the part where the /* shell is bash */ comment is now. Would anyone be able to help me out here?

TIA,
Dai
 
Old 03-22-2006, 03:44 PM   #2
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Original Poster
Rep: Reputation: 34
Figuring out which shell?

Hi.

Is there a shell script command that detects the type of shell it is run in? I need to write a script that executes a certain command if the shell is, say, bash. It should look roughly like this:
Code:
#!/bin/sh
if [ /* shell is bash */ ]; then
    alias ch='source ${COMMAND_FOR_BASH};
else
    /* do something else */
fi
I need to fill in the part where the /* shell is bash */ comment is now. Would anyone be able to help me out here?

TIA,
Dai
 
Old 03-22-2006, 03:48 PM   #3
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Original Poster
Rep: Reputation: 34
Oops, this is a duplicate. I messed up the "submit" button. I asked the mods to delete this thread. If you have any feedback, please post it in the other thread with the same subject. Sorry for the inconvenience.

Dai
 
Old 03-22-2006, 03:54 PM   #4
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
# Set a default shell prompt:
#PS1='`hostname`:`pwd`# '
if [ "$SHELL" = "/bin/pdksh" ]; then
PS1='! $ '
elif [ "$SHELL" = "/bin/ksh" ]; then
PS1='! ${PWD/#$HOME/~}$ '
elif [ "$SHELL" = "/bin/zsh" ]; then
PS1='%n@%m:%~%# '
elif [ "$SHELL" = "/bin/ash" ]; then
PS1='$ '
else
PS1='\u@\h:\w\$ '


This is from the /etc/profile script on Slackware, try this and see if it works
 
Old 03-22-2006, 04:35 PM   #5
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Original Poster
Rep: Reputation: 34
Thanks, J! I took your advice and wrote it this way:
Code:
tmpshell=`echo $SHELL | awk -F/ '{ print $NF }'`
if [ "$tmpshell" == "bash" ]; then
    alias ch='source ${COMMAND_FOR_BASH}'
else
    /* do something else */
fi
Works great for me. Again, thanks for your quick response and useful information.

This must run on various UNIX/Linux flavours, such as Solaris, HP-UX, AIX, QNX-Neutrino, and RHEL. I hope I can safely assume that any such system has "awk" installed.

Last edited by daihard; 03-22-2006 at 04:36 PM.
 
Old 03-22-2006, 04:45 PM   #6
__J
Senior Member
 
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973

Rep: Reputation: 46
Not sure if it will run on the other systems, slack only runs on a few architectures. Hopefully it works for you.
 
Old 03-22-2006, 05:13 PM   #7
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by __J
Not sure if it will run on the other systems, slack only runs on a few architectures. Hopefully it works for you.
I tried it on all our UNIX systems, and it seems to work. Thanks again!
 
Old 03-22-2006, 08:25 PM   #8
mikshaw
LQ Addict
 
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320

Rep: Reputation: 45
Not that it's seriously important, but you're running echo, awk, and if, when it can all be done inside a single case statement.

case $SHELL in
*bash) alias ch='source ${COMMAND_FOR_BASH}' ;;
*) whatever else ;;
esac
 
Old 03-22-2006, 08:47 PM   #9
daihard
Member
 
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915

Original Poster
Rep: Reputation: 34
Quote:
Originally Posted by mikshaw
Not that it's seriously important, but you're running echo, awk, and if, when it can all be done inside a single case statement.

case $SHELL in
*bash) alias ch='source ${COMMAND_FOR_BASH}' ;;
*) whatever else ;;
esac
That is super cool! Thanks a lot for the advice. I learn something new every day here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help figuring out ESR Problem sboney Linux - Newbie 1 07-28-2004 04:53 PM
Having problems figuring out these commands Dandy Linux - Newbie 2 04-18-2004 10:21 PM
Figuring out download speed chrisk5527 Linux - General 2 01-28-2004 07:35 AM
figuring out what ports are open joesbox Linux - Security 7 03-18-2003 04:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration