| 1 | #!/bin/bash |
| 2 | # pick-card.sh |
| 3 | |
| 4 | # This is an example of choosing random elements of an array. |
| 5 | |
| 6 | |
| 7 | # Pick a card, any card. |
| 8 | |
| 9 | Suites="Clubs |
| 10 | Diamonds |
| 11 | Hearts |
| 12 | Spades" |
| 13 | |
| 14 | Denominations="2 |
| 15 | 3 |
| 16 | 4 |
| 17 | 5 |
| 18 | 6 |
| 19 | 7 |
| 20 | 8 |
| 21 | 9 |
| 22 | 10 |
| 23 | Jack |
| 24 | Queen |
| 25 | King |
| 26 | Ace" |
| 27 | |
| 28 | # Note variables spread over multiple lines. |
| 29 | |
| 30 | |
| 31 | suite=($Suites) # Read into array variable. |
| 32 | denomination=($Denominations) |
| 33 | |
| 34 | num_suites=${#suite[*]} # Count how many elements. |
| 35 | num_denominations=${#denomination[*]} |
| 36 | |
| 37 | echo -n "${denomination[$((RANDOM%num_denominations))]} of " |
| 38 | echo ${suite[$((RANDOM%num_suites))]} |
| 39 | |
| 40 | |
| 41 | # $bozo sh pick-cards.sh |
| 42 | # Jack of Clubs |
| 43 | |
| 44 | |
| 45 | # Thank you, "jipe," for pointing out this use of $RANDOM. |
| 46 | exit 0 |