Skip to content

Instantly share code, notes, and snippets.

@blaklinten
Created August 5, 2024 21:37
Show Gist options
  • Select an option

  • Save blaklinten/3dd29f3f73b75337a86b8d7cdb59bdd7 to your computer and use it in GitHub Desktop.

Select an option

Save blaklinten/3dd29f3f73b75337a86b8d7cdb59bdd7 to your computer and use it in GitHub Desktop.
Bash-math
#!/bin/bash
################################################
# Test to see if Bash actually don't know math #
################################################
ROUNDS=0
ERRORS=0
START_TIME_SEC="1722888722"
END_TIME_SEC="$((START_TIME_SEC + 3695))"
# 3695 sec = 1h1m35s
DURATION="1:1:35"
diff()
{
((DIFF = $2 - $1))
((SECONDS = DIFF % 60))
((MINUTES = (DIFF % 3600) / 60))
((HOURS = DIFF / 3600))
echo "$HOURS:$MINUTES:$SECONDS"
}
trap break SIGINT
while true; do
((ROUNDS += 1))
if [ "$((ROUNDS % 10))" = "0" ]; then
printf '\r\e[0K%s' "$ROUNDS processed."
fi
CALCULATED_DIFF="$(diff "$START_TIME_SEC" "$END_TIME_SEC")"
if [ "$DURATION" != "$CALCULATED_DIFF" ]; then
((ERRORS += 1))
printf '\n%s\n' "Round $ROUNDS gave invalid duration <$CALCULATED_DIFF> - Should be $DURATION" | tee ERROR-$ERRORS
fi
done
printf '\r\e[0K%s\n' "$ROUNDS processed."
echo "Test done, $ERRORS errors were found. See ERRORS"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment