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.