Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Data Science with the Command Line

You're reading from  Hands-On Data Science with the Command Line

Product type Book
Published in Jan 2019
Publisher Packt
ISBN-13 9781789132984
Pages 124 pages
Edition 1st Edition
Languages
Authors (3):
Jason Morris Jason Morris
Profile icon Jason Morris
Chris McCubbin Chris McCubbin
Profile icon Chris McCubbin
Raymond Page Raymond Page
Profile icon Raymond Page
View More author details

The simple case

Frequently, string comparison is done using the test operator, [. This is ill-advised in bash, as there's a much more convenient format for string comparison, using the case statement. Here's a simple example:


testcase() {
for VAR; do
case “${VAR}” in
'') echo “empty”;;
a) echo “a”;;
b) echo “b”;;
c) echo “c”;;
*) echo “not a, b, c”;;
esac
done
}
testcase '' foo a bar b c d

The testcase function lets us test the case statement by wrapping it in a for loop that assigns each function argument to the VAR variable, then executes the case statement. With the foo a bar b c d arguments, we can expect the following output:

 empty
not a, b, c
a
not a, b, c
b
c
d
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}