Concept Flow - if-then-else
Start
Evaluate condition
Execute THEN block
End
Execute ELSE block
End
The script checks a condition. If true, it runs the THEN part; if false, it runs the ELSE part, then ends.
if [ "$num" -gt 10 ]; then echo "Greater than 10" else echo "10 or less" fi
| Step | Condition | Condition Result | Branch Taken | Output |
|---|---|---|---|---|
| 1 | [ "$num" -gt 10 ] | True | THEN | Greater than 10 |
| 2 | End of if-then-else | - | - | - |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| num | 15 | 15 | 15 |
if [ condition ]; then # commands if true else # commands if false fi Checks condition once. Runs THEN block if true. Runs ELSE block if false. Ends after one branch runs.
if-then-else structure do in a bash script?if [ condition ]; then.#!/bin/bash x=5 if [ $x -gt 3 ]; then echo "Greater" else echo "Smaller" fi
#!/bin/bash if [ $num -eq 10 ] echo "Ten" else echo "Not Ten" fi
then keyword to start the true block.then after the condition line.num=4 # Choose correct script
Pass the test and this pattern moves unknown