Register
It is currently Sat Apr 19, 2014 10:31 pm

Renaming Script?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu May 23, 2013 3:24 pm   

Joined: Thu May 23, 2013 3:21 pm
Posts: 4
Is there a way to create a bash script that would go into a zip file, into a .xml file inside the zip file, find a string of text between a xml tag, copy it into memory, and rename the file with that sting of text minus the xml tag?


Top
 Profile  
 PostPosted: Thu May 23, 2013 4:12 pm   

Joined: Thu May 23, 2013 3:21 pm
Posts: 4
All I need to know is how to look inside zip files, I can get the string of text with xml_grep and pipe over to mv, but I need to get inside the zip.


Top
 Profile  
 PostPosted: Thu May 23, 2013 10:50 pm   

Joined: Thu May 23, 2013 3:21 pm
Posts: 4
Ok, I figured part of it out. But I'm having trouble with an if statement.

Here's the code
Code:
#!/bin/sh
mkdir "tmp"
for i in *.epub; do
if [ -e "$i" ]; then
unzip "$i" -d "tmp/$i"
for a in "tmp/$i/*.opf"; do
if [ -e "$a" ];
then
name=`xml_grep 'dc:title' "tmp/$i/content.opf" --text_only`
author=`xml_grep 'dc:creator' "tmp/$i/content.opf" --text_only`
cp "$i" "$name - $author.epub"
rm -r "tmp/$i/"
else
name1=`xml_grep 'dc:title' "tmp/$i/*/content.opf" --text_only`
author1=`xml_grep 'dc:creator' "tmp/$i/*/content.opf" --text_only`
cp "$i" "$name1 - $author1.epub"
rm -r "$i"/
fi
done
fi
done


The problem is right after the "else". It's not finding the content.opf. I think it's because of the *, but how could I fix this? The file it's in may change.


Top
 Profile  
 PostPosted: Fri May 24, 2013 2:42 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

Code:
for a in "tmp/$i/*.opf"
unquote the star
Code:
for a in "tmp/$i/"*.opf
otherwise it wouldn't expand.

imho, you should use /tmp directory instead of creating one in the current directory.


Top
 Profile  
 PostPosted: Fri May 24, 2013 8:27 pm   

Joined: Thu May 23, 2013 3:21 pm
Posts: 4
Thanks, for replying, I used a tmp directory in the current directory so that way I didn't mess up the other things in /tmp when I was creating the code.

But, I figured out a way to get the script to work.

Here's the code if anyone is interested.
Code:
#!/bin/sh
#needs xml_grep from the package xml-twig-tools
mkdir "tmp"                              # creates temporary workspace
  for i in *.epub; do                     #checks that there are epub books in directory
    if [ -e "$i" ]; then
      unzip "$i" -d "tmp/$i"               #unzips ebooks and gets information for new name
         a=`find "tmp/$i/" -name *.opf`
          if [ -e "$a" ]; then
         name=`xml_grep 'dc:title' "$a" --text_only`
         author=`xml_grep 'dc:creator' "$a" --text_only`
          r=`find "tmp/$i/META-INF/" -name "rights.xml"`            #checks to see if ebook is ecrypted and decrypts the ebook
            if [ -e "$r" ]
            then
            for key in ~/bin/*.b64; do
            python ~/bin/ignobleepub.pyw "$key" "$i" "$name - $author.epub"
            rm "$i"
             done
            else
             mv "$i" "$name - $author.epub"                     #renames epub
            fi
        fi
      chmod a+rw "tmp/$i/"                        #cleans up workspace
      rm -rf "tmp/$i/"      
   fi                     
done
rmdir "tmp/"


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP