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 05-15-2009, 09:44 PM   #1
spiralofhope
LQ Newbie
 
Registered: May 2009
Posts: 12

Rep: Reputation: Disabled
Question 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:
Code:
setopt xtrace
bash:
Code:
set -x

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.
 
Old 05-16-2009, 06:24 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
How about:
Code:
ps -fp $$
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.
 
Old 05-16-2009, 04:37 PM   #3
synss
Member
 
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Rep: Reputation: 22
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
Code:
#!/bin/bash
if you want zsh
Code:
#!/bin/zsh
.

Last edited by synss; 05-16-2009 at 04:41 PM. Reason: added second paragraph
 
Old 05-16-2009, 07:04 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,406
Blog Entries: 55

Rep: Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578Reputation: 3578
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?').
 
Old 05-19-2009, 07:56 PM   #5
spiralofhope
LQ Newbie
 
Registered: May 2009
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Disillusionist View Post
How about:
Code:
ps -fp $$
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 View Post
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 View Post
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 View Post
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 View Post
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 View Post
The opposite solution if you insist on non-POSIX is to set the shebang right: if you want your script to run on bash
Code:
#!/bin/bash
if you want zsh
Code:
#!/bin/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.
 
Old 06-14-2009, 04:54 PM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
While I suspect that unSpawn's
Code:
readlink -f /proc/$$/exe
is the most reliable,
Code:
echo $SHELL
will work in bash; I'm curious to know what others it also works in.
 
Old 06-14-2009, 11:36 PM   #7
spiralofhope
LQ Newbie
 
Registered: May 2009
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by archtoad6 View Post
While I suspect that unSpawn's
Code:
readlink -f /proc/$$/exe
is the most reliable,
Code:
echo $SHELL
will work in bash; I'm curious to know what others it also works in.
That works in zsh too.
 
  


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
LQ member's opinions about Zsh compared to Bash-Differences? linus72 Linux - Newbie 1 05-07-2009 08:09 PM
Running a bash script in PHP Nerdstock2005 Programming 14 12-09-2008 03:28 PM
running su from a bash script caminoix Programming 7 12-28-2005 03:41 PM
Bash or Zsh reddazz Linux - General 5 05-27-2005 08:16 AM
zsh equivalents in bash saurya_s Linux - General 1 01-04-2004 03:06 AM

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

All times are GMT -5. The time now is 02:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration