LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   910607 - working directory changes in shell (https://www.linuxquestions.org/questions/linux-newbie-8/910607-working-directory-changes-in-shell-4175424407/)

hamidi2 08-28-2012 09:12 AM

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

yucefrizk 08-28-2012 10:18 AM

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

chrism01 08-28-2012 09:00 PM

Option 3: edit Perl program to chdir() before it does anything
http://perldoc.perl.org/functions/chdir.html

hamidi2 08-28-2012 10:20 PM

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?

yucefrizk 08-29-2012 09:21 AM

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!

hamidi2 08-30-2012 01:37 AM

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.

yucefrizk 08-30-2012 03:00 AM

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")"


All times are GMT -5. The time now is 09:25 PM.