Unix在expect脚本中创建和使用变量

Unix在expect脚本中创建和使用变量,unix,awk,ssh,expect,csh,Unix,Awk,Ssh,Expect,Csh,在我尝试自动访问远程计算机时, 我试图在expect脚本中创建和使用变量 我正在努力做到以下几点: #!/bin/csh -f /user/bin/expect<<EOF_EXPECT set USER [lindex $USER 0] set HOST [lindex $HOST 0] set PASSWD [lindex $PASSWD 0] set timeout 1 spawn ssh $USER@$HOST expect "assword:" send

在我尝试自动访问远程计算机时, 我试图在expect脚本中创建和使用变量

我正在努力做到以下几点:

#!/bin/csh -f
/user/bin/expect<<EOF_EXPECT
set USER     [lindex $USER 0]
set HOST     [lindex $HOST 0]
set PASSWD   [lindex $PASSWD 0]
set timeout 1
spawn ssh $USER@$HOST
expect "assword:"
send "$PASSWRD\r"
expect ">"
set list_ids (`ps -ef | grep gedit | awk '{ print $2 }'`)
expect ">"
for id in ($list_ids)
    send "echo $id\r"
end
send "exit\r"
EOF_EXPECT
#/垃圾箱/垃圾箱-f
/用户/bin/expect
  • shell和
    tcl
    变量都用
    $
    标记。shell正在扩展您的here文档的内容。你不会想要的
    csh
    没有
    $2
    的值,因此将其扩展为空字符串,awk命令最终变成
    ps-ef | grep gedit | awk'{print}'
    。这就是为什么要在输出中得到整行

    你这里的上下文有点混乱。如果希望将
    $
    转义到嵌入的awk命令,则需要从外部
    csh
    转义
    $
    。(这很可怕,但显然是
    csh

    一般来说,您不需要尝试合并csh和tcl命令/等等。这样可以极大地帮助您了解正在发生的事情

  • “未被承认”是什么意思?您是否收到任何其他错误(如来自
    set
    命令)
  • 我认为您正在寻找:

  • $env(list_id)
    是一个
    tcl
    变量。csh对它一无所知,这与任何事情都无关(除了上面第1点的问题,所以逃避它)。如果在运行
    tcl
    脚本的
    csh
    会话中导出
    list\u id
    ,则
    $env(list\u id)
    应在
    expect
    脚本中工作

    我认为,您也不希望
    ()
    set
    命令中的值附近。我相信它们是真的。如果您试图从该
    ps
    管道的(shell扩展)输出创建一个tcl列表,那么您需要:

    set list_ids [list `ps ....`]
    
    但正如我之前所说的,你并不真的希望像那样混合上下文

    如果您可以使用非
    csh
    shell,这可能也会有所帮助,因为
    csh
    通常并不好


    另外,如果您可以直接将expect脚本作为脚本文件编写,那么不将expect脚本嵌入csh脚本也会有所帮助。

    阅读此处对我帮助很大:

    下面几行就可以了,并回答所有问题:

    set list_ids [list {`ps -ef | grep gedit | awk '{print \$2 }'}]
    set i 0
    while {[lindex \$list_ids \$i] > 0} {
      puts [lindex \$list_ids \$i]
      set i [expr \$i + 1]
    }
    

    非常感谢。
    [list….]
    真的很有帮助。我不得不使用
    csh
    ,它是更大脚本的一部分
    foreach
    在我的
    expect
    脚本下无法识别,因此我在最后使用了
    。把我的答案贴在下面。
    
    set list_ids [list {`ps -ef | grep gedit | awk '{print \$2 }'}]
    set i 0
    while {[lindex \$list_ids \$i] > 0} {
      puts [lindex \$list_ids \$i]
      set i [expr \$i + 1]
    }