1

The purpose of this is basically create a deck of cards and randomly draw 1. Right now, it's not even breaking up the strings and reading them into the array. I get both a command not found for the suites and the denominations string and then another error for RANDOM.

Am I writing in the IFS line wrong? I'm brand new to bash scripting and I really appreciate everyone's help =]!

#!/bin/bash
# Count how many elements.
Suites=“Clubs Diamonds Hearts Spades”
Denominations=“2 3 4 5 6 7 8 9 10 Jack Queen King Ace”
# Read into array variable.
IFS=' '
suite=($Suites)
denomination=($Denominations)
# Count how many elements.
num_suites=${#suite[*]}
num_denominations=${#denomination[*]}
echo -n "${denomination[$((RANDOM%num_denominations))]} of "
echo ${suite[$((RANDOM%num_suites))]}
exit 0
4

1 回答 1

2
#!/bin/bash
# Count how many elements.
Suites="Clubs Diamonds Hearts Spades"
Denominations="2 3 4 5 6 7 8 9 10 Jack Queen King Ace"
# Read into array variable.
IFS=' '
suite=($Suites)
denomination=($Denominations)
# Count how many elements.
num_suites=${#suite[*]}
num_denominations=${#denomination[*]}
echo -n "${denomination[$((RANDOM%num_denominations))]} of "
echo ${suite[$((RANDOM%num_suites))]}
exit 0

Here is the script I ran. The one difference I see is that I use " while you used “ and ” on your Suites and Denomination lines. Or, 0x22 versus u+201c and u+201d.

于 2013-04-29T00:00:25.837 回答