|
| 1 | +--- |
| 2 | +Title: 'Conditional Statements' |
| 3 | +Description: 'Allow a script to make decisions based on conditions.' |
| 4 | +Subjects: |
| 5 | + - 'Bash/Shell' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Bash/Shell' |
| 9 | + - 'Command Line' |
| 10 | + - 'Script' |
| 11 | + - 'Conditionals' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-the-command-line' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +Conditional statements in Bash allow a script to make decisions based on conditions. These statements help in controlling the flow of execution by performing different actions based on whether a condition evaluates to true or false. Bash supports several types of conditional checks, including comparisons between numbers, strings, and file conditions. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +Bash conditional statements are typically written using `if`, `elif`, `else`, and `fi` keywords. The basic syntax is: |
| 22 | + |
| 23 | +```pseudo |
| 24 | +if [ condition ]; then |
| 25 | + # Code to execute if condition is true |
| 26 | +elif [ another_condition ]; then |
| 27 | + # Code to execute if another_condition is true |
| 28 | +else |
| 29 | + # Code to execute if no conditions are met |
| 30 | +fi |
| 31 | +``` |
| 32 | + |
| 33 | +### Basic Operators |
| 34 | + |
| 35 | +The different operators that Bash provides can be classified into several categories based on the data type of the operands. |
| 36 | + |
| 37 | +**Numeric Operators:** |
| 38 | + |
| 39 | +| Operator | Description | |
| 40 | +| -------- | ------------------------ | |
| 41 | +| `-eq` | Equal to | |
| 42 | +| `-ne` | Not equal to | |
| 43 | +| `-lt` | Less than | |
| 44 | +| `-le` | Less than or equal to | |
| 45 | +| `-gt` | Greater than | |
| 46 | +| `-ge` | Greater than or equal to | |
| 47 | + |
| 48 | +**String Operators:** |
| 49 | + |
| 50 | +| Operator | Description | |
| 51 | +| -------- | ------------------- | |
| 52 | +| `=` | Equal to | |
| 53 | +| `!=` | Not equal to | |
| 54 | +| `-z` | String is empty | |
| 55 | +| `-n` | String is not empty | |
| 56 | + |
| 57 | +**File Test Operators:** |
| 58 | + |
| 59 | +| Operator | Description | |
| 60 | +| -------- | ---------------------- | |
| 61 | +| `-e` | File exists | |
| 62 | +| `-f` | File is a regular file | |
| 63 | +| `-d` | File is a directory | |
| 64 | +| `-r` | File is readable | |
| 65 | +| `-w` | File is writable | |
| 66 | +| `-x` | File is executable | |
| 67 | + |
| 68 | +## Example |
| 69 | + |
| 70 | +Here is an example that takes a number from the user and then uses conditional statements in Bash to check if the number is greater than, less than, or equal to 0: |
| 71 | + |
| 72 | +```bash |
| 73 | +#!/bin/bash |
| 74 | + |
| 75 | +read -p "Enter a number: " num |
| 76 | + |
| 77 | +if [ "$num" -gt 0 ]; then |
| 78 | + echo "The number is positive." |
| 79 | +elif [ "$num" -lt 0 ]; then |
| 80 | + echo "The number is negative." |
| 81 | +else |
| 82 | + echo "The number is zero." |
| 83 | +fi |
| 84 | +``` |
| 85 | + |
| 86 | +The above code produces the following output if the user inserts the number 5 when prompted: |
| 87 | + |
| 88 | +```shell |
| 89 | +Enter a number: 5 |
| 90 | +The number is positive. |
| 91 | +``` |
0 commit comments