state="start"
while true; do
case "$state" in
"start")
echo "initial state"
state="next"
;;
"next")
echo "next state"
state="done"
;;
"done")
echo "done"
break
;;
*)
echo "invalid state \"$state\""
break
;;
esac
done
running the above state machine prints
initial state
next state
done
This structure is useful for programs that are easier to express using a flow chart (state chart) than a linear structure, such as an interactive script with retry options.
No comments:
Post a Comment