LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-09-2008, 07:09 AM   #1
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Rep: Reputation: 15
linux illiterate needs help making a script


ok the following is what i have posted on the sabnzbd forum but they dont have as many helpful and newb friendly people over there so im posting it here as well
Quote:
k heres the ultermint plan;

tv episodes converted for my archos upon download! no more queing up on my main pc to converrt cross network

ok first i did ask about this ages ago but at that time i had a differnet plan of attack and the pc was used for more then sabnzbd

i have spent the last two days trying to get a command line conversion tool to do what i do in lathe and finaly im close (just need to tweak the sttings but thats not what i need help with)

ok i can think of two approaches but have ZERO skill at making scripts hell less then zero so i really need help with this (being dyslexic dont help either lol)

aproach one

1) the post process script finds the largest .avi file in the download folder and marks that to a varrible name, lets say $file for ease and the folder name as $folder
2) the post process script hands that varrible to another script (so to not bog down sabnzb)
3) the other script executes the following command "mencoder /$folder/$file -ovc xvid -oac mp3lame -xvidencopts bitrate=748:aspect=16/9 -lameopts br=80 -vf scale=424:234 -o /home/mrgreaper/archos/$file_DONE.avi"

the pros of that is i will proberly understand it easier once some one explains the how lol and its simple (looking atleast) the disadvantage is i could end up with a few mencoders running at the same time (not sure if that would crash the pc)

aproach two

1) the post process script finds the largest .avi file in the download folder and marks that to a varrible name, lets say $file for ease and the folder name as $folder
2) /$folder/$file is added to the botom of a list
3) once an hour mencoder checks to see if its in the processes list(does linux have one?) if it is then it does nothing if it isnt it moves to 4
4) mencoder starts converting using the first filename and location on the list once done it deletes the first name from the list and does the second
5) repeats till the list is empty at which point it will likely error and close so the next hour mencoder launches it will see no mencoder and do any new ones on the list

i prefer aproach two but anything that will work would be good, i know im asking a lot but i just not linux savy enough to do it myself and trying to figure it out makes my head hurt (like i say it took twodays just to get the command line converter to work woth me putting the command in (and for that i had a lot of help)

my linux distro is ubuntu 8.04 (hardy?) my sabnzbd+ is the latest final build 0.4.4 and any help is greatly appreciated
what i have done to find a solution so far;
i know that sabnzbd passes the directory name to a script as $1 so that is the folder i need to search though for the largest avi

ls -lR | sort +4n seems to be the key i have found it on several sites telling you how to find the biggest file on your hard drive (im also guessing the R means recursive so it would end up searching all folders but thats where my knowledge and google start to fail (googles is only as effective as the brain using it )

i did also think of another aproach, instead of writting them to a text file as in aproach 2 maybe it would be easier to write them to a database (mysql or something) again lots of help available to install mysql not a lot in how to use it like i want to, damn i wish i was better at this

ok while writing this i recieved this code
Code:
files=`ls -S "$1"`
file=`echo $files | cut -d' ' -f1`
fileLC=`echo $file | tr 'A-Z' 'a-z'`
cd $1
from there i could just add mencoder /$1/$file -ovc xvid -oac mp3lame -xvidencopts bitrate=748:aspect=16/9 -lameopts br=80 -vf scale=424:234 -o /home/mrgreaper/archos/$file_DONE.avi" and i guess aproach 1 would be done (if its right, it looks 90% confusing to me)

taking that script i did a search on a few of the commands and found this
echo '$1$file' >> /home/mrgreaper/conv.text
would append the file name and directory to a text file, now how im stuck again how do i read it out to mencoder a line at a time deleting the line afterwards?
 
Old 11-09-2008, 09:05 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Here's some help I can offer...

Better way to get largest avi file in the directory:
Code:
file="$(ls -1S *.avi | head -n 1)"
Yeah, to add to a list you use the append operator '>>'.

To get running processes run 'ps'.

To get the first name in the list do:
Code:
head -n 1 list.txt
To delete the first line, maybe something like:
Code:
sed '1d' list.txt > temp.txt
mv temp.txt list.txt
Oh, and to check if 'FILE exists and has a size greater than zero' do this:
Code:
test -s list.txt
you can use this in a statement:
Code:
# while the list is not empty
while ! test -s list.txt
do
  #stuff
done
Some resources:
http://tldp.org/LDP/abs/html/
http://www.grymoire.com/Unix/

Last edited by H_TeXMeX_H; 11-09-2008 at 09:11 AM.
 
Old 11-09-2008, 03:26 PM   #3
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
i tried file="$(ls -1S *.avi | head -n 1)" | echo '$file' >> /home/mrgreaper/conv.text in a test folder the conv.text

conv.txt contains one word $file ...... doh! what did i do wrong?

i did this from terminal could that be why (wanted to test the command before commiting it to a script
 
Old 11-09-2008, 05:29 PM   #4
martina
LQ Newbie
 
Registered: Nov 2008
Distribution: SLES
Posts: 2

Rep: Reputation: 0
the character | "pipes" stdout.
there is no stdout output in file="$(ls -1S *.avi | head -n 1)"
You either want to do
file="$(ls -1S *.avi | head -n 1)" ; echo '$file' >> /home/mrgreaper/conv.text
or
ls -1S *.avi | head -n 1 >> /home/mrgreaper/conv.text

$(someting) is the same as `something` . the $(something) notation is newer (and can be nested)

hth
 
Old 11-09-2008, 05:38 PM   #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
Also don't use single quotes if you want to echo the value of the variable. The following will echo the value of the variable "file"
Code:
echo $file
echo ${file}
echo "$file"
whereas using single quotes you prevent the shell to substitute the $file notation with the actual value of the variable. Single quotes just preserve the literal string, resulting in the word $file printed out.
 
Old 11-10-2008, 04:28 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
You know, you really cannot make a good script without knowing a bit about bash scripting, so try to read some stuff about it. I posted some resources earlier, but you can also look here for a shorter introduction:
http://rute.2038bug.com/node10.html.gz
 
Old 11-10-2008, 08:20 AM   #7
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
ok heres my script thus far
Code:
dir=$1
cd "$dir"
file="$(ls -1S *.avi | head -n 1)" ; echo $file >> /home/mrgreaper/conv.text

mencoder "$file" -ovc xvid -oac mp3lame -xvidencopts bitrate=748:aspect=16/9 -lameopts br=80 -vf scale=424:234 -o /home/mrgreaper/archos/"$file"_DONE.avi
the first echo is just for testing but also for archiving what i have (not my original intention but it works quite well)

so far so good the files are being converted and there converts droped into the right folder (i may ned to play around with bitrates etc but im happy

you guys are amazing, no way could i of done this myself, every time i look at the resources posted or the help files for commands i go cross eyed i simply cant get my head around them

right two stages left one i think i have down (though needs refining the other i have no idea)

Moving completed files and folders

im thinking mv "$dir""$file" \home\mrgreaper\done\"$file" have i got that right?
(this would leave a lot of emty folders though so maybe if i could remove the folder afterwards? would rm "$dir" do that ?)

the next step might be better in a seperate script but easier for me to tag on the end

i want to delete anything in the mrgreaper/done/ folder thats over 14days old
im thinking
cd /home/mrgreaper/done/
old = ls -c (or -t?) SOMETHING date-14 (http://www.computerhope.com/unix/uls.htm just confused me more im certain its gonna be ls something but no idea what)
rm "$old"



again all help appreciated ( i know i could just experiment butwith linuz and my level of incompetence i could end up doing real damage specialy when were talking about deleting files !)


to H_TeXMeX_H
reading it now and my god i actualy understand some of it, m ay not help in what im trying to do now but certainly interesting

Last edited by mrgreaper; 11-10-2008 at 08:23 AM.
 
Old 11-10-2008, 02:00 PM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Quote:
Originally Posted by mrgreaper View Post
Moving completed files and folders

im thinking mv "$dir""$file" \home\mrgreaper\done\"$file" have i got that right?
(this would leave a lot of emty folders though so maybe if i could remove the folder afterwards? would rm "$dir" do that ?)
yes that would work, just make sure the output has the correct number of slashes, especially no slashes at all, that would be bad, more slashes are usually ignored

if they are only empty directories I would use 'rmdir' instead of 'rm' because it is safer, it's always good to be safer when deleting things

Quote:
the next step might be better in a seperate script but easier for me to tag on the end

i want to delete anything in the mrgreaper/done/ folder thats over 14days old
im thinking
cd /home/mrgreaper/done/
old = ls -c (or -t?) SOMETHING date-14 (http://www.computerhope.com/unix/uls.htm just confused me more im certain its gonna be ls something but no idea what)
rm "$old"
Hmmm, realize that this is a rather dangerous thing to do, so be very careful ! I'll try to explain what is happening.

I cannot stress enough how dangerous this command is, it will delete everything in that directory that is more than 14 days old, don't run it on something important.

Code:
find /home/mrgreaper/done -mtime +14 -print0 | xargs -0 rm -f
This is saying find files and directories in /home/mrgreaper/done with a modify time of more than 14 days old print them using null terminators, pipe to xargs which accepts null terminators and passes them to rm -f which forcefully deletes them. Hopefully that makes sense, I've read the command through option by option to make it more clear what is being done. I take no responsibility if this command screws your system, caveat emptor ...
 
Old 11-12-2008, 08:59 AM   #9
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by H_TeXMeX_H View Post
yes that would work, just make sure the output has the correct number of slashes, especially no slashes at all, that would be bad, more slashes are usually ignored

if they are only empty directories I would use 'rmdir' instead of 'rm' because it is safer, it's always good to be safer when deleting things



Hmmm, realize that this is a rather dangerous thing to do, so be very careful ! I'll try to explain what is happening.

I cannot stress enough how dangerous this command is, it will delete everything in that directory that is more than 14 days old, don't run it on something important.



Code:
find /home/mrgreaper/done -mtime +14 -print0 | xargs -0 rm -f
This is saying find files and directories in /home/mrgreaper/done with a modify time of more than 14 days old print them using null terminators, pipe to xargs which accepts null terminators and passes them to rm -f which forcefully deletes them. Hopefully that makes sense, I've read the command through option by option to make it more clear what is being done. I take no responsibility if this command screws your system, caveat emptor ...

sorry it took so long to say thanx just had some real life issues that interfered with my linux experimenting

ok heres the new script;
Code:
dir=$1
cd "$dir"
file="$(ls -1S *.avi | head -n 1)" ; echo $file >> /home/mrgreaper/conv.text

mencoder "$file" -ovc xvid -oac mp3lame -xvidencopts bitrate=600:aspect=16/9 -lameopts abr:br=92 -vf scale=424:234 -o /home/mrgreaper/archos/"$file"_DONE.avi

mv "$file" \home\mrgreaper\done\"$file"

rmdir "$dir"


find /home/mrgreaper/done -mtime +14 -print0 | xargs -0 rm -f
i figure since its already in the folder then mv "$file" was safer then mv "$dir""$file" and tidyier

i also changed the bitrate of the video (im only watching on a small screen so dvd quality is not needed and the audio, apears that you need to add abr: before the br= command, wierd but that explains why the audio was full bitrate

its in testing now will edit when its done

and thankyou for the help its majorly apreciated


*******edit i got this erro******

/home/mrgreaper/SABnzbd/script/conv2: 7: Syntax error: Unterminated quoted string

the file was converted fine
the converted file was placed in done folder correctly

the original file was NOT moved to the correct directory
the folder was not deleted
nothing at all in done yet so no idea if that bit worked

my guess is it failed here ;
mv "$file" \home\mrgreaper\done\"$file"

so i have changed it to
mv "$dir""$file" \home\mrgreaper\done\"$file"
i cant see why it would need the$dir as its already in $dir but then i have no idea how linux works stuff out lol

will do another test at 1500 i cant till then
******end of edit*****

Last edited by mrgreaper; 11-12-2008 at 09:37 AM.
 
Old 11-12-2008, 09:38 AM   #10
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
No problem, remember to read those links if you want to learn more. It's actually quite useful to know some bash scripting, it will help greatly in automation.
 
Old 11-12-2008, 10:45 AM   #11
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
hmm same error
/home/mrgreaper/SABnzbd/script/conv2: 7: Syntax error: Unterminated quoted string
Code:
dir=$1
cd "$dir"
file="$(ls -1S *.avi | head -n 1)" ; echo $file >> /home/mrgreaper/conv.text

mencoder "$file" -ovc xvid -oac mp3lame -xvidencopts bitrate=600:aspect=16/9 -lameopts abr:br=92 -vf scale=424:234 -o /home/mrgreaper/archos/"$file"_DONE.avi

mv "$dir""$file" \home\mrgreaper\done\"$file"

rmdir "$dir"


find /home/mrgreaper/done -mtime +14 -print0 | xargs -0 rm -f
i canr see any "" with out the last " which is how i interpret the error any ideas? its got to be a problem with the ..... hmmm just released while typing what it may be

i used \ instead of / on the mv line, i really dobt that would make a differnce i mean its no worse then using lowercase instead of uprecase a "A" is the same as a "a" a \ is the same as a / but have changed it to be safe and will try it out in a bit (the converting before the rest of the script takes a while)

if its not the / \ thing what else could it be? have i got the syntax right on the mv command?
 
Old 11-12-2008, 12:23 PM   #12
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
I recommend a syntax highlighting text editor or IDE to do this, here's the corrected code:

Code:
dir=$1
cd "$dir"
file="$(ls -1S *.avi | head -n 1)" ; echo $file >> /home/mrgreaper/conv.text

mencoder "$file" -ovc xvid -oac mp3lame -xvidencopts bitrate=600:aspect=16/9 -lameopts abr:br=92 -vf scale=424:234 -o /home/mrgreaper/archos/"$file"_DONE.avi

mv "$dir""$file" "\home\mrgreaper\done\"$file"

rmdir "$dir"


find /home/mrgreaper/done -mtime +14 -print0 | xargs -0 rm -f
 
Old 11-12-2008, 09:44 PM   #13
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
the following code worked to a point,
Code:
dir=$1
cd "$dir"
file="$(ls -1S *.avi | head -n 1)" ; echo $file >> /home/mrgreaper/conv.text

mencoder "$file" -ovc xvid -oac mp3lame -xvidencopts bitrate=600:aspect=16/9 -lameopts abr:br=92 -vf scale=424:234 -o /home/mrgreaper/archos/"$file"_DONE.avi

mv "$dir"/"$file" /home/mrgreaper/done/"$file"

rm "$dir"


find /home/mrgreaper/done -mtime +14 -print0 | xargs -0 rm -f
the last bit thats not working is the rm "$dir" command see below

also i had to put the / instead of \ and it needed one between "$dir" and "$file"

i did try the rmdir "$dir" but i get a folder not empty error (all downloads come with a few junk files)

the rm "$dir" tells me rm: cannot remove `/home/mrgreaper/downloads/complete/TV/test`: Is a directory im like well duh (stupid linux lol)

after a lot of googling i have changed that command to
rmdir --ignore-fail-on-non-empty "$dir"

fingers crossed that will fix it lol
 
Old 11-13-2008, 03:37 AM   #14
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
For non-empty directory you'll probably need either that ignore option for 'rmdir' or just plain:

Code:
rm -rf "$dir"
This recursively removes all files in the directory as well as the directory without prompting you. Be careful not to use it on an important directory or it will screw your computer.
 
Old 11-13-2008, 11:18 AM   #15
mrgreaper
Member
 
Registered: May 2006
Posts: 167

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by H_TeXMeX_H View Post
For non-empty directory you'll probably need either that ignore option for 'rmdir' or just plain:

Code:
rm -rf "$dir"
This recursively removes all files in the directory as well as the directory without prompting you. Be careful not to use it on an important directory or it will screw your computer.
rmdir --ignore-fail-on-non-empty "$dir" worked fine but thankyou for the alternative
i think the script is done i just need to tweak settings (im planning to go trough the mencoder help novel all volumes to find how to increase the outputs volume etc etc

Again thankyou for the help it is majorly appreciated, over at the sabnzbd forums i got zero help
 
  


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
linux illiterate to the xtreme SiinsoftheFuture Slackware - Installation 5 04-13-2007 07:47 PM
Linux-illiterate user needs help with his wireless card. bighops2000 Linux - Newbie 4 01-31-2006 12:30 PM
Script making in Linux fiending Linux - General 4 12-10-2003 03:07 PM
linux illiterate (win & RH7.3 problem) spaztic_fury Linux - Networking 1 10-28-2003 11:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:24 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