Linux Shell Scripting Tutorial (LSST) v1.05r3 | ||
Chapter 3: Shells (bash) structured Language Constructs | ||
|
Making decision is important part in ONCE life as well as in computers logical driven program. In fact logic is not LOGIC until you use decision making. This chapter introduces to the bashs structured language constricts such as:
Is there any difference making decision in Real life and with Computers? Well real life decision are quit complicated to all of us and computers even don't have that much power to understand our real life decisions. What computer know is 0 (zero) and 1 that is Yes or No. To make this idea clear, lets play some game (WOW!) with bc - Linux calculator program.
$ bc
After this command bc is started and waiting for your commands, i.e. give it some calculation as follows type 5 + 2 as:
5 + 2
7
7 is response of bc i.e. addition of 5 + 2 you can even try
5 - 2
5 / 2
See what happened if you type 5 > 2 as follows
5 > 2
1
1 (One?) is response of bc, How? bc compare 5 with 2 as, Is 5 is greater then 2, (If I ask same question to you, your answer will be YES), bc gives this 'YES' answer by showing 1 value. Now try
5 < 2
0
0 (Zero) indicates the false i.e. Is 5 is less than 2?, Your answer will be no which is indicated by bc by showing 0 (Zero). Remember in bc, relational expression always returns true (1) or false (0 - zero).
Try following in bc to clear your Idea and not down bc's response
5 > 12
5 == 10
5 != 2
5 == 5
12 < 2
Expression | Meaning to us | Your Answer | BC's Response |
5 > 12 | Is 5 greater than 12 | NO | 0 |
5 == 10 | Is 5 is equal to 10 | NO | 0 |
5 != 2 | Is 5 is NOT equal to 2 | YES | 1 |
5 == 5 | Is 5 is equal to 5 | YES | 1 |
1 < 2 | Is 1 is less than 2 | Yes | 1 |
It means when ever there is any type of comparison in Linux Shell It gives only two answer one is YES and NO is other.
In Linux Shell Value | Meaning | Example |
Zero Value (0) | Yes/True | 0 |
NON-ZERO Value | No/False | -1, 32, 55 anything but not zero |
Remember both bc and Linux Shell uses different ways to show True/False values
Value | Shown in bc as | Shown in Linux Shell as |
True/Yes | 1 | 0 |
False/No | 0 | Non - zero value |
| ||
Linux Command(s) Related with Process | if condition |