这是我的代码:
#!/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
我没有使用sed,而是尝试了echo和printf,但文件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中有< disabled> true,则将“test”打印到list.txt中.
解决方法:
首先,请注意您的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] 举报,一经查实,本站将立刻删除。