Linux - SoftwareThis 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.
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.
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.
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?').
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.