I've written a deployment plan in Bamboo, which runs all the scripts on an agent using sh, and since those agents run ubuntu 14.04 that equates to dash.

I want to do the following step:

rm -rf !(${bamboo.scripts_folder})

This should remove all the files/folders except the ones in the variable.

This works fine in bash, but when i run this in dash it gives me:

dash: 2: Syntax error: "(" unexpected

I tried running the following at the start of the script to force it to run as bash instead:

if [ "$(ps -p "$$" -o comm=)" != "bash" ]; then
    bash "$0" "$@"
    exit "$?"
fi

While that seems to have worked for me in the past, in this case for some reason, it doesn't appear to, as I get:

line 8: syntax error near unexpected token `('

I tried updating my script to read:

#!/bin/bash
echo "Early $0"
if [ "$(ps -p "$$" -o comm=)" != "bash" ]; then
    bash "$0" "$@"
    exit "$?"
fi

echo "Later $0"

rm -rf !(${bamboo.scripts_folder})

I was hoping that the echo statements would print out the shell that was currently running, but instead I get:

11-Apr-2017 13:51:25    Early /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh
11-Apr-2017 13:51:25    Early /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh
11-Apr-2017 13:51:25    Later /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh
11-Apr-2017 13:51:25    /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh: line 13: syntax error near unexpected token `('
11-Apr-2017 13:51:25    /home/bamboo/bamboo-agent-home/temp/49020935-118947854-118064182-ScriptBuildTask-740980554529016045.sh: line 13: `rm -rf !(font)'

This didn't give me anything more to go on. Can anyone suggest the "right" way to run this using dash? Or the "right" way to make this execute using bash instead of dash (note that bamboo launches everything as sh and you can't change that)

share|improve this question
    
    
That's in my question, it doesn't work – Rumbles Apr 11 '17 at 14:46
    
Extglobs are very much a bash-only feature. Part of something being a bash-only feature is that it... well... doesn't exist in shells that aren't bash (or similarly extended). – Charles Duffy Apr 11 '17 at 14:47
    
BTW, ${bamboo.scripts_folder} isn't a valid shell variable name. Is that being replaced with a value by something else (ie. code in a different language running a template to generate your script)? – Charles Duffy Apr 11 '17 at 14:48
up vote 1 down vote accepted

Is there a way? Yes.

Will you like it? Probably not.

set --; for d in */; do [ "$d" = "ignoreme/" ] && continue; set -- "$@" "$d"; done
rm -rf "$@"

That said, your other script isn't using dash at all -- at least, if it's called in accordance with its shebang. In that context, it just needs shopt -s extglob passed to enable extglobs, which are an extension that's turned off by default:

#!/bin/bash

# you almost certainly don't need this at all
[ -n "$BASH_VERSION" ] || [ -n "$ran_reexec" ] || ran_reexec=1 exec bash "$0" "$@"

shopt -s extglob    
rm -rf !(ignoreme)
share|improve this answer
    
It's run with dash, as bamboo runs everything with sh, and in ubuntu sh is a symlink to dash... – Rumbles Apr 11 '17 at 14:59
    
Just because it runs everything with sh doesn't mean everything that sh runs also uses sh. sh -c yourcommand will honor #!/bin/bash if that's what yourcommand starts with (and it's marked executable). Not true for sh yourcommand, of course, but that falls into the category of "don't do that" -- using sh yourcommand means your tool can't run yourcommand if it's a compiled binary, a Python script, or otherwise anything but a POSIX-compliant script. – Charles Duffy Apr 11 '17 at 15:00

I know I can do:

find . ! -name 'folder' -delete -maxdepth 1

Is there a way without find?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.