Mac OS Expect命令用于删除配置文件

时间:2017-02-07 04:14:09

标签: bash macos expect

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

/usr/bin/profiles -D

它要求用户输入“你确定要删除所有配置文件吗?[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]:""

任何人都可以帮助我们,我们真的很感激。

2 个答案:

答案 0 :(得分:2)

  1. Tcl的[...]是命令替换语法,就像Bash中的$(...)一样。
  2. [...]对于glob模式(或正则表达式)也很特殊。
  3. 所以你应该这样写:

    /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]:}
    

答案 1 :(得分:0)

首先,您不需要在此处使用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