LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2005, 05:06 PM   #1
jayeola
Member
 
Registered: Dec 2005
Location: london, uk
Distribution: BLAG GNU/Linux
Posts: 58

Rep: Reputation: 15
the `source` command and my $PATH


Say I've got a script called blah.sh. Blah.sh has a line:-
source foo.sh && foo.sh
where foo.sh is another script. The whole idea is to have small re-usable scripts and foo.sh is one of them. When I run blah.sh as user root, foo.sh is sourced and run but the shell tells me
./blah.sh: line 23: foo.sh: command not found
even though foo.sh has been run.

Is this reflected the $PATH of the regular user and that of root? (I'm sure that it's related to my $PATH, but I'm not sure how to rectify this - yet)
 
Old 12-14-2005, 05:27 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
i think it probably is. the problem comes from the fact that './' is not the $PATH variable by default. my guess is that if you change the line to

source foo.sh && foo.sh

it will work. this is of course assuming the script is executable.

chmod +x foo.sh

try that.
 
Old 12-14-2005, 05:34 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
sirclif:

I think you mean:
Code:
source foo.sh && ./foo.sh
but the point is moot. Sourcing the file or simply executing it do exactly the same thing. You are running your script twice. Why?
 
Old 12-14-2005, 05:42 PM   #4
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
haha, yes, that is what i meant.

however, they do not do exactly the same thing. running the script directly, like ./foo.sh, will run it in a subshell. That is probably a detail that doens't make much difference, it never has in anything ive done, but i think it can if the environment variables are important in your script. i was wondering the same thing myself about running it twice...
 
Old 12-14-2005, 05:50 PM   #5
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
however, they do not do exactly the same thing. running the script directly, like ./foo.sh, will run it in a subshell. That is probably a detail that doens't make much difference, it never has in anything ive done, but i think it can if the environment variables are important in your script. i was wondering the same thing myself about running it twice...
Your right, it does make shell variables active in the current scope, however, it still doesn't make sense to me in the OP's context. If you have sourced the script, then any executable code has been run. If the executable code depends on the shell variables declared, just declare them before the commands, and it just needs to be sourced, right? Why the source then the strait execution?

Maybe I just don't get it...
 
Old 12-14-2005, 06:33 PM   #6
jayeola
Member
 
Registered: Dec 2005
Location: london, uk
Distribution: BLAG GNU/Linux
Posts: 58

Original Poster
Rep: Reputation: 15
I had no idea that `source blah.sh` and `blah.sh` did the same thing. I thought that source blah would (kind of), load a script anywhere in my $PATH and then I'd have to run it with the name of the script....

edited to add:- thanks guys. I chaged the line to `source blah.sh`, the script was executed with no complaints and everything's lovely

Last edited by jayeola; 12-14-2005 at 06:40 PM.
 
Old 12-14-2005, 10:45 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, 'source' or short form '. ' ie dot-space includes the contents of the named file directly into the calling file ie it's at the same shell level.
Using ./file.sh will create a sub-shell, so only exported vars from the parent are visible, and any vars created in the subshell cease to exist as soon as it terminates, so you can't use them in the parent shell.
HTH
 
  


Reply



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
Is there a way to change source install path on RHEL? guest Linux - Newbie 1 04-11-2005 05:01 PM
Finding My --kernel-source-path CAL Linux - Newbie 6 04-14-2004 07:57 AM
What is the path to Linux Kernel source beginner16 Linux - Newbie 4 11-24-2003 04:21 AM
Is there a good uninstall system when you install from source? My PATH is getting bad JoeLinux Linux - Software 3 05-08-2002 01:49 AM
Linux Kernel Source Path smeckel Linux - Newbie 1 01-08-2002 05:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:39 PM.

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
Open Source Consulting | Domain Registration