O'Reilly logo

Shell Programming in Unix, Linux and OS X, Fourth Edition by Patrick Wood, Stephen G. Kochan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7. Decisions, Decisions

This chapter introduces the conditional statement, a construct that is present in almost every programming language: if. It enables you to test a condition and then change the flow of program execution based on the result of the test.

The general format of the if command is

if commandt then         command         command         ... fi

where commandt is executed and its exit status is tested. If the exit status is zero, the commands that follow between then and fi are executed; otherwise, they’re skipped.

Exit Status

To understand how conditional tests work, it’s important to know about how Unix works with what’s called the exit status. Whenever any program completes execution, it returns an exit status code to the shell. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required