0

I have a config file which contains path to a some files .

path.conf file

path=/home/work_group/Desktop/rt.txt

path=/home/test/

.... ....

path=/home/work_group/Documents/offdoc/

In a script i want to read all these file path and set permission to them. I tried the following code but it doesn't even print the path name.

#!/bin/bash 
while IFS= read -r line || [[ -n "$path" ]]; do
if [ -n "$path" ]
then
echo "Text read from file: $path"
chmod 0750 $path

fi
path=
done < admin.cfg

can somebody help me to write a script which takes all the file paths mentioned in the config file and set their permissions to 0750.

Adhz
  • 55
  • 7
  • Please indent the question properly. Also please see what makes a [\[ mcve \]](http://stackoverflow.com/help/mcve). – sjsam Jun 04 '16 at 11:29
  • Whatever you are trying to accomplish, **`chmod 777` is wrong** and a security problem. What makes you want to permit anyone with access to the system to write to these files? Do you understand the concept of [privilege escalation?](https://en.wikipedia.org/wiki/Privilege_escalation) – tripleee Jun 04 '16 at 11:53
  • You are reading data into `line` but then examine a different variable named `path`. Voting to close as trivial error. – tripleee Jun 04 '16 at 12:02
  • See also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable and the [Stack Overflow `bash` tag wiki](/tags/bash/info) for a list of common newbie pitfalls. – tripleee Jun 04 '16 at 12:05

1 Answers1

1

Here is a quick one-liner that will get the job done for you:

for i in $(awk -F'=' '{print $2}' path.conf | xargs); do chmod 0750 $i; done

The $() runs awk in a subshell and outputs the filenames. xargs gathers them into one line, so that the for loop can iterate over them.

It should be easy to turn this into a script yourself.

Update: Tripleee has pointed out the error in your attempt, you should take his advice. Honestly I just read the first line, decided it made no sense, and wrote this answer instead.

Niall Cosgrove
  • 1,273
  • 1
  • 15
  • 24