Mac OS 期待删除配置文件的命令

Mac OS Expect Command for Profile Removal

我们正在替换我们公司使用的配置文件,我们需要 运行 一个命令来删除旧的配置文件。

/usr/bin/profiles -D

它要求用户输入 "Are you sure you want to delete all configuration profiles? [y/n]:"

我们正在尝试自动执行此过程,并研究了 expect 命令,但无法将其发送到 运行。

/usr/bin/expect -f - <<EOD

spawn /usr/bin/profiles -D 

expect "Are you sure you want to delete all configuration profiles? [y/n]:"
send "y\n"
EOD

但是当我们尝试 运行 时,我们得到了这个错误。

 sudo /Users/gpmacarthur/Desktop/test.sh 
spawn /usr/bin/profiles -D
invalid command name "y/n"
    while executing
"y/n"
    invoked from within
"expect "Are you sure you want to delete all configuration profiles? [y/n]:""

谁能帮助我们,我们将不胜感激。

  1. Tcl 的 [...] 是命令替换语法,就像 Bash 中的 $(...)
  2. 并且[...]对于glob模式(或正则表达式)也是特殊的。

所以你应该这样写:

/usr/bin/expect -f - << 'QUOTED-EOD'
spawn /usr/bin/profiles -D 

expect "Are you sure you want to delete all configuration profiles? \\[y/n]:"
send "y\n"

expect eof;  # This is required!
QUOTED-EOD

或者您可以使用 Tcl 的 {...} 引用样式(如 Bash 的单引号 '...'):

expect {Are you sure you want to delete all configuration profiles? \[y/n]:}; # The `[' still needs to be escaped.

或者只是

expect {\[y/n]:}

首先,这里不需要使用expect。您可以只使用以下标志:

-f, auto confirm any questions

/usr/bin/profiles -fD

因为我已经输入了 expect 解释:

方括号被评估为命令替换,也需要在正则表达式匹配中转义。您可以使用 {} 符号来避免这种情况。

/usr/bin/expect -f - <<EOD

spawn /usr/bin/profiles -D 

expect {Are you sure you want to delete all configuration profiles? \[y/n]:}
send "y\n"
EOD