Work the Shell - Calling All Functions, and Some Math Too!

Shuffle off to the next step in creating a shell script Blackjack game.


case $rank in
 0 ) rank="King"    ;;
 1 ) rank="Ace"     ;;
 11) rank="Jack"    ;;
 12) rank="Queen"  ;;

Put these together in a function called showCard (which returns “$cardname” as the calculated rank and suite), and we can now clean up a bit:

initializeDeck shuffleDeck

echo -n "** Player's hand: "
  showCard ${newdeck[1]} ; echo -n "$cardname, "
  showCard ${newdeck[3]} ; echo "$cardname"

echo -n "** Dealer's hand: "
  showCard ${newdeck[2]} ; echo -n "$cardname, "
  showCard ${newdeck[4]} ; echo "$cardname"

Now we can start to see the game come together! Consider:

$ ./
** Player's hand: 8 of Clubs, 3 of Diamonds
** Dealer's hand: King of Spades, 3 of Spades
$ ./
** Player's hand: 2 of Spades, 4 of Spades
** Dealer's hand: 10 of Spades, 4 of Hearts

Let's stop here this month as that's a lot of code to dig through already. I invite you to pop over to the LJ FTP site to grab all the source code so far, so you can experiment with this script yourself too.

Next month, we'll start looking at the game logic itself, but for now, Vegas beckons for a big tradeshow and, well, I can write it off as research for Linux Journal, can't I?

Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at


Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Anonymous's picture

AARGH - four SUITs of cards... a SUITE of rooms... where's your editor!