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 09-10-2015, 03:19 PM   #1
rlsj
LQ Newbie
 
Registered: Aug 2015
Posts: 18

Rep: Reputation: Disabled
cd Preservation?


I hope this isn't too simple a question.

How do you issue, in a script,

cd <path>/somedirectory

and have somedirectory continue as the working directory when you exit from the script?
 
Old 09-10-2015, 03:45 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 10,981

Rep: Reputation: Disabled
Just source your script instead of executing it directly:
Code:
. <script>
or
Code:
source <script>

Last edited by Didier Spaier; 09-10-2015 at 06:40 PM. Reason: Corrected: "source <script>" instead of "source cd.sh"
 
Old 09-10-2015, 06:16 PM   #3
rlsj
LQ Newbie
 
Registered: Aug 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
(Sigh)

What does it mean, "to source my script?" I didn't know that "source" was a verb!

Let's say I have a script that contains,
cd ~/several/dirs/deep/petunia

The script is ~/bin/setpet and has the permissions 0755.
How can I arrange it such that issuing
setpet
while in the directory ~/notso/deep/do_it

returns, if I should subsequently issue
pwd
~/several/dirs/deep/petunia
?
 
Old 09-10-2015, 06:38 PM   #4
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 10,981

Rep: Reputation: Disabled
Type either (mind the dot followed by a space):
Code:
. setpet
or:
Code:
source setpet
PS the syntax with the dot "." works in any POSIX compliant shell. The syntax with "source" only in some shells (e.g. in bash).

PPS In that case setpet doesn't even need to be executable.

Last edited by Didier Spaier; 09-10-2015 at 06:52 PM. Reason: PPS added.
 
Old 09-10-2015, 06:40 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,999

Rep: Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683
Basically when you execute a script it runs in a separate shell with a separate environment. When the script finishes your back where you were when you started. To change the directory in the current environment you need to either source, use a function or via typed command. Sourcing a script is running those commands in the current shell.

http://www.linuxquestions.org/questi...script-926556/
 
Old 09-10-2015, 07:31 PM   #6
rlsj
LQ Newbie
 
Registered: Aug 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
michaelk wrote,
>Sourcing a script is running those commands in the current shell.

That's a head scratcher. All this time I've believed that _all_ commands are run in the current shell! Not so?

Hmm. Do you mean that by putting a period-space before the script name on the shell command line, the shell will run the script's commands without forking a different thread? That could be useful!
 
Old 09-10-2015, 07:47 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,999

Rep: Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683Reputation: 5683
Quote:
All this time I've believed that _all_ commands are run in the current shell! Not so?
If you type in the command at the command prompt then yes. If you run a script that calls a command then no.
 
Old 09-10-2015, 08:36 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by michaelk View Post
If you type in the command at the command prompt then yes. If you run a script that calls a command then no.
Depends on the command. Things like "echo", then the answer is yes.

If the command is "ls" then no. The ls utility gets anew process.
If it is a shell script, then USUALLY no. Only if the shell script is invoked by ". scriptname" (or an alias equivalent) will it be run in the same context.
 
Old 09-11-2015, 04:09 PM   #9
rlsj
LQ Newbie
 
Registered: Aug 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
"dot-space" does indeed let cd's in scripts carry over to the command line! Thank you very much.

A suggestion: How about the shell maintainers devising an environment variable that sets either way in stone -- along with an "undot-space" capability?

Thanks again!
 
Old 09-14-2015, 01:32 AM   #10
iHateErrors
Member
 
Registered: Sep 2015
Posts: 32

Rep: Reputation: Disabled
hi wasup
 
Old 09-14-2015, 03:24 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by rlsj View Post
"dot-space" does indeed let cd's in scripts carry over to the command line! Thank you very much.

A suggestion: How about the shell maintainers devising an environment variable that sets either way in stone -- along with an "undot-space" capability?

Thanks again!
Not possible. This isn't Windows (or VMS where it held logical names in a different name space).
 
Old 09-14-2015, 11:12 AM   #12
rlsj
LQ Newbie
 
Registered: Aug 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
To my suggestion the shell allow an environment variable that made all scripts run in the command line context, jpollard replied, "Not possible. This isn't Windows ..."

Just the fact that "this isn't Windows" is enough to make me reject a claim of impossibility. Call it the "SOURCE" variable and let the shell check whether it's enabled before executing the script. Should have the same result as invoking the script with dot-space.
 
Old 09-14-2015, 11:50 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by rlsj View Post
To my suggestion the shell allow an environment variable that made all scripts run in the command line context, jpollard replied, "Not possible. This isn't Windows ..."

Just the fact that "this isn't Windows" is enough to make me reject a claim of impossibility. Call it the "SOURCE" variable and let the shell check whether it's enabled before executing the script. Should have the same result as invoking the script with dot-space.
Not everything is a shell script. Not everything uses the SAME shell or has the SAME syntax.

An executable shell script gets exactly the same environment as every other process. Don't want it? use the ". xxx" syntax or "source xxx" (depending on which shell you use...).

The difference is "context". Running within a single process is possible for multiple shell scripts. Unfortunately that also means you have only ONE global variable storage.

It is not possible to do for other executables. Those get a new context. Even if you use the "exec" you don't get the same environment variable context. You also get logged out if any of the scripts cause an exit...

Now you COULD sort of emulate it by writing your own command interpreter... then using "utilities" that are created as shared libraries. Then the interpreter could load/execute those utilities by using "dlopen" and associated functions. But it is cumbersome, slow, and error prone (any error/segfault within a library could cause the termination of your interpreter), it is also artifically made harder to debug.

Last edited by jpollard; 09-14-2015 at 11:54 AM.
 
Old 09-14-2015, 03:57 PM   #14
rlsj
LQ Newbie
 
Registered: Aug 2015
Posts: 18

Original Poster
Rep: Reputation: Disabled
jpollard wrote,
"Not everything is a shell script..."

Agreed. But please keep in mind my original request.

If the bash shell, when executing my script, which issues only one cd:
cd <path>/xyz
causes <path>/xyz to be the current working directory for the commands I type after the script returns, then the suggested environment variable works as requested. How it affects other contexts, if it does, is another issue. Side effects are not specified.
 
Old 09-14-2015, 05:28 PM   #15
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Actually, the "side effects" ARE specified.

ANY change in the context will affect the caller - and there is no way to undo it. Environment variables - path (which you usually don't want to change), working directory (which you do want to change), parameters, any variables used, traps,

What you end up with is one big context - and without a way to debug it.

The simple way this is protected is by processes. A process inherets a context, but does not export it. Thus when the process terminates, the parent process still has its context.

And without unexpected side effects. Any exit/abort - only terminates the process. It doesn't affect the parent.

The only element of the process returned to the parent is the exit status (which is how it detects that the process had an error). Without that - if the script exits... you are logged out.

That is why the "source" syntax works the way it does. It is very useful for initializing a context. But it is very poor at being an execution unit of its own.

For a rather complicated situation, consider the firefox utility. The /usr/bin/firefox command is actually a shell script. How would you like being logged out just because you closed the window? How would you like to try and work with such a script?

Just working with the bashrc initialization is tricky. Do it wrong and things like scp will not work. You can also be blocked from login.

That is why it is a case by case operation. Doing a "source" on every script would leave you with a nonfunctional environment.

Last edited by jpollard; 09-14-2015 at 05:37 PM.
 
1 members found this post helpful.
  


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
Format Preservation in Variable Declaration gmconklin Programming 4 05-10-2013 04:09 PM
Making mirrow using mkraid with preservation information on one of the disks. Shakin_Aleksey Linux - Server 1 07-24-2011 11:21 PM
Searching for an appropriate Interprocess communication with data preservation Esika Programming 2 06-06-2011 06:07 AM
Debian / Ubuntu : Config File Preservation and Merging? tinyTux Debian 2 03-22-2011 12:19 PM
AIX 4.3.3 Preservation Install ajoshay9 Linux - Software 0 12-02-2003 12:35 AM

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

All times are GMT -5. The time now is 03:42 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