sed命令在循环中发出

我正在尝试逐行阅读以下文件以进行以下列出的操作

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

文件-bkp.txt

# 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

我的代码

#!/bin/sh
input="bkp.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"

必需的输出

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,other::r-x   /disk1/script_1//bkp_10.txt

但以以下错误结尾

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
cwal10 回答:sed命令在循环中发出

dependencies { implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion" implementation "com.android.support.constraint:constraint-layout:$rootProject.ext.constraintLayoutVersion" implementation "com.android.support:design:$rootProject.ext.supportLibraryVersion" implementation "com.android.support:recyclerview-v7:$rootProject.ext.supportLibraryVersion" implementation "com.android.support:support-core-utils:$rootProject.ext.supportLibraryVersion" implementation "com.google.android.gms:play-services-vision:18.0.0" implementation "pub.devrel:easypermissions:$rootProject.ext.easypermissionsVersion" androidTestImplementation("com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion",{ exclude group: "com.android.support",module: "support-annotations" }) // Dependencies for local unit tests testImplementation "junit:junit:$rootProject.ext.junitVersion" testImplementation "org.mockito:mockito-all:$rootProject.ext.mockitoVersion" testImplementation "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion" testImplementation "org.powermock:powermock-module-junit4:$rootProject.ext.powerMockito" testImplementation "org.powermock:powermock-api-mockito:$rootProject.ext.powerMockito" testImplementation "junit:junit:$rootProject.ext.junitVersion" } 程序可在文件上运行(在stdin上)。您不能在命令行上将字符串传递给它。替换如下行:

sed

使用

file_name=`sed -e 's/# file:\(.*\)/\1/g' "$line" | awk '{print $1}'`

看起来输入是空格分隔的。您还可以将sed和awk合并为单个“ awk”

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

或更改read语句(无需覆盖IFS)直接读取文件和ACL。

file_name=$(echo "$line" | awk '{print $2}')
本文链接:https://www.f2er.com/3109587.html

大家都在问