Shell conditions and if statements

Since the contents of variables will, well, vary it is often useful to be able to make decisions based on them. Strings and numbers can be easily compared to explicit values or other variables. Here is a simple example:

$ i=107
$ if [ $i -gt 100 ]
> then
> echo "Wow, i got all the way up to $i"
> else
> echo "i is only up to $i"
> fi
Wow, i got all the way up to 107
$ i=22
$ if [ $i -gt 100 ]
> then
> echo "Wow, i got all the way up to $i"
> else
> echo "i is only up to $i"
> fi
i is only up to 22

Here we see a simple if statement. When executed the expression within the brackets is evaluated to either true or false. If the expression is found to be true the commands after the then will be executed, otherwise the commands after the else are executed.

The expression shown here is the greater than expression (>). The symbols we typically use for greater than and less than have specific significance in the UNIX shell, so to compare values we use -gt for greater than and -lt for less than. Comparisons can also be made between strings of text. More information about comparing text and numbers can be found in my book.

Oracle Shell ScriptingFor more information like this check out my book Oracle Shell Scripting, only $34.95 from Rampant TechPress.

Buy it now! rates Life After Coffee an 8.0!

Life After Coffee at Blogged
Another nice little bit of recognition showed up in my inbox recently… has rated Life After Coffee an 8.0 in the category of Technology Blogs!

OK, so 8.0 isn’t the greatest rating ever, but given that they take into account relevance of content (relevant to what I don’t know) and frequency of updates (there is no April) I’m pretty happy with it.

Now, if you’re interested in the best technology blogs around, check out their Technology Blog Directory. Many of the top picks are focused, professional blogs, not like the crap you’ll find here. There’s some great stuff to check out though and in general I would agree with their rankings.

Oracle Shell Scripting review – Excellent read!

My book, Oracle Shell Scripting has received another 5-star review on Amazon!

A. Tucker “Rat” writes:

Oracle administrators of all levels will find benefit in this book. The author has put together the basics in shell scripting and OS fundamentals in aid of maintaining and monitoring scalable production Oracle environments. Each example is easy to read and follow and leaves the reader with room to explore and extrapolate on their own with little effort. A great lookup source for old timers and “must have” for newbies.

I highly recommend this book

Thanks for the great review Rat!

Oracle Shell ScriptingCheck out my book Oracle Shell Scripting, only $34.95 from Rampant TechPress.

Buy it now!

The next must-have travel gadget: Belkin Mini Surge Protector with USB Charger

Belkin surge protector with USB chargerFinally, the solution to my mobile charging needs: the Belkin Mini Surge Protector with USB Charger.

Coming soon from Belkin this small device offers surge protection for your laptop and devices when you travel and also has two USB charging ports! You should be able to charge many cell phones (like the Razr or Blackberry) and devices like iPods right off the USB ports eliminating the need to keep your computer awake while your devices charge. This should also be nice if you want to leave the computer behind but still need a way to charge your iPod (it’s been a long time since iPods came with 110 volt chargers.)

Belkin is listing the surge protector with a modest $25 price on their site and list it as “Coming soon”. As soon as this becomes available I’ll grab one and post a review here.

via the Daily Giz Wiz podcast

Linux and UNIX shell variables

Shell variables give us a place to store values for use by the system, our shell, shell scripts or by programs we run. Each session in UNIX has a set of variables that collectively are referred to as the environmental variables. These variables tell the system where to find applications and documentation, where the user’s home directory is, the current working directory and much more. You can easily view all the environmental variables in the current session with the env command:

$ env
SSH_CLIENT= 54620 22

These are some of the default variables provided by the system. As Oracle users we’re also familiar with shell variables such as $ORACLE_HOME and $ORACLE_SID.

While most of the environmental variables in the output above are set by the system, variables like $ORACLE_HOME need to be set by the user. In the bourne and bash shells, environmental variables are set by giving the variable name, the equal sign (=) and then the value the variable should be set to. The variable must then be exported with the export command so it can become available to subsequent commands.

$ ORACLE_HOME=/u01/app/oracle/product/10.2.0/Db_1
$ export ORACLE_HOME

When setting variables you simply use the variable name; however, as we see in the above example we use the $ prefix to retrieve the value of a variable. You may also see the variable definition followed by the export on the same line separated by a semicolon. The semicolon marks the end of the first command allowing the commands to be executed as if they were on separate lines.

These exported environmental variables typically affect how UNIX and Linux behave or how other commands run. Though not a rule, environmental variable names are typically all uppercase. Lowercase variable names are typically used for local shell variables that are only needed in the current session or script. Local shell variables need not be exported.

$ day=Monday
$ echo $day

It’s important to remember that shell variables are specific to a session, not a user. That means if you change the environment variables in one session it will not have any effect on other active sessions.

Beyond the familiar Oracle related shell variables we’ll be using shell variables for storing date information, file and directory names, passwords and much more.

Oracle Shell ScriptingFor more information like this check out my book Oracle Shell Scripting, only $34.95 from Rampant TechPress.

Buy it now!