Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 08-12-2006, 03:42 AM   #1
Senior Member
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,544
Blog Entries: 4

Rep: Reputation: 56
zsh return 0 if script sourced not executed

I'm working on extending a set of shell scripts, one of the originals has a ton of functions that I'd like to use in others, however sourcing it executes it and things get all messy from there. I added this to hopefully stop it at a certain point if it's not directly executed:
if [ "$(basename $0)" != "makepkg" ]; then
	return 0
That works in bash where $0 is set to "bash" however the target shell is zsh which has $0 set to "makepkg".

Does anyone have any suggestions as to other ways I can get around this? I've checked the zshbuiltins manpage to see if there is any tricky ways around it but couldn't find anything useful and Google isn't much help as soon as the word "source" is included in the search :/

Any ideas would be greatly appreciated
Old 08-12-2006, 11:53 AM   #2
Senior Member
Registered: Nov 2005
Distribution: Debian
Posts: 2,817

Rep: Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999Reputation: 999
I think you want to set the option NO_FUNCTION_ARG_ZERO:

~/tmp% cat                                                            
#! /bin/zsh

echo "0 = $0"
~/tmp% source                                                         
0 =
~/tmp% ./                                                              
0 = ./
~/tmp% setopt NO_FUNCTION_ARG_ZERO                                             
~/tmp% source                                                         
0 = zsh
~/tmp% ./                                                              
0 = ./
But doesn't zsh have all that cool autoloading stuff you can use for defining functions?
Old 08-12-2006, 12:41 PM   #3
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
But doesn't zsh have all that cool autoloading stuff you can use for defining functions?
Indeed, but that's mainly intended for interactive use, rather than in scripts (though I suppose you can use it in a script as long as you run compinit etc).

however sourcing it executes it and things get all messy from there.
If you're sourcing files, it's usually better to put everything in functions, so the code is only executed when the function is called.

Could you reverse the logic? E.g.

if [[ ${0:t} == makepkg ]]; then
        ... do whatever
        ... do something else
Any good?

Note that you don't need to quote $variables in zsh (unless you set shwordsplit). Also, :t does the same job as basename without spawning another program.

Last edited by soggycornflake; 08-12-2006 at 12:49 PM.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DVD writes bad data to disk when growisofs is executed from script akiladila Linux - Hardware 4 04-17-2006 06:30 PM
hotplug script executed 3 times Borelian Linux - Software 1 06-29-2004 12:46 AM
No effect when 'export' command executed in a script? sylvain_gnu Linux - Software 6 04-20-2004 08:15 PM
Is it possible to return variables from a script? pedrosan Linux - Newbie 6 04-20-2004 06:51 AM
Simple script to be executed on startup. Korff Linux - General 2 06-03-2003 10:25 PM

All times are GMT -5. The time now is 04:15 PM.

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