Introduction
Use if-then-else to make decisions in your script. It helps your script choose what to do based on conditions.
if [ condition ] then commands else other_commands fi
if [ "$name" = "Alice" ] then echo "Hello, Alice!" else echo "Who are you?" fi
if [ -f "file.txt" ] then echo "File exists." else echo "File not found." fi
#!/bin/bash read -p "Enter a number: " num if [ "$num" -gt 0 ] then echo "The number is positive." else echo "The number is zero or negative." fi
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