LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-11-2015, 06:47 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
need bash script that modifies files relative to its location safely


I'm working on a script (to be executed via bash) which must set a variety of file permissions and ownership. Because this script may change file ownership, it must have elevated permissions.

I'd like for the script to modify the permissions of files using pathnames relative to its location on the file system. For example if permissions.sh is in scripts and target-files contains the files to be modified:
Code:
/var/
  www/
    scripts/
      - permissions.sh
    target-files/
      target-dir-1/
      - target1.php
      - target2.php
I don't want to have to refer to the full path names of all these files because they may be in /var/www on one machine and /var/www/project-x on some dev's workstation.

I was thinking I could use relative path names (e.g., ../target-files/targe1.php) but I expect this would be evaluated relative to the cwd of the executor rather than the location of permissions.sh.

I unfortunately have little bash experience setting variables and string operations so I don't know how to capture the directory of permissions.sh from within that script and then concatenate filenames, etc.

Any help would be much appreciated.
 
Old 03-11-2015, 06:52 PM   #2
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Quote:
I expect this would be evaluated relative to the cwd of the executor rather than the location of permissions.sh.
In the script, change the directory to the location of the script file. Then write your commmands relative to that point.
 
1 members found this post helpful.
Old 03-13-2015, 04:07 PM   #3
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Thanks for your response.

Having looked at your link and searched a bit, I've concocted a brief bash script, test.sh, that will cd to the parent directory of the directory where the script is located:
Code:
#!/bin/bash
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd "$DIR/.."
pwd
I expect that I could then simply enter my chown/chmod commands with relative pathnames and it would probably work. However, I'm wondering what effect that cd command might have. Suppose I run this test.sh script and then run subsequent scripts or commands from some other script, other.sh ?
Code:
# contents of /home/sneakyimp/other.sh
cd /some/other/dir
/home/sneakyimp/script1.sh
/home/sneakyimp/test.sh # this has the cd command in it
/home/sneakyimp/script2.sh
# etc
I've tested this on my workstation (Ubuntu 12.x) and it appears that the cd commands only last for the duration of the current script and do not affect the cwd in other.sh or in script1.sh and script2.sh. Can I reasonably expect this to be the case on all *nix platforms? E.g., CentOS?
 
Old 03-13-2015, 07:13 PM   #4
Miati
Member
 
Registered: Dec 2014
Distribution: Linux Mint 17.*
Posts: 326

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by sneakyimp View Post
I'm wondering what effect that cd command might have.
directory changes... stay with the current shell and are inherited by child or sub shells.

Let's say I make a script with the following to demonstrate:

Code:
echo $PWD
cd ~/
echo $PWD
(
cd ..
echo $PWD
)
echo $PWD
Assuming when I run the script that my $PWD is /,
I would expect this output:

Code:
/
/home/miati
/home
/home/miati
Which ends up being correct.
Examine that and why each one is as it is. Specifically, why the last one != the second to last one (remember, going into a child shell will cause the child shell to inherit the current directory but discard any changes once it exits)
A really easy way to test this is by typing this in the terminal:
Code:
(cd /; ls)
Your current directory doesn't change, yet you know it did change directory in the subshell since it listed /'s contents.

Quote:
Can I reasonably expect this to be the case on all *nix platforms?
You are most likely using bash, so this is bash behavior. If other *nix platforms use something besides bash, this behavior could change.
However you could easily load up a bash shell (type in bash in the shell)
OR - type #!/bin/bash
This indicates to the script reader that it should be interpreting it as bash script (my understanding of this is less clear.)

Quote:
cd commands only last for the duration of the current script and do not affect the cwd in other..
Since starting a script opens a child shell, this is following that behavior.

Last edited by Miati; 03-13-2015 at 07:21 PM.
 
1 members found this post helpful.
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to delete and store location of full path gasric Linux - Newbie 7 10-24-2013 09:11 AM
how do i write a bash script to restore deleted files to original location? BrinkofMadness Linux - Newbie 8 12-05-2010 06:04 PM
[SOLVED] Shell script that modifies/touches files in an unappreciated manner ... dr_strangelove Linux - Newbie 12 06-02-2010 06:25 AM
adding a network location to a bash script command yandos Linux - Newbie 1 03-15-2009 06:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:11 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
Open Source Consulting | Domain Registration