Fix minor complaints from shellcheck
[einar-bin] / imgurgame.bash
1 #!/bin/bash
2 # Because why not? curl must be installed
3
4 readonly BROWSER="google-chrome"
5
6 # Create a random imgur url that may or may not be valid
7 imgururl() {
8 # 1/3 chance for length of 5, 6 or 7 length string
9 STRLN=$((5 + ${RANDOM} % 3))
10 local ID
11 ID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${STRLN} | head -n 1)
12 echo "http://i.imgur.com/${ID}.jpg"
13 }
14
15 # Test if we found a valid URL
16 testimgur() {
17 local URL=${1}
18 local RESULT
19 RESULT=$(curl -s -o /dev/null -I -w "%{http_code}" ${URL})
20 if [[ ${RESULT} == "200" ]]; then
21 echo true
22 else
23 echo false
24 fi
25 }
26
27 main() {
28 local FAIL=0
29 while true; do
30 local URL
31 URL=$(imgururl)
32 if [[ $(testimgur ${URL}) == "true" ]]; then
33 echo "Found ${URL} after ${FAIL} failed tries"
34 ${BROWSER} ${URL} > /dev/null 2>&1
35 local FAIL=0
36 else
37 ((FAIL++))
38 fi
39 done
40 }
41
42 main