When you want to explain something in the script, or stop a command from executing, put a # in front of it, it disables all text after that line that line specifically.
# Commands
Basically it's just commands you would enter normally in the terminal and write them down in the script.
Sab serves some things to a script so you can use them to execute the commands with, listed here: http://wiki.sabnzbd.org/user-scripts. In Bash you can use these by naming them $1 to $7. Copying a downloaded folder ($1) is easy that way, open a textfile, shebang at top, enter cp $1 /new/location. Save it make it executable, and there you have it, your first postprocessingscript.
# Variables
As above Sabnzbd executes a script and gives it 7 variables ($1 to $7). You can create your own variables too, or rename the variables Sabnzbd passes to the script. Variables are made by word=something (= is used to define a variable) and then callable by using a $ sign for it, like $word.
Some examples:
name=mars ($name) >> output your name in terminal or log >usage: echo "Hi my name is $name"
location=/some/folder/on/my/nas ($location) >> copy a file to that folder on nas > usage: cp $1/*.avi $location
cleannzb=$3 ($cleannzb) >> output that name in terminal or log > usage: echo "$name copied $1 to $location, job $cleannzb is done"
Variables can be a command to, use the command inside $(). actioncopy=$(cp *.avi /home/user/Videos) $actioncopy will execute it.
Using variables makes a script flexible and editable, change a variable and everywhere it's used it will be changed too. Usually they are written before all functions.
# Functions
Using code from someone else is also easy. Just use functions. You can call them whatever you like and write 'm like this:
Code: Select all
nameoffunction () {
commands you want to execute
}
Now you want to combine two scripts that cleans up your downloadfolder and then copy's it to a specific directory and then tells (echo) you you are a great guy and then start a script someone else wrote. Search this forum for other scripts that do that, for example [url=http://"http://forums.sabnzbd.org/index.php?topic=5031.0"]this[/url] and [url=http://"http://forums.sabnzbd.org/index.php?topic=4748.0"]this[/url] one. Embed them in functions and the new script will be:
Code: Select all
!/bin/bash
#### clean up directory so execute cleanupscript
topic1 () {
GARBAGE=".nfo .srr .sfv .nzb .jpg" #### Add or remove extensions here
for junk in $GARBAGE
do
find $1 -name *$junk -type f -exec rm -f {} \;
done
}
#### the above function does the same as the more readable:
removefiles () {
rm -f $1/*.nfo
rm -f $1/*.srr
rm -f $1/*.sfv
rm -f $1/*.nzb
rm -f $1/*.jpg
}
#### copy to someplace if movies and another if tvshows copied from forum and embedded in function
topic2 () {
if [ "$5" = "movies" ]; then
mv -fv "$1" "/Volumes/External HD/Media/Movies"
elif [ "$5" = "tv" ]; then
mv -fv "$1" "/Volumes/External HD/Media/TV"
fi
}
#### Yes I wrote my own bashscript
tellme () {
echo "I love Linux and Sabnzbd!"
}
#### execute all the above function, except the first
#topic1 #because this one is commented it won't execute.
removefiles # you can test one function of your script by commenting all the other functions and then run the script.
topic2
tellme
# More difficult functions
Bash has many ways to check for conditions. Those are called statements. For example if-statements are most used in script.
If statements condition like "if this then do x else do y fi". A simple example:
Code: Select all
if $name=mars
then
echo "hi $name" #this can be a command too, or multiple commands like this
rm -f /home/mars/*.rar
echo "Oh noes, I accidentally 98mb of rar-files, is that bad?
else
echo "hi i don't know your name"
fi
# Running multiple scripts from 1 script
This is also easy. You can do 2 things. First the easiest:
1. Blank textfile, shebang @ top and the following lines in the script:
./path/toscript/one/scriptname1
./path/toscript/two/scriptname2 &&
./and/so/on.
2. Copy all text in all scripts, put the code from script one in function1, script two in function 2 etc. (watch out with variables here by the way).
If you put && after an executescriptcommand the next command will only be started if the previous one was succesful (see example scriptname2).
# Great sources for bashwriting are:
http://mywiki.wooledge.org/EnglishFrontPage
http://www.faqs.org/docs/Linux-HOWTO/Ba ... HOWTO.html
and google of course.
The basic thing is:
If you repeatedly do the same stuff on your linux-pc, over and over again, then write a bashscript for it. Throw all commands you use in a file, shebang at top and functions at bottom and chmod +x it. It will save you a lot of time (after you invest some first) and you will learn a lot about bash and linux. You can make Sabnzbd do fun stuff and make nice logs.
for example a script with:
echo "Hi $name, I did this job called $3 for you, as you feeded me $2, I moved it to $1 for you, hope you will enjoy this $5."
(You can log this into a real file with > /home/user/filename.txt at the end).
Have fun scripting! ;)
Forgot one thing:
It's always a good idea to use #/bin/bash -x as a shebang when writing scripts from scratch. This will enter debugmode and the output in terminal will be much more verbose, so it will be easier to find mistakes.