Informing the IBM Community

AIX tech tip: Userproof your scripts, or else…


Abstract technology background with hexagonal shapes
It must be the techie in me, but I don’t like to make arrangements to meet someone without having a backup plan. What if they don’t make it on time? What if I don’t?

I know what you’re thinking: call their mobile. But do I have their mobile number handy? Will we have coverage? Will my battery be charged?

Perhaps you’re starting to think that I might need some professional help. I mean, how paranoid can this guy get? Well, when it comes to scripting, I’d say very.

Often when you write a script, you’ll say something like: if X is true, do this. But if Y is true, do that. Here’s a simple example:

echo “Do you want to continue? (Enter ‘y’ or ‘n’): “
read answer

if [ “$answer” == “n” ]
    echo “Bye bye.”
elif [ “$answer” == “y” ]
     # script does interesting stuff part 1.
    echo “Running part 1”
# interesting stuff part 2
echo “Running part 2”

This little piece of code asks the user to enter “n”to drop out or “y” if they want to continue. Sounds easy, right? I mean, how hard is it to press the n key or the y key and then press enter?

So basic, that if you wrote the script yourself, you’d probably test it by running it with an n as your answer:

Do you want to continue? (Enter ‘y’ or ‘n’):
Bye bye.
[ Script exits]

And, of course, you’d test it with by answering ‘y’:

Do you want to continue? (Enter ‘y’ or ‘n’):

Running part 1
Running part 2

How basic could you get? Do you want to continue? If the answer is ‘n’, then you’re out of here. Otherwise, you’re on the express train to whatever the script is going to continue doing, and the next exit is…well, who knows?

Answer y or n (or something random)

But what if the user enters an upper case “N”? The answer doesn’t equal “n” (AIX being case-sensitive), so the script will continue. The user may think they’ve said no, and the script continues anyway. The one thing I’ve learnt over the years is that  computers do not have a mind of their own. In fact, they’re ruthlessly obedient.

Look at the script example. We give the choice either to answer “y” to continue, and then the script does interesting stuff part 1, followed by part 2.

Now, supposing the user has given a wrong answer to the “continue” question: neither ‘n’ (don’t continue) or ‘y’ (continue).  The script will not do interesting stuff part 1, but it will still go onto part 2.

This is bad. Or at least it may be.

Part 1, for example, may do a backup of some files, and then part 2 removes them. Backup, then remove. And it could all come undone because someone has entered a Y (upper case) instead of y in lower case.

You could waste a lot of time finding out what happened when it could be a simple step to capture the wrong answers.

All you need is a catch-all “else” statement.

elif [ “$answer” == “y” ]
      # script does interesting stuff part 1.
     echo “You should have answered ‘y’ or ‘n’.  Start again.”

Unlike computers, people are not always obedient. So you need to cover for the occasional user who doesn’t read properly, or has fat fingers, or answers ‘y’ with something like:


(u is next to the y on my keyboard)

or anything else.

The moral of the story: whenever you write an “if” statement, always include an “else”. This is just a scripting way of making backup plans when you arrange to meet someone somewhere.

Do it. Or else.

Having worked on IBM systems since 1991, Anthony English has seen how AIX and virtualisation can manage all kinds of business environments. He is a well-recognised author in the IT field and he writes about business systems improvement through his blog at Anthony is based in Sydney, Australia, with his wife and his seven children.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.