简体   繁体   English

使用Shell脚本读取配置文件

[英]config file reading using shell script

I have a config file which contains path to a some files . 我有一个配置文件,其中包含一些文件的路径。

path.conf file path.conf文件

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

path=/home/test/ 路径= /家庭/测试/

.... .... .... ....

path=/home/work_group/Documents/offdoc/ 路径= / 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. 有人可以帮助我编写一个脚本,该脚本采用配置文件中提到的所有文件路径并将其权限设置为0750。

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. $()在子shell中运行awk并输出文件名。 xargs gathers them into one line, so that the for loop can iterate over them. xargs将它们收集到一行中,以便for循环可以遍历它们。

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. 更新:Tripleee指出了您的尝试中的错误,您应该听从他的建议。 Honestly I just read the first line, decided it made no sense, and wrote this answer instead. 老实说,我只是读了第一行,认为没有意义,而是写了这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM