Introduction
The if-then-fi structure helps your script make decisions by running commands only when certain conditions are true.
if [ condition ]
then
commands
fiif [ -f myfile.txt ] then echo "File exists" fi
if [ "$USER" = "admin" ] then echo "Welcome, admin!" fi
if [ "$number" -gt 10 ] then echo "Number is greater than 10" fi
#!/bin/bash read -p "Enter a number: " num if [ "$num" -gt 0 ] then echo "You entered a positive number." fi
if-then-fi structure in a bash script?#!/bin/bash x=10 if [ $x -lt 5 ]; then echo "Less" fi echo "Done"
if [ $num -eq 5 ] then echo "Number is five" fi
n is even, and "Odd" otherwise. Which of these is the correct if-then-fi structure to do this?Pass the test and this pattern moves unknown