【发布时间】: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