我有一个简单的bash脚本,该脚本接收带有主题文本的文件,并逐行处理。
如果该行以某些字符开头-请运行复合命令块。我正在尝试使用grep测试行的模式(但会接受其他建议)。
#!/usr/bin/env bash
for i in "$@"
do
if grep "^M" $i # I want grep to "assume" $i was a file
# and test if the pattern "^M" is present
then
echo "This line started with an M: "$i
# command 1
# command 2
# etc
fi
done
D bar
M shell_test.sh
M another_file
然后运行脚本
cat subject_text.txt | xargs --delimiter="\n" ./matcher.sh
如何让grep$i
将参数列表中的每次迭代都视为$i
文件?
您可以Subject_text.txt
循环读取文件,并提供matcher.sh
文件名以进行检查:
while IFS= read -r _ file_name
do
./matcher.sh "$file_name"
done < "Subject_text.txt"
但是,现在我知道了,您正在使用matcher.sh
每一行。请注意,以每行作为参数调用脚本有点过分。
如何正常循环文件并执行grep
?
#!/usr/bin/env bash
file=$1
while IFS= read -r line
do
if grep "^M" <<< "$line" # I want grep to "assume" $i was a file
# and test if the pattern "^M" is present
then
echo "This line started with an M: $i"
# command 1
# command 2
# etc
fi
done < "$file"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句