这是我的代码:
#!/bin/bash -e
dirs=( * )
for f in "${dirs[@]}"
do
while IFS= read -r line; do
case "$line" in
*disabled\>true* )
sed -i '1i "$f"' list.txt;;
esac
done < "$f/config.xml"
done
我也尝试了echo和printf而不是sed,但是文件list.txt始终为空。为什么我无法附加到文件?
echo "$f" >> list.txt;;
printf '%s\n' "$f" >> list.txt;;
测试文件夹下的示例config.xml文件:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<disabled>true</disabled>
</project>
目标:如果其中包含test / config.xml,则将“ test”打印到list.txt<disabled>true
中。
首先,请注意您的sed表达式不适用于空文件。我修改了代码,它满足了您的目标:
#!/bin/bash -e
for f in */config.xml # process all config.xml files
do
while IFS= read -r line; do
case "$line" in
*disabled\>true* )
# obtain directory name of the file
# and append it to list.txt
dirname "$f" >> list.txt
esac
done < "$f"
done
但是,我宁愿使用以下方法:
#!/bin/bash -e
for f in */config.xml
do
# -m1 - exit at first occurence for performance
if grep -m1 'disabled>true' "$f" >/dev/null; then
dirname "$f" >> list.txt
fi
done
甚至更简单:
grep -l 'disabled>true' */config.xml | cut -d/ -f1 > list.txt
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句