Skip to content

Instantly share code, notes, and snippets.

@tevino
Last active June 21, 2021 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevino/e390f186c77e4a39811fbb0d4fe18366 to your computer and use it in GitHub Desktop.
Save tevino/e390f186c77e4a39811fbb0d4fe18366 to your computer and use it in GitHub Desktop.
A simulation of The Ultimate Machine built by Claude Shannon https://en.wikipedia.org/wiki/Useless_machine
#!/bin/bash
IS_ON=0
loading(){
for _ in {1..3}; do
printf '.'
sleep 0.2
done
}
handle_usr1(){
((IS_ON^=1))
}
work(){
printf "\n => Sending USR1 to %d" $$
loading
kill -USR1 $$
printf "\n"
}
main() {
printf "This is a simulation of The Ultimate Machine built by Claude Shannon\n"
printf "https://en.wikipedia.org/wiki/Useless_machine\n"
printf "\n"
printf "Usage:\n"
printf " Send USR1 to this process(%d) to toggle the switch:\n" "$$"
printf " kill -s SIGUSR1 %d\n\n" "$$"
trap 'handle_usr1' USR1
while true; do
printf "\rThe Ultimate Machine is "
if [[ $IS_ON != 0 ]]; then
printf "ON "
work
else
printf "OFF"
fi
sleep 0.1
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment