如何在破折号中 运行 "rm -rf !(folder)"

How to run "rm -rf !(folder)" in dash

我已经在 Bamboo 中编写了一个部署计划,其中 运行s 使用 sh 的代理上的所有脚本,并且由于这些代理 运行 ubuntu 14.04 等同于破折号.

我想执行以下步骤:

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

这应该删除除变量中的那些之外的所有 files/folders。

这在 bash 中工作正常,但是当我 运行 这在破折号中时它给我:

dash: 2: Syntax error: "(" unexpected

我尝试 运行 在脚本开头使用以下命令强制它 运行 为 bash:

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

虽然这在过去似乎对我有用,但在这种情况下由于某种原因,它似乎并没有,因为我得到:

line 8: syntax error near unexpected token `('

我尝试将我的脚本更新为:

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

echo "Later [=16=]"

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

我希望 echo 语句会打印出当前 运行ning 的 shell,但我却得到:

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)'

这并没有让我继续下去。任何人都可以建议 "right" 使用破折号 运行 的方法吗?或者 "right" 使用 bash 而不是破折号执行此操作的方法(请注意,bamboo 以 sh 启动所有内容,您无法更改它)

我知道我能做到:

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

有没有find的方法?

有办法吗?是的

你会喜欢吗?应该不是。

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

也就是说,您的其他脚本根本没有使用破折号 -- 至少,如果它是根据其 shebang 调用的。在这种情况下,它只需要传递 shopt -s extglob 来启用 extglobs,这是一个默认关闭的扩展:

#!/bin/bash

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

shopt -s extglob    
rm -rf !(ignoreme)