-2

I am wanting to use a switch/ case statement in bash to check if a file name which is a string contains something but also does not.

Here is my case:

case "$fileName" in
    *Failed|!cp*)
       echo "match"
     ;;
esac

But this does not work currently, how can I see if the string matches "Failed" but also does not contain "cp"?

It would be great if this could be done in a switch/ case as well

3

2 Answers 2

2

! has to be followed by a parenthesized list of patterns, not the pattern itself.

| in a case is for OR, not AND. To get AND, you should nest cases.

case "$fileName" in
    *Failed)
        case "$fileName" in
            cp*) ;;
            *) echo "match" ;;
        esac
     ;;
esac

Or you could just use if instead of case:

if [[ $filename = *Failed && $filename != cp* ]]
then echo match
fi
Sign up to request clarification or add additional context in comments.

2 Comments

What is the 'extglob' option for?
Extended globbing syntax.
0

Alternatively you can use if and pipes for instance:

if echo 'Failed' | grep -v cp | grep -q Failed ; then
    echo Failed without cp
else
    echo It's either Winned or cp.
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.