It is possible to pass arguments to a bash script when it is called from the command line. This is the technique to use when you need to have your script carry out different actions each time it runs dependent on the input and the context. This is done by passing selected parameters to the file on the command line and these parameters are called arguments.
Lets look at an example, you may have a script called "graph.sh" that performs a particular operation on an RRD file, such as extracting the data. If you want to be able to use that script on many RRD files in many different user directories, it is best to pass the file path as an argument, so that you can use the same script for all the files to be processed.
For instance, if the username to be graphed is "ASmith", you would enter the following command line:
sh graph.sh ASmith
Any arguments passed to the file are accessed internally within the script by using variables $1, $2, etc. This denotes that $1 references the first argument, $2 the second, and so on. Lets illustrate this in an example:
in order to ensure readability, assign your variables with descriptive names and then call the graphing utility (rrdgraph) on this variable ($ASmith).
If the number of arguments is likely to change then you can use the "$@" variable, which creates an array of all the input parameters. This technique enables the use of a for-loop to iteratively process each one, as illustrated in the following example:
for $user in "$@"
Here is an example of how to call this script with arguments from the command line:
sh graph.sh user1 user2 user3
What if your arguments have spaces?
If any of your arguments have spaces, you need to enclose the full argument in single quotes.
Let say you have a script that pulls information from your database using specific parameters, such as "uname", "todays date", and "description", and then produces a report in an "output format" of the users choice. Now you want to write your script so that you can pass in these parameters when the script is called. It might look like this:
extractreport -u jsmith -d notebooks -td 10-20-2011 -of pdf
Bash enables this functionality with the "getopts" function. For the above example, you could use getopts as follows:
while getopts u:d:td:of: option
This is a while-loop that uses the "getopts" function and a so-called "optstring", in this case "u:d:p:f:", to iterate through the arguments. The while-loop walks through the optstring, which contains the flags that can be used to pass arguments, and assigns the argument value provided for that flag to the variable "option". The case-statement then assigns the value of the variable "option" to a global variable that can used after all the arguments have been read.
The colons in the optstring mean that values are required for the corresponding flags. In the above example all flags are followed by a colon: "u:d:p:f:".
sh stats.sh 'songlist 1' 'songlist 2' 'songlist 3'
Frequently a script is written such that the user can pass in arguments in any order using flags. With the flags method, you can also make some of the arguments optional.
This means, all flags need a value. If, for example, the "d" and "f" flags were not expected to have a value, the optstring would be "u:dp:f".
A colon at the beginning of the optstring, for example ":u:d:p:f:", has a completely different meaning. It allows you to handle flags that are not represented in the optstring. In that case the value of the "option" variable is set to "?" and the value of "OPTARG" is set to the unexpected flag. The allows you to display a suitable error message informing the user of the mistake.
Arguments that are not preceded by a flag are ignored by getopts. If flags specified in the optstring are not provided when the script is called, nothing happens, unless you specially handle this case in your code.
Any arguments not handled by getops can still be captured with the regular $1, $2, etc. variables.
As an ex military satellite communications engineer I certainly remember working with spread spectrum modulation and also frequency hopping technology in the 1980's. Wireless Local Area Networking technology today exploits a technology which was thitherto mostly hidden inside this shadowy domain of military communications and radar. This technology comprises a collection of ideas which are termed Spread Spectrum Techniques (SST). Spread Spectrum techniques have some powerful properties which make them an excellent candidate for networking applications. To better understand why, we will take a closer look at this fascinating area, and its implications for networking.
It was almost a whim which made us decide to make The Cobbler our first climb of the year. We'd left it late for sure. We're normally out there bashing about the hills throughout the winter but this year was different because of the work we had been doing in the house and my own multiple commitments elsewhere.
I'd spent the previous two weeks feeling pretty poor with a mixture of back pain and virus but the weather gods were smiling today. There was nothing stopping us. Had they not been, weaker motivation may have crumbled but even today with the weakest motivation and the briefest preparation, there was only one outcome.
So off we went. When we arrived we jumped out the car and were so eager to get on with it we even forgot the fruit wed bought specifically for the climb. If you know the path up The Cobbler, you'll know that the first section is a pretty tough, winding slog up through the forests which cover the steep lower slopes of the hill and border Loch Long. It was during this section that I discovered that, despite my porridge breakfast and my sirloin steak/fish finger dinner the night before, I had little to no energy in my body. It was all I could do to place one leg in front of the other for a while but I took frequent rests and somehow managed to slog on. I was gutted. It was a lovely day and Brian (and I) had never seen the view from the top of this mountain without it being obscured by clouds. Today was to be that day, but how. I was exhausted and didn't feel like I had anything in reserve. I promised Id make it to the reservoir by hook or by crook and despite plenty of puffing and wheezing we got there and let the dogs play in the water.
At this point, after a bit of a break, I felt a little better and said I'd like to carry on to the shelter stones and decide there whether or not I could carry on. Once there we made up some energy drink from a sachet and I gulped it down. Again the break made me feel like I could go on just a little bit more so I committed to make it up to the point where the path splits 3 ways to head off for Beinn Narnain, Ben Ime and The Cobbler. As I walked the sugars in my stomach seemed to energise me with every step. I was still hurting but it was becoming less of a problem.
When we got to the 3 way split we stopped again and had a good break. The final and steepest part of the climb loomed above me like a skyscraper but I'd decided if I could make it this far I could make it to the top. After popping 3 dextrose tablets we set off. The final part of the climb is like a long hellish staircase with stones of all sizes making up the crazy paving stairs. It was a tough stretch but, with the help of plenty of breaks and the knowledge we were nearly there, we made it. As you can see from the picture at the top, the views were well worth the slog. Spectacular!
After a linger at the windy top to take in the majesty, it was time to head back down the path to the delights of the chip shop and the chance to undo some of the healthy goodness we'd subjected ourselves to. Well, life is all about striking a happy balance. What a great day!
Having recently finished reading Aldous Huxley's great prophetic masterpiece Brave New World, I thought I’d blog a little about the book and how it left me feeling. This book is no stranger to analysis being, as it is, a course book in many academic settings. That it is so, is no surprise. The book is one of the earliest mainstream examples of dystopian literature. First published in 1932 at the height of the great depression, the novel was written in Huxleys house in France. It was his fifth novel and first dystopian work. Huxley himself regarded the novel as a parody of the utopian novels of HG Wells which were extremely popular at the time, referring to it as a negative utopia.
The first thing which struck me about the book were the strong Shakespearian references throughout.
There are perceived allusions to both Hamlet and The Tempest among others however the most obvious nods are to these two plays. The Tempest has been often described as an allegory of imperialism since Prospero, the rightful Duke Of Milan decides to impose his values on the monstrous Caliban. The World State can be seen as the great civilising power sanctioning as it does, only English. The novel gives the impression that all other world cultures have been suppressed globally except for a few ‘reservations’. The character John further resemblees Caliban in that he becomes known as ‘The Savage” when he enters the mainstream world.
Furthermore, as the novel develops, the allusions to Hamlet become more apparent as John tries to balance his two weltanshauungen and their conflicting effects. He is an avid fan of Shakespeare and it seems Hamlet is his favourite play. In the novel there is no evidence of religion. The reader finds out that this is because God is incompatible with the automated mechanised society. The message is that society has eliminated suffering and therefore doesn't need God anymore.
It turns out that Huxleys Brave New World isn't just a portent about science, its also a warning about education and by extension, the media. The reality which becomes apparent is that it is possible to create any required reality in the minds of the citizens by constantly drip feeding them any truth as needed. In the novel, the mechanism used is called hypnopedia however the horrific parallels to modern life with its unabating indoctrinating media are clear. The warnings are stark. Question everything. Make up your own mind. Criticise. Doubt. Assume nothing. Every media agency in the world has its own agenda. Even the so called friendly ones. While were still in a position to seek out the truth its everybody responsibility to do so. Lets not mess things up.