12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
15 : 05 : 54 : 54
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
Become a member
to unlock all features

    Use case for Complicated Conditional Statements in Bash

    Cameron NokesCameron Nokes

    Lots of if and if else statements are hard to read and tough to maintain in any language, and bash is no exception. In this lesson, we’ll learn how to use case, bash’s construct for a switch statement. In bash, case actually does pattern matching, which gives some great flexibility. We’ll write a script that will uncompress a variety of different archive file types (like .tar or .zip), based on their file extension.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: To create a switch statement in Bash, we use the case statement. I'm going to create a switch statement here on the first positional parameter that's passed to the script. We do case in. On the following lines, we'll enumerate the cases that we match against.

    Then we close the case statement with case spelled backwards, just like an if statement. Here, we match against this value. I'm just going to do the string a. I follow that by a parenthesis there. Then right here, I execute the code if this case is true.

    I'll just say echo a matched. Then I do a double semicolon. Then I'll do "b" here, echo b matched. That's the syntax for case. Also note that this can be on multiple lines. Let's jump to a terminal and try this out here.

    Let's do case, and I'll do a. That's matching. There's b. Let's do c. What if we do one that we didn't do? We'll do D. We can see that there is no matching case for the d, so just nothing happened. Let's do something a little bit more practical.

    Let's write a script that extracts a file archive, depending on the file extension. Let's clear these cases. We'll make it so that the first file passed to the script is what we match against. One thing that's different about Bash, compared to other programming languages, is that the case statement supports pattern matching.

    It doesn't support full regexes, but it does support Bash's glob-like syntax for pattern matching. We'll see how that works here. We'll do tar or, and we'll do .tgz. * here will match anything that ends with this file extension.

    Then the pipe here is an or, just like in regex. In here, we'll say tar, and then pass our file name. If you want to read up on this command and the flags, look at the man pages for tar. Then next, we'll do gzip. That's .gz. We'll g unzip, and we'll do .zip here.

    Then for our last one here, I'm just going to do a wildcard. What this is going to do is that if none of these match, basically then it'll enter this case. In here, I'll just throw an error, just say inaudible extract one. Then I'll exit with an error status.

    Let's save that and try it out. We can see here, I've already set up several of these archive file types for us to test. Let's just try it out. We'll run case, and we'll do the tar. It worked. I just had a photos folder in there, so it extracted those.

    Let's jump up, and we'll try the archive.zip. Cool, that looks like that worked. Let's test our fail case. I'm actually just going to pass our script file in again, because that should fail. OK, cool. I'm seeing the error message. Then let's check the exit status, which is one.