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
2
  • This is working for me. I get no errors, and on a few runs, I see echoed: Queen of Spades; Ace of Clubs; 7 of Hearts. This is with bash 4.2.24. Commented Apr 29, 2013 at 0:03
  • That's weird... how are you running it? I'm using Ubuntu and in the terminal, I type chmod a+rx cards.sh then ./cards.sh to run it. Commented Apr 29, 2013 at 0:06

1 Answer 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.

Sign up to request clarification or add additional context in comments.

1 Comment

face palm So it was a typo all along. Must've been the auto correct on the word document that that given. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.