Advanced

How To Use Conditions in Shell Scripts

Posted by aweber on · 9 comments
BeginLinux.com; By Mike Weber
Mail this story
Print this story

One of the most important tasks for a Linux administrator to achieve in shell scripting is to test for information you need to know. Conditions is a way to test for one thing and if that is true then take one action and if it is not true take a different action. This tutorial, with illustrations, will help you start writing conditions to test for the information you need.

Full Story

  Nav
» Read more about: Story Type: Tutorial

» Get new stories by e-mail: nightly or as they are published

« Return to the newswire homepage

  Related stories
» More like this

Comments (9)

Perhaps it's because I am self taught that I peruse all scripting HOWTOs to pick up tips on both coding and style. I am a little concerned about some of the style points used by the author in a lesson obviously aimed at newbies.

1. Variables should always be defined without anything on either side of the"=", yet this author often takes a newline. Also, I always quote variables, even if they are a number or the output of a command.

2. I like to put stuff echoed in quotes: it's good practice.

3. The conditional "if" often takes the "then" on the same line. Newbies should be advised that this is acceptable (and usual) practice.

4. But, most importantly, the author has failed to explain how the conditional works (viz, by checking exit status). If he had done so, he could neatly have transitioned from (a) grep, followed by (b) test exit status to simply "if $(grep -q ...); then". An explanation that the "q" switch to grep changed the function of grep's exit status would then have made sense, and would have introduced a better style of coding.

Or am I nitpicking?
rht --

The script jarred me. I didn't even think that the code in the example would work. I thought (and it seems to work that way on my box) that, instead of setting ADMIN to the email address, it set ADMIN to ''.

Unless, I guess, there is a missing line continuation...

I didn't see one, but it might have gotten swallowed.
The value of ADMIN is being hidden under JavaScript.
rht,

[serious]
Quoting: 1. Variables should always be defined without anything on either side of the"=" [...]
Do you really mean what this says? Do you mean typing? While I have not done much scripting in the shell language other, better scripting tools* certainly put content after the equals sign as an assignment of value [and can even set the data type with the use of quotes or their absence].

So perhaps you should consider writing an article on LXer or on your site to elucidate the proper coding style and rules of shell scripting. I would certainly read your articles on the topic.

Txt.

* e.g. perl, python or php [/serious]
Unix/Linux shells are unique in that you *can't* have white space between variable name, '=' sign, and value:

SHVAR="some dern string" # No spaces before or after the '='.

I believe that's what rht attempted to say.

SHVAR = "some dern string"

Just won't work. I think this arises from the shell parsing lines into tokens based on whitespace. If it parses a line into more than one token, it tries to run "SHVAR" as a command, not assign a value to it.
phsolide

[serious] Thank You! I thought I was hallucinating or even more ignorant than I suspected.

Txt. [/serious]
@phsolide @TxtEdMacs

TxtEdMacs is quite correct. I did mean no whitespace on either side of the "=". Thnx for pointing that out. However, on reflection, I think that so far as the author of the article is concerned my "uninterpreted" comment is probably valid.

As for coding style, I have a simple two-part test: In six months time will I be able to read my script in one pass; and will anyone else be able to read it at all.

A good test of "coding style" is: Write an eight-deep nested conditional (with loops) in awk. If you can read it when you have finished debugging it then you have good coding style.
rht,

[serious]

You mixed us up, I was the confused one, it was phsolide that explained your intent.

Txt. [/serious]

P.S. Something is badly skewed when I am forced to write serious notes.

Post a comment

A free account is needed to post. Write your comment here first: your text is kept in this browser while you sign up or log in, and is waiting for you when you come back.

Your message:
|||||
10
Markdown: **bold**, *italic*, [text](https://link), - lists, > quotes, $x^2$ for math. Formatting help

Sign up and post (it is free)  or  Log in and post