无法使用sed命令读取文件内容

时间:2019-11-14 15:51:28

标签: unix sed

我正在尝试逐行读取以下文件以执行以下操作

  1. 仅提取文件/目录的名称并为其分配一个变量,
  2. 提取该行中可用的权限,并在该权限之间添加逗号。然后将其分配给另一个变量,
  3. 最后应用setfacl逻辑,如输出部分所示。

File

# file:  /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x
# file:  /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file:  /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x

Code

input="bkp_23.txt"
while IFS= read -r line;
do
echo $line
file_name=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{print $1}'`
echo $file_name
file_perm=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{$1=""}{print}' | tr ' ' ',' | awk 
'{sub(",","")}1'`
echo $file_perm
echo "setfacl -m "$file_perm" "$file_name" executing"
done <"$input"

Output

setfacl -m user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x  /disk1/script_1/
setfacl -m  user::rw-,group::r--,other::r--  /disk1/script_1//hello.txt
setfacl -m  user::rwx,group::r-x,other::r-x   /disk1/script_1//bkp_10.txt

Error

sed: can't read # file: /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x other::r-x: No such file or directory

2 个答案:

答案 0 :(得分:0)

$ cat input
# file:  /disk1/script_1/ user::rwx group::r-x group:service:r-x mask::r-x     other::r-x
# file:  /disk1/script_1//hello.txt user::rw- group::r-- other::r--
# file:  /disk1/script_1//bkp_10.txt user::rwx group::r-x other::r-x
$ while read  _ _ path perms; do perms="$(echo "$perms" | tr -s ' ' ,)"; echo path="$path", perms="$perms"; done < input
path=/disk1/script_1/, perms=user::rwx,group::r-x,group:service:r-x,mask::r-x,other::r-x
path=/disk1/script_1//hello.txt, perms=user::rw-,group::r--,other::r--
path=/disk1/script_1//bkp_10.txt, perms=user::rwx,group::r-x,other::r-x

答案 1 :(得分:-1)

尝试像这样的sed逻辑一起回显行内容 file_name=$(echo "$line" | sed 's/# file:\(.*\)/\1/g' | awk '{print $1}')

file_perm=$(echo "$line" | sed -e 's/# file:\(.*\)/\1/g' | awk '{$1=""}{print}' | tr ' ' ',' | awk '{sub(",","")}1')