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.

4
  • Please indent the question properly. Also please see what makes a [ mcve ]. Commented Jun 4, 2016 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? Commented Jun 4, 2016 at 11:53
  • You are reading data into line but then examine a different variable named path. Voting to close as trivial error. Commented Jun 4, 2016 at 12:02
  • See also stackoverflow.com/questions/10067266/… and the Stack Overflow bash tag wiki for a list of common newbie pitfalls. Commented Jun 4, 2016 at 12:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.