When you break programming down, regardless what language or syntax it is written or developed in, the basis of a piece of code is how it handles a condition.
What happens when you have something? How do you handle it? What do you do with it?
The absolute basic conditional statement in shell scripting is the "if" statement. The basics of the if is that "if the conditions are met, then do something". To be fancier, you can also add "else". Here is an example:
#!/usr/bin/sh
variable1=Hello;
variable2=Goodbye;
echo "Let's start!";
if [ "$variable1" = "$variable2" ]; then
echo "The salutations are the same!";
else
echo "The salutations are not the same!";
fi
variable2=Hello;
echo "";
echo "Let's try again!";
if [ "$variable1" = "$variable2" ]; then
echo "The salutations are the same!";
else
echo "The salutations are not the same!";
fi
This simple script sets up 2 variables, and the compares them. In the second paragraph, it changes the second variable to be the same as the first variable, and runs the same if statement test on them again. The output looks like:
$ ./test.sh
Let's start!
The salutations are not the same!
Let's try again!
The salutations are the same!
While not very exciting, you can do many different types of data. Keep in mind that comparing text is different from comparing data. More comparison tests can be found here.
Lastly, I would like to mention that you can put a shell script into a type of "debug" mode where it will output more verbose information while it is running. This is by changing the first line of the script from:
#!/usr/bin/sh
to
#!/usr/bin/sh -x
Now the output looks like:
$ ./test.sh
variable1=Hello
variable2=Goodbye
+ echo Let's start!
Let's start!
+ [ Hello = Goodbye ]
+ echo The salutations are not the same!
The salutations are not the same!
variable2=Hello
+ echo
+ echo Let's try again!
Let's try again!
+ [ Hello = Hello ]
+ echo The salutations are the same!
The salutations are the same!
Good luck with your scripting, and I welcome any comments or queries.
ric_man's personal observations on Information Technology, Freedom, Open Source, Linux, Football, sneakers, and whatever...
Wednesday, 11 May 2011
Friday, 6 May 2011
Things they could fix in Canberra #9.1
This is not a post in its own right, but more of an additional comment to a previous post that I made about the Gungahlin Drive Extension (GDE). Not much useful thought goes into Canberra, and here is more evidence of the single lane road that was to connect north Canberra to south Canberra - which was a short-sighted decision by the local government in Canberra.
These images were taken during Easter 2011, driving from Gungahlin towards Woden (ie. south):
Due to lack of proper planning, the local Canberra resident had to put up with delays and bottlenecks in traffic while using the GDE. And from these images, they would have to put up with them even more, as they have moved entire sections of road, and had to widen and reconstruct certain bridges.
I hope all Australians now realize why taxes are high. What else funds this stupidity ($85.5 million worth of it)?
These images were taken during Easter 2011, driving from Gungahlin towards Woden (ie. south):
Due to lack of proper planning, the local Canberra resident had to put up with delays and bottlenecks in traffic while using the GDE. And from these images, they would have to put up with them even more, as they have moved entire sections of road, and had to widen and reconstruct certain bridges.
I hope all Australians now realize why taxes are high. What else funds this stupidity ($85.5 million worth of it)?
Subscribe to:
Posts (Atom)
























































































































