0

I need to restore rrd files from my exisiting xml files. So I have used following simple bash script.

#!/bin/bash

for i in /home/dump_xml/*.xml;
do
rrdtool restore $i /home/rrd_new/"${i%.xml}".rrd;
done

I could not execute following script due to this error,

ERROR: Could not create xml reader for: /home/dump_xml/*.xml

But I could restore files one by one. Can someone help me to solve this?

Shehan
  • 417
  • 2
  • 11
  • It means that there are no files with name ending in `.rrd` inside `~/dump_xml`. But even if they are, your _rrdtool_ command looks odd: It would expand into something like `rrdtool restora /home/dump_xml/foo.rrd /home/rrd_new/home/dump_xml/foo.rrd.xml`, which is perhaps not what you want. – user1934428 May 20 '22 at 09:49
  • @user1934428 I am really sorry I have updated the question. – Shehan May 20 '22 at 09:52

1 Answers1

1

When you use:

for i in /home/dump_xml/*.xml
do
    echo "$i"
done

You will see that $i equals:

  • /home/dump_xml/a.xml
  • /home/dump_xml/b.xml

You see, it contains the path. Therefore your rddtool will try to write the results in /home/dump_xml/home/dump_xml/a.rrd.

You have to do:

#!/bin/bash

for i in xml/*.xml
do
    filename=$(basename "$i")
    rrdtool restore "$i" /home/rrd_new/"${filename%.xml}".rrd;
done

Do not forget to double-quote your variable extensions.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
  • I would do a `basename "$i" .xml`. so that `filename` already doesn't have an extension. You then don't have to remove the extension afterwards. – user1934428 May 21 '22 at 16:18
  • ah ah, very nice, I did not know about the extension part of `basename`, thanks! – Nic3500 May 21 '22 at 21:39