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.
|
 |
05-15-2009, 09:44 PM
|
#1
|
LQ Newbie
Registered: May 2009
Posts: 12
Rep: 
|
script: Am I running in bash or zsh?
I've googled for answers, and I've also searched through this forum and I don't know the right search phrase to use for my question. I've also checked the zsh manual for a parameter which might give a hint, but no luck so far.
I have a script which I want to be able to run under many different scripting environments. I want it to be able to adapt appropriately to use the builtins for its shell.
So, for example there are some differences between bash and zsh
Code:
time=`ls -gG --full-time "$1" 2> /dev/null`
zsh:
Code:
echo "${time[12,30]}"
bash:
Code:
echo "${time:15:29}"
or
zsh:
bash:
So for my script to do the right thing, without making any assumptions about its environment, it needs to learn how it's been run.
I don't want to assume anything based on the names of the processes on the system, for example by using `kill` to determine if a process exists.
I don't want to rely on $SHELL, since if I start off in zsh and then I manually run `bash` .. $SHELL remains pointed at zsh! Maybe bash doesn't use $SHELL?
Is there some way to do this?
Last edited by spiralofhope; 05-19-2009 at 07:40 PM.
|
|
|
05-16-2009, 06:24 AM
|
#2
|
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039
Rep:
|
How about :
EDIT:
or:
Code:
shell=`ps -o cmd --no-heading $$|cut -d' ' -f1`
echo $shell
Last edited by Disillusionist; 05-16-2009 at 08:08 AM.
|
|
|
05-16-2009, 04:37 PM
|
#3
|
Member
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137
Rep:
|
What about programing using POSIX? Any shell should understand POSIX. You can try POSIX compatibility with dash. At least, that is what I do…
The opposite solution if you insist on non-POSIX is to set the shebang right: if you want your script to run on bash if you want zsh .
Last edited by synss; 05-16-2009 at 04:41 PM.
Reason: added second paragraph
|
|
|
05-16-2009, 07:04 PM
|
#4
|
Moderator
Registered: May 2001
Posts: 29,417
|
That 'ps' output only works correct if 'ps' is not an alias. Running 'readlink -f /proc/$$/exe' OTOH should work correctly. Builtins could be another way: of the shells I tested (Ash, Bash, Bsh, Jsh, Ksh, Tcsh) only Bash has internal "help" (as in something like 'help >/dev/null 2>&1 || echo "not bash?').
|
|
|
05-19-2009, 07:56 PM
|
#5
|
LQ Newbie
Registered: May 2009
Posts: 12
Original Poster
Rep: 
|
Quote:
Originally Posted by Disillusionist
How about :
EDIT:
or:
Code:
shell=`ps -o cmd --no-heading $$|cut -d' ' -f1`
echo $shell
|
Aha, I knew there was a way.
Quote:
Originally Posted by unSpawn
That 'ps' output only works correct if 'ps' is not an alias.
|
I could run ps as "ps" or \ps to ensure that I bypass an alias/procedure.
Code:
echo `"ps" -o cmd --no-heading $$|"cut" -d' ' -f1`
Quote:
Originally Posted by unSpawn
Running 'readlink -f /proc/$$/exe' OTOH should work correctly.
|
Good one. I could do it like this:
Code:
"basename" `"readlink" -f /proc/$$/exe`
Or like this:
Code:
MYSHELL="$("basename" $("readlink" -f /proc/$$/exe))"
Quote:
Originally Posted by unSpawn
Builtins could be another way: of the shells I tested (Ash, Bash, Bsh, Jsh, Ksh, Tcsh) only Bash has internal "help" (as in something like 'help >/dev/null 2>&1 || echo "not bash?').
|
Code:
help >/dev/null 2>&1 || echo "not bash?"
That's pretty sneaky! It's a great way to avoid using external programs. I don't think I'll go for this because it's a sortof hackish undocumented thing to do, and it might not work in the future or for all shells.
Quote:
Originally Posted by synss
What about programing using POSIX?
|
While that's an interesting problem to solve, it's not important for me right now. I may need to rework a lot of the way I think in order to avoid the shell-specific stuff I've been doing, so it'll be something for me to work on later.
Quote:
Originally Posted by synss
The opposite solution if you insist on non-POSIX is to set the shebang right: if you want your script to run on bash if you want zsh
|
I cannot make an assumption as to what's available on their system, so this won't work for me.
Last edited by spiralofhope; 05-19-2009 at 08:33 PM.
|
|
|
06-14-2009, 04:54 PM
|
#6
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
While I suspect that unSpawn's
Code:
readlink -f /proc/$$/exe
is the most reliable,
will work in bash; I'm curious to know what others it also works in.
|
|
|
06-14-2009, 11:36 PM
|
#7
|
LQ Newbie
Registered: May 2009
Posts: 12
Original Poster
Rep: 
|
Quote:
Originally Posted by archtoad6
While I suspect that unSpawn's
Code:
readlink -f /proc/$$/exe
is the most reliable,
will work in bash; I'm curious to know what others it also works in.
|
That works in zsh too.
|
|
|
All times are GMT -5. The time now is 09:27 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
|
|