Case statement - continue

I have a case statement. IS "continue" working in case?

 for file in ls dir/*
  
 case $file in
      a)
            do something
            continue
       ;;
       b)
            do something
            continue
       ;;
 esac
 

It is a Bourne shell

You're missing the "done" at the end, but yes, "continue" works properly inside case.

Example:

for X in a b c d e
do
        case "$X" in
        a) ;;
        b) ;;
        c) ;;
        d) continue ;;
        e) ;;
        esac

        echo "$X"
done

prints

a
b
c
e

...since the continue for "d" skips the echo by going straight back to the top.

I need continue after each case in

You don't need a continue after every case. You only need a continue if you want it to skip up to the top of the loop instead of running things after esac. continue is a control for the loop, not for case.

See my example.

That is what I need

 for file in ls dir/*
do
 case $file in
     a)
        do something
         continue
        ;;
     b)
        do something
         continue
        ;;
  esac
       do something else
done
 

Will this works?

For that, you can do:

case "$X" in
a) something ;;
b) something else ;;
*) whatever else ;;
esac
1 Like

No, it show me an error

./sftp_ondemand.sh[95]: syntax error at line 324 : `else' unexpected

Hi,

Here's a code example combining case , for and if :

$ cat script.sh
#!/bin/bash

for animal in `/bin/ls animals`
do
        case "$animal" in
                "cat")
                        echo "This is an acceptable animal: $animal"
                        ;;
                "dog")
                        echo "This is another cceptable animal: $animal"
                        ;;
                *)
                        if [ "$animal" == "furby" ]
                        then
                                #Let's skip this - it's not a real animal
                                continue
                        else
                                echo "And this is also an acceptable animal: $animal"
                        fi
                        ;;
        esac
done
$ ls animals
cat  dog  furby  monkey  zebra
$ ./script.sh
This is an acceptable animal: cat
This is another cceptable animal: dog
And this is also an acceptable animal: monkey
And this is also an acceptable animal: zebra
$

When we run the script, the effect of the continue statement when animal is equal to "furby" is to skip at that point entirely to the next anmal in the list, which is "monkey". So we only see "cat", "dog", "monkey" and "zebra" mentioned in the output, with the continue causing "furby" to be skipped in the output entirely.

Hope this helps clear up how these things can be used in combination.

We have no idea what 'else' that is, where it is, or why you put it there.

This is because we can't see your computer from here. Show your code.

Hi,

To also show how continue can be used with case alone, here's a slightly modified version without the if clause.

#!/bin/bash

for animal in `/bin/ls animals`
do
        case "$animal" in
                "cat")
                        echo "This is an acceptable animal: $animal"
                        ;;
                "dog")
                        echo "This is another cceptable animal: $animal"
                        ;;
                "furby")
                        #Let's skip this - it's not a real animal
                        continue
                        ;;
                *)
                        echo "And this is also an acceptable animal: $animal"
                        ;;
        esac
done

Sorry all, but I am working on AIX and Bourne shell

The *) does the trick. Because it is last and matches everything that was not matched before, it behaves like an else .

cd dir &&
for file in *
do
  case $file in
  a)
        do something
  ;;
  b)
        do something
  ;;
  *)
       do something else
  ;;
  esac
done

So change #!/bin/bash to #!/bin/sh. Nothing about that code was bash specific.

Another thing to note is that on AIX /bin/sh is a POSIX shell, not a Bourne shell (it is hard linked to psh and ksh).

The Bourne shell is bsh , but it is only there for legacy reasons, and you probably do not want to use that..