LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-28-2012, 10:12 AM   #1
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Rep: Reputation: Disabled
910607 - working directory changes in shell


since i'm familiar with Windows and today is my first try with Linux, let me describe how i may, based on words popular in Windows.
a perl script is located somewhere which is included in PATH. when i type the name of perl without its path it's run with working folder set to where the perl script is located. but when i put it in a batch file (shell file) the working directory changes to where the batch file is located.
how can i override this behavior so that the working folder in this case be changed to where the perl script is located?
thx
 
Old 08-28-2012, 11:18 AM   #2
yucefrizk
Member
 
Registered: Nov 2009
Posts: 44

Rep: Reputation: 18
add a change directory command to your shell script just before the line where you are calling your perl script, or inside your shell script, instead of calling the script using /path/to/the/script/script.pl or script.pl you can call the perl as below:

Code:
cd /path/to/the/script && ./script.pl

Last edited by yucefrizk; 08-28-2012 at 11:21 AM. Reason: /path/to/the/script inside PATH
 
1 members found this post helpful.
Old 08-28-2012, 10:00 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Option 3: edit Perl program to chdir() before it does anything
http://perldoc.perl.org/functions/chdir.html
 
Old 08-28-2012, 11:20 PM   #4
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
yes, ur right, but it depends me on where the script is located. i prefer a way in which i've not to restate the path.
and what causes the working directory for script when run from inside a batch file (what do u call it?) and outside of it at the command prompt differ? how can i make shell run both the same way? is it a built-in behavior and cannot be changed?
 
Old 08-29-2012, 10:21 AM   #5
yucefrizk
Member
 
Registered: Nov 2009
Posts: 44

Rep: Reputation: 18
The working directory of any script is the working directory of who's calling this script! so if you are calling your perl script from a SHELL SCRIPT (that's how we call it!) it will have the wd of this shell script, if you are calling it directly from the CLI it will have the wd of your shell, it's always the same behaviour!
 
Old 08-30-2012, 02:37 AM   #6
hamidi2
Member
 
Registered: Aug 2012
Posts: 61

Original Poster
Rep: Reputation: Disabled
hmm, i confused a bit. see whether i'm right:
when we login to Linux no script is run. just sh which is an executable file is executed which allows us to run scripts as well as other executable files. so when we call it directly from CLI, sh is the one who's calling the script. so the wd of the perl script in this case must be where sh is located. right?
(what's CLI? is it an abbreviation of Command Line Interface?)
and when i call the perl script from a shell script the working directory of the perl script will be where the shell script is located.

Last edited by hamidi2; 08-30-2012 at 02:40 AM.
 
Old 08-30-2012, 04:00 AM   #7
yucefrizk
Member
 
Registered: Nov 2009
Posts: 44

Rep: Reputation: 18
There's difference between working directory and path; when you login to Linux, sh (or bash or whatever other default shell you have in /etc/passwd) is executed and your working directory is the user home directory you are using to login (you can get the working directory by running the command `pwd` after logging in) and not the path of your default shell! So, before running a script execute `pwd` to know what working directory will it have.

Anyway, when you run a shell script, a sub-shell is running to execute your script and not the same shell you are using to login.

Solve this issue by adding to the script the below line, this will change dynamically to the directory of your script wherever the script is located:

Code:
cd "$(dirname "$0")"

Last edited by yucefrizk; 08-30-2012 at 04:02 AM.
 
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
910607 - how to bring up ethernet at startup? hamidi2 Linux - Newbie 2 08-28-2012 10:03 AM
get shell scripts own directory name mvairavan Linux - Newbie 6 03-25-2012 11:56 AM
how to change working directory of the current shell? kevin_chn Programming 6 05-16-2008 03:26 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM
Set working directory in shell paultaylor Programming 9 11-19-2004 02:35 AM


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