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!

One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix