2

Not sure why this script kept getting a syntax error on my CI machine.

if [ -d "$ws" ]; then
    if $condition; then
        echo "first condition"
    elif $second_condition; then
        ls -al
        shopt -s extglob
        rm -rf !(".repo")
        ls -al
        $[/myResource/ec.perl] $[/myResource/PWToolRootLocationFull]SetProperty.pl $[/myParent/parent.workflow.path]/builds/$[/myParent/variant.id]/is.repo.preserved "true"
    else
        echo "else"
    fi
fi

syntax error near unexpected token `('

I've tried

rm -rf !(.repo)
rm -rf "!(.repo)"
rm -rf !\(.repo\)
rm -rf !"(.repo)"

None of them are working.

9
  • I get the error only when shopt -s extglob is missing. Commented Nov 20, 2020 at 21:58
  • @choroba did you use bash shell? Commented Nov 20, 2020 at 21:59
  • 1
    That's the kind of error you'd get by invoking the script with sh instead of bash, but then you'd probably get a "shopt: not found" error first. The $[...] syntax looks odd: that's bash deprecated syntax for arithmetic expansion -- does it have some kind of special meaning in your CI pipeline? Commented Nov 20, 2020 at 22:00
  • If you use shopt -s extglob, then rm -rf -- !(*.repo) will delete anything in the current directory that doesn't end in .repo. Commented Nov 20, 2020 at 22:03
  • @glennjackman hmm, this CI pipeline is really old (over 10 years). I won't feel weird if it contains something out of date. I didn't get the "shopt: not found" error tho. Commented Nov 20, 2020 at 22:04

1 Answer 1

4

The shell has to parse the entire outer if statement (and all of its contents) before it can execute it, which means it has to parse that pattern before it executes shopt -s extglob. Solution: move shopt -s extglob to the very beginning of the script (or at least before the first if).

Sign up to request clarification or add additional context in comments.

1 Comment

BTW, this problem is a near-duplicate of the one in "Case statement with extglob", except in that case it was a function definition rather than an enclosing if block that caused pre-parsing. I'm not sure if it should be marked as a dupe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.