Jump to content

Control structures: Difference between revisions

From Squishu Wiki
No edit summary
No edit summary
Line 23: Line 23:
...
...
fi
fi
* elif
* example:  elif that exits with error code when given bad input
<pre>


You an put then on the same line, but then you have to use a semi
You an put then on the same line, but then you have to use a semi

Revision as of 12:36, 5 July 2025

General

Simple if statement

  • The general structure of an if statement
if condition
then
  statements
else
  statements
fi
if test -f blah.py
then
...
fi

if [ -f blah.py ]
then
...
fi

* elif
* example:  elif that exits with error code when given bad input

<pre>


You an put then on the same line, but then you have to use a semi

if [ -f blah.py ]; then
...
fi
  • if statement after reading user input
echo "Is it morning?  Please answer yes or no"
read timeofday

if [ $timeofday = "yes" ]; then
  echo "good morning"
else
  echo "good afternoon"
fi