Register
It is currently Wed Apr 16, 2014 3:52 pm

Find directory above file and also some decimal problem


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue Nov 30, 2010 3:50 pm   

Joined: Tue Nov 30, 2010 3:26 pm
Posts: 2
Hi, first post here, I hope I put it in the right place.

I'm a beginner with bash so you'll probably laugh at these questions, but I need a few tips. I'm writing a script to sort my downloaded videos to the right folder based on length and also based on the UL/DL ratio. I've commented the script so it should be clear what I'm trying to do. My problem is it's working great for all files that are only files, but sometimes a torrent comes as a directory and then I cant find the name of that dir, only the filename inside. I need this to find it in transmission list. How can I get the name of the dir that contains the file?

Second question is I have some trouble with comparing decimal numbers in UL/DL ratio. As of now it rounds to whole numbers so I have to go with RATIO=2. Id like to be able to put a decimal number and I think I'm supposed to do so with bc but I'm not sure of usage. How can I compare these decimals?

Script:
Code:
#!/bin/bash

DLPATH=/home/scarleo/Videos/Download
LOGPATH=/var/log/mediasort
MEDIAPATH=/home/scarleo/Videos
RLIMIT=2

# Check all files in Download directory
FILMER=(`find $DLPATH -name *.[avimk]* `)                # ls -AR $DLPATH/*.[avimk]*`)
echo ${FILMER[@]}

# Check number of seconds for each file
for (( i=0; i<${#FILMER[@]}; i++ ));
do
LEN=`mplayer -vo null -ao null -frames 0 -identify "${FILMER[i]}" 2>/dev/null | \
  grep "^ID_LENGTH" | sed -e 's/ID_LENGTH=//g'`

# Convert frames to h, m, s
INT=$(echo "$LEN" / 1 | bc)
HR=$(echo "$INT" / 3600 | bc)
MIN=$(($(($INT - $HR * 3600)) / 60 | bc ))
SEC=$(($INT % 60))
echo $INT

# Check UL/DL-ratio
FILMNAMN=$(`echo basename ${FILMER[i]}`)
RATIO=(`transmission-remote -n user:password -l | grep "$FILMNAMN"`)
# echo "${RATIO[@]}"

# Film or serie? Just for testing, will be removed
if [ $INT -ge 4200 ] ; then echo "Film"
else echo "Serie"
fi

# If > 4200 sec it's a film, otherwise it's a serie. Also if Ratio >= RLIMIT we move the file to the right directory
# ...and remove the torrent from transmission
INTRATIO=`echo ${RATIO[7]} / 1 | bc`
echo $INTRATIO | bc
if [ $INTRATIO -ge $RLIMIT -a $INT -ge 4200 ] ; then mv ${FILMER[i]} $MEDIAPATH/Filmer ; echo "Will move"
elif [ $INTRATIO -ge $RLIMIT -a $INT -lt 4200 ] ; then mv ${FILMER[i]} $MEDIAPATH/Serier ; transmission-remote -n user:password -t ${RATIO[0]} -r ; echo "Will move"
else echo "Won't move"
fi
echo Ratio: ${RATIO[7]}
echo `basename ${FILMER[i]}`
echo Length: "$HR:$MIN:$SEC"
echo ; echo
done



P.S. I know this script probably contains a lot of weired constructions, but like I said I'm a beginner trying to learn by trial and error. If you have suggestions to improvements feel free to say so.


Top
 Profile  
 PostPosted: Tue Nov 30, 2010 3:59 pm   

Joined: Tue Nov 30, 2010 3:26 pm
Posts: 2
This part is also a bit strange:
Code:
# Convert frames to h, m, s
INT=$(echo "$LEN" / 1 | bc)
HR=$(echo "$INT" / 3600 | bc)
MIN=$(($(($INT - $HR * 3600)) / 60 | bc ))
SEC=$(($INT % 60))
echo $INT


Sometimes the file length comes out as 1234.12 and sometimes it gets decimals in the operations. I tried to pipe it through bc first but it wouldn't work without "/ 1 | bc". I'm sure there is a better way to do this.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 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:  


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