The introduction
To avoid looking silly and being made fun the world over for having made a program that prints hair-raising butchered English like Copying 1 files, many programming languages have ternary constructs.
in PHP for instance one can use the ternary operator, ?:
<?php $intFiles=1; printf("Copying %d file%s",$intFiles,1==$intFiles?null:'s'); ?>
BASH is often insulted for having awkward and arcane syntax and as far as ternaries are concerned it doesn't disappoint. The closest equivalent is the case construct:
The first solution
file=1 echo -n "Copying ${file} " case "${file}" in 1) echo "file" ;; *) echo "files" ;; esac
There are some problems with this approach. Not only do you have to split up your printing into several parts you also have to have a case construct for each test. What if you wanted to construct a sentence with several items being counted? Can you spell tiresome?
Luckly BASH, awkward arcane that it is, has a more florid solution, in the form of parameter expansion:
${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.
In other words, using this expansion you can print the value of a variable, or, a default value if the variable isn't defined or set
A better solution
word=([1]=file) number=1 echo "Copying ${word} ${word[${number}]:-files}"
OK, as long as you manage to keep track of the tongue-twisting mix of curly braces and brackets it is surprisingly easy and compact, at least when compared to the case construct.
Let's put it in a small script and see how it behaves with some edge cases:
#!/bin/bash # save as /tmp/newcats.sh catword=( [1]=cat ) while `true`; do read -p "How many? " cats || break echo "You have seen ${cats} ${catword[${cats}]:-cats}" done
$ sh /tmp/newcats.sh How many? 0 You have seen 0 cats How many? 1 You have seen 1 cat How many? 2 You have seen 2 cats How many? -1 You have seen -1 cat How many? -2 You have seen -2 cats How many? a You have seen a cats How many? @ /tmp/newcats.sh: line 13: @: syntax error: operand expected (error token is "@") How many? 1.5 /tmp/newcats.sh: line 13: 1.5: syntax error: invalid arithmetic operator (error token is ".5") How many? a few /tmp/newcats.sh: line 13: a few: syntax error in expression (error token is "few")
Not bad, we see that negatives are handled correctly because for negative subscripts BASH substracts them from the total array count. So -1 means the highest item and since we have only one item, -1 is treated as 1, and -anything else is treated as plural. Great!
We do fall apart when it comes to floats and gibberish because indexed arrays need an integer subscript.
The obvious solution, therefore, is to use an associative array:
Solution the third
For associative arrays, BASH will not perform arithmetic on its subscripts so the handy trick with negatives will not work any more. This is however easily remedied by explicitly defining a -1 item.
BASH also requires explicit declaration of associative arrays through the -A flag.
#!/bin/bash # save as /tmp/coolcats.sh declare -A catword=( [1]=cat [-1]=cat ) while `true`; do read -p "How many? " cats || break echo "You have seen ${cats} ${catword[${cats}]:-cats}" done
$ sh /tmp/coolcats.sh How many? 0 You have seen 0 cats How many? 1 You have seen 1 cat How many? 2 You have seen 2 cats How many? -1 You have seen -1 cat How many? -2 You have seen -2 cats How many? a You have seen a cats How many? @ You have seen @ cats How many? 1.5 You have seen 1.5 cats How many? a few You have seen a few cats How many? /tmp/coolcats.sh: line 14: catword: bad array subscript You have seen cats How many? %#$%^#[] You have seen %#$%^#[] cats How many? '"`:{} You have seen '"`:{} cats
Excellent! We are handling all cases correctly and the only thing we choke on is an empty input. That is easily helped though by using the same technique on the variable referencing the index.
Putting it all together
#!/bin/sh # save as /tmp/ubercat.sh declare -A catword=( [-1]=cat [1]=cat ) declare -A is=( [-1]=is [1]=is ) while `true`; do read -p "How many? " cats || break echo "There ${is[${cats:-0}]:-are} ${cats:-0} ${catword[${cats:-0}]:-cats}" done
~]$ sh /tmp/ubercat.sh How many? There are 0 cats How many? 1 There is 1 cat How many? 1.5 There are 1.5 cats How many? -1 There is -1 cat How many? -1.5 There are -1.5 cats How many? a few There are a few cats How many? %#@%$^! There are %#@%$^! cats
In conclusion
BASH is a versatile scripting language and although its syntax is awkward and possibly arcane, it can quite hold its own when it comes to manipulating strings. In this age of ever faster computers with more and more memory one is often seduced to outsourcing functionality in BASH scripts to utilities such as sed and awk and even full-size interpreters like perl and php. Digging into the BASH manual is daunting. The thing is a behemoth containing more than 100 pages of dry terse language. Spend some time experimenting however and you will discover some real gems and ultimately, doing things in the language is always going to be more efficient than doing it externally.