Ok, this is the quick version.....
- When you run a bash script anything you put after the script name get passed as "positional parameters".
- These start at 1 through whatever....
- So if you run the script /home/myscript.sh The Quick Brown Fox in your script the variable $1 will have the value The, $2 will be Quick, $3 = Brown, $4 = Fox
When we set up the incrontab line:
Code:
/home/incron/source IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/dest
we are telling incrond that when the line "fires" for a file to decode the mystical symbols $@ $# $% and then run the script
/home/incron/action.sh with them. So if you create a file
testfile1 incrond will decode the symbols as follows:
Code:
$@ /home/incron/source
$# testfile1
$% IN_CLOSE_WRITE
and then will run the script as though you'd typed this in the command line:
Code:
/home/incron/action.sh /home/incron/source testfile IN_CLOSE_WRITE /home/incron/dest
Which means that in the script the positional parameters will be:
Code:
$1 /home/incron/source
$2 testfile1
$3 IN_CLOSE_WRITE
$4 /home/incron/dest
Personally I hate having to remember what parameter is what in a script and also going back and looking at a line and trying to work out what $3 is way down a file, so it's
my personal scripting preference to declare and populate more meaningful variables from the positional ones at or near the start of my scripts.
As I previously explained, I like to make my scripts portable where possible, so by having incron decode $@ we are passing the source folder to the script and we're passing the destination folder too, so we could easily reuse the same script multiple times without having to change anything in it. For example:
Code:
/home/incron/source IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/dest
/home/incron/anothersource IN_DELETE,IN_CLOSE_WRITE /home/incron/action.sh $@ $# $% /home/incron/somewheredest
And this will work with them using exactly the same
action.sh because it was written to be reusable.
In reality,
if it was my system, I'd consider adding another parameter to the incrontab entries that would be $5 and used for THELOG so that I could have separate log files for each of the entries. I'd also alter the log file format a bit to have the entries on a single line with a timestamp.