Skip to content

Instantly share code, notes, and snippets.

@chris-sun
Created May 1, 2019 16:17
Show Gist options
  • Save chris-sun/9126fc162d9f1aa63758e22666c71165 to your computer and use it in GitHub Desktop.
Save chris-sun/9126fc162d9f1aa63758e22666c71165 to your computer and use it in GitHub Desktop.
Shell script - while loop example
#!/bin/sh
MAX_TRIES=100
TRIES=0
OUTPUT=/tmp/output.txt
while [ $TRIES -lt $MAX_TRIES ]
do
# run some command here, "ls -l" is used below
ls -l
EXIT_VAL=$?
echo "********************"
echo "EXIT_VAL: ${EXIT_VAL}"
if [ $EXIT_VAL -ne 0 ]
then
echo $OUTPUT
# say command only works on Mac
say "A failure happened. Awesome"
fi
TRIES=`expr $TRIES \+ 1`
echo "Num tries: ${TRIES}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment