首页 > 解决方案 > 将 xml 文件从文件夹移动到另一个文件夹取决于 bash shell 中的条件

问题描述

我只需要根据 xml 标签注释将 xml 文件从文件夹移动到另一个文件夹,例如:

If :
1- comment tag is  Student1 will be move to folder Student_1
2- comment tag is Student2 will be move to folder Student_2

xml:

<?xml version = "1.0" encoding = "UTF-8" ?>
<!--Student1 grades are uploaded by months-->
<class_list>
   <student>
      <name>Tanmay</name>
      <grade>A</grade>
   </student>
</class_list>

任何建议,

先感谢您

标签: bashshellunix

解决方案


Grep有一个有用的退出状态,所以应该这样做。

#!/usr/bin/env bash

if grep -q '^<!--Student1.*-->$' file.xml; then
  echo cp file.xml Student_1
elif grep -q '^<!--Student2.*-->$' file.xml; then
  echo cp file.xml Student_2
fi

echo如果您对输出感到满意,请删除's,以便 cp 可以实际复制文件。


推荐阅读