【问题标题】:Passing parameter into script with tcsh and expect使用 tcsh 将参数传递给脚本并期望
【发布时间】:2015-11-19 20:42:28
【问题描述】:

看了一些博客,最后我构建了一个脚本来使用 SCP 备份远程文件并在压缩后:

#!/usr/bin/sh

set IP [lindex $argv 0]
set EQ [lindex $argv 1]
set NOW [exec date "+%Y%m%d"]

mkdir /tftpboot/tmp/$EQ
#
expect <<'END'
spawn scp -rpc "sadmin@$IP:/usr/local/etc/*" /tftpboot/tmp/$EQ
        expect {
                -re ".*es.*o.*" {
                exp_send "yes\r"
                exp_continue
                }
                -re ".*sword.*" {
                exp_send "Ti3OlEwP8h4\r"
                }
        }
        interact
END
#
tar cf - /tftpboot/tmp/$EQ | 7za  a  -si /tftpboot/2015/$EQ.$NOW.tar.7z
#
rm -rf /tftpboot/tmp/$EQ

但是当我运行它时收到这个错误:

# ./testbk.sh 1.1.1.1 TestServer  > log.txt
mkdir: Failed to make directory "/tftpboot/tmp/"; File exists
can't read "IP": no such variable
    while executing
"spawn scp -rpc "sadmin@$IP:/usr/local/etc/*" /tftpboot/tmp/$EQ"

我系统上的一些其他数据:

# echo $tcsh
6.08.00
# echo $version
tcsh 6.08.00 (Astron) 1998-10-02 (i386-sun-solaris) options 8b,nls,dl,al,rh,color
# uname -a
SunOS CSPC2 5.10 Generic_144489-17 i86pc i386 i86pc
# cat /etc/release
                        Solaris 10 6/06 s10x_u2wos_09a X86
        Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
                        Use is subject to license terms.
                            Assembled 09 June 2006

【问题讨论】:

  • /usr/bin/sh 几乎肯定不会是 C-shell。如果您希望脚本在 csh/tcsh 下运行,那么您应该使用正确的 shebang/#! 行和/或运行 tcsh testbk.sh 而不是 ./testbk.sh
  • 此外,您的“tcsh”脚本不是有效的 tcsh 脚本。你希望set NOW [exec date "+%Y%m%d"] 做什么?这看起来更像 TCL。
  • 在您的代码中,您将expect 语法与bash 语法混合在一起。如果我是对的,set IP [lindex $argv 0] 应该在期望脚本中。
  • 你所有的 cmets 都是正确的,抱歉,我不是程序员。我试图构建这个脚本来执行 SCP 连接并在备份之后。请帮我解决这个问题:1.-如何将值传递给bash中的变量?和 2.- 如何将这些相同的值传递给期望?

标签: expect scp tcsh solaris-10


【解决方案1】:

您编写的此脚本更像是 Tcl/Expect 脚本而不是 bash 脚本,因此您需要将顶行更改为

#!/usr/bin/expect

由于您的脚本在 bash shell 中运行,因此它无法识别诸如 set 之类的关键字,这是您为变量赋值的方式。由于 shell 没有为这些变量分配任何东西,因此没有创建变量,因此您的错误。

can't read "IP": no such variable

【讨论】:

  • 如何在同一个脚本中执行所有期望和 bash 任务?根据一些使用这个的参考:期望
  • 你不需要。稍作调整,这应该是一个功能齐全的期望脚本。但是,如果你真的想要,你可以从 bash 脚本运行一个期望脚本 stackoverflow.com/questions/4780893/… 示例见这篇文章
猜你喜欢
  • 2017-06-20
  • 2017-09-02
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2012-03-15
相关资源
最近更新 更多