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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-22-2006, 03:43 PM
|
#1
|
Member
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915
Rep:
|
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
|
|
|
03-22-2006, 03:44 PM
|
#2
|
Member
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915
Original Poster
Rep:
|
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
|
|
|
03-22-2006, 03:48 PM
|
#3
|
Member
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915
Original Poster
Rep:
|
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
|
|
|
03-22-2006, 03:54 PM
|
#4
|
Senior Member
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973
Rep:
|
# 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
|
|
|
03-22-2006, 04:35 PM
|
#5
|
Member
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915
Original Poster
Rep:
|
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.
|
|
|
03-22-2006, 04:45 PM
|
#6
|
Senior Member
Registered: Dec 2004
Distribution: Slackware, ROCK
Posts: 1,973
Rep:
|
Not sure if it will run on the other systems, slack only runs on a few architectures. Hopefully it works for you.
|
|
|
03-22-2006, 05:13 PM
|
#7
|
Member
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915
Original Poster
Rep:
|
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!
|
|
|
03-22-2006, 08:25 PM
|
#8
|
LQ Addict
Registered: Dec 2003
Location: Maine, USA
Distribution: Slackware/SuSE/DSL
Posts: 1,320
Rep:
|
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
|
|
|
03-22-2006, 08:47 PM
|
#9
|
Member
Registered: Jul 2003
Location: Seattle, WA
Distribution: Kubuntu 14.04 LTS
Posts: 915
Original Poster
Rep:
|
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. 
|
|
|
All times are GMT -5. The time now is 04:17 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|