Jump to content

Control structures: Difference between revisions

From Squishu Wiki
Created page with "== General == == Simple if statement == * The general structure of an if statement <pre> if condition then statements else statements fi </pre> <pre> if test -f blah.py then ... fi if [ -f blah.py ] then ... fi You an put then on the same line, but then you have to use a semi if [ -f blah.py ]; then ... fi </pre>"
 
No edit summary
Line 30: Line 30:
fi
fi
</pre>
</pre>
* if statement after reading user input
<pre>
echo "Is it morning?  Please answer yes or no"
read timeofday
if [ $timeofday = "yes" ]; then
  echo "good morning"
else
  echo "good afternoon"
fi

Revision as of 12:34, 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

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