LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2011, 07:12 AM   #1
Jamie.Aston
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Rep: Reputation: 0
Talking auto_extract.sh Bash Script Help


Hi Guys,

Am a noob here so apologies for diving straight in. Have worked with Linux at home for some time and have wrote the below script. This script is run when a .torrent file finishes downloading and extracts any .rar files into an extracted directory. However there are 2 thing that i cannot for the life of me get working. These are:

1. I need the script to check a txt file to see if the file has been extracted previously.
2. The script does not work with any paths that contain spaces.

Any and all help to resolve these issues or comments to improve any part of the script would be greatly appreciated.

Thanks
Jamie

Code:
#!/bin/bash

SOURCE="/share/transmission/complete/"
DESTINATION="/share/transmission/extracted/"
REMOVESOURCE="NO"

# Find all RARs in working directory.
for MYRAR in `find "$SOURCE" -wholename *.r01 -o -wholename *.part01.rar`
do
    #For Each RAR list files within
    for FILE in `unrar lb "$MYRAR"`
    do
        #Set Extract Flag to Y
        EXTRACTFLAG="Y"

        #Check if each file within RAR has been extracted adn set extractflag accordingly
        while read EXTRACTEDFILE   
        do
            if [ "$FILE" == "$EXTRACTEDFILE" ];
            then 
                EXTRACTFLAG="N"
                break
            fi
        done </share/transmission/extracted.txt 

        #Check If EXTRACTFLAG is set
        if [ $EXTRACTFLAG == "Y" ];
        then
            #Extract into destination directory.
            echo "Unpacking in directory: "`basename "$MYRAR"`
            #unrar e -inul "$MYRAR" -n "$FILE" "$DESTINATION"
            
            #Append filename to extracted.txt
            #echo "$FILE" >>/share/transmission/extracted.txt

            #Test if delete flag is set.
            if [ "$REMOVESOURCE" = "DELETE" ]; 
            then
                echo "Deleting directory: "`dirname $MYRAR`
                rm -r `dirname $MYRAR`
            fi
        else
	    echo "File has prevoiusly been extracted, skipping: "`basename "$MYRAR"`
        fi
    done 
done

exit
 
Old 01-25-2011, 07:25 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Regarding the spaces in the filenames you can set the IFS to "\n" (please read the bash reference manual about the IFS internal variable) OR use a while read construct. The read statement assigns the whole line to the specified variable, so that it is seen as a single string including blank spaces:
Code:
while read MYRAR
do
  commands here
done < <(find "$SOURCE" -name \*.r01 -o -name \*.part01.rar)
this uses process substitution to pass the output of the find command to the loop.
 
Old 01-25-2011, 07:27 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
I'm not sure about the "I need the script to check a txt file to see if the file has been extracted previously" part. What the problem really is? Can you provide an example?
 
Old 01-25-2011, 07:34 AM   #4
Jamie.Aston
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Cheers colucix for the reply, will read up on the IFS variable now. As for the script checking the txt file perhaps a bit of background will help. This script runs everytime a .torrent finishes so will try to extract all the .rar files found after each .torrent, i did write the script to check whether the file existed in it's destination but this meant i had to keep the file there after i've read the doc or watched the episode. By listing these files in a txt and checking this i can delete the extracted file after watching but keep the compressed version to seed. Does this make sense? Not sure it does to me LOL.

Cheers
 
Old 01-25-2011, 07:50 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Just to be sure: the while read loop is an alternative to the IFS setting. Change the IFS if you want to preserve the for loop, leave it alone if you want to try the while read loop.

Thank you for the explanation about the torrent files (actually I'm totally noob about torrent). Maybe you can avoid the
Code:
while read EXTRACTEDFILE   
do
loop. Once you have the FILE from the unrar list, you can simply do a grep to see if it already appears in the txt file, e.g.
Code:
for FILE in $(unrar lb "$MYRAR")
do
  if grep "$FILE" extracted.txt
  then
    echo already extracted
  else
    extract here
  fi
done
this should be more efficient. The same considerations about the filenames with spaces applies here, so that you might consider a while read loop instead. Hope this helps.
 
1 members found this post helpful.
Old 01-25-2011, 07:59 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
As an add on to excellent advice above, I would include the '-q' switch in the grep so you don't get the data flying up the screen when all you want is a yes or no that it is there
 
Old 01-25-2011, 08:03 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by grail View Post
As an add on to excellent advice above, I would include the '-q' switch in the grep so you don't get the data flying up the screen when all you want is a yes or no that it is there
Correct. I forgot to add the -q option. Thanks, grail!
 
Old 01-25-2011, 08:27 AM   #8
Jamie.Aston
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Have changed out my code for the grep code and works well but cannot get the while loop to work to allow for spaces :-(
 
Old 01-25-2011, 08:31 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Please, post the last version of your code. Which part does not work for blank spaces? That one reading the list of rar files from the find command or the files coming from the unrar lb command?
 
Old 01-25-2011, 08:35 AM   #10
Jamie.Aston
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Got it working now, set IFS=$'\012' (octal value for new line) and working Perfectly. Thanks for all your helps guys.
 
Old 01-25-2011, 08:51 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
Old 01-25-2011, 09:41 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,550

Rep: Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898Reputation: 2898
I would caution playing with IFS too much and also remember that once set it will affect the rest of the loops you create as well that may
not wish to split on a newline. The while option does not suffer this side affect as it is contained within the loop.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
[SOLVED] Setting function default in a bash script - Bash sixtensixone Linux - General 13 01-08-2011 02:44 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration