0

Reading some blogs finally I built a script to use SCP to backup remote files and after compress them:

#!/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

But when I run it receive this error:

# ./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"

Some other data on my system:

# 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
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
spwlite
  • 25
  • 5
  • 2
    `/usr/bin/sh` is almost *certainly* not going to be a C-shell. If you are expecting the script to run under `csh`/`tcsh` then you should use a correct shebang/`#!` line and/or run `tcsh testbk.sh` instead of `./testbk.sh`. – Etan Reisner Aug 26 '15 at 00:02
  • In addition, your "tcsh" script is not a valid tcsh script. What do you expect `set NOW [exec date "+%Y%m%d"]` to do? That looks more like TCL. – Martin Tournoij Aug 26 '15 at 06:17
  • in your code you are mixing `expect` syntax with `bash` syntax. `set IP [lindex $argv 0]` should be inside expect script if I am right. – Jakuje Aug 26 '15 at 11:16
  • All your comments are rigth, sorry but I´m not a programmer. I trying to built this script to perform a SCP connection and after a backup. Please help me with this questions: 1.- how pass values to a variable in bash? and 2.- how pass these same values to expect? – spwlite Aug 26 '15 at 16:24

1 Answers1

0

You've written this script more like a Tcl/Expect script than a bash script so you'll need to change your top line to

#!/usr/bin/expect

Since your script is running in a bash shell it won't recognize keywords like set which is how you're assigning values to your variables. Since the shell doesn't assign anything to these variables the variables aren't created, hence your error.

can't read "IP": no such variable
NickLamp
  • 862
  • 5
  • 10
  • how can I perform expect and bash tasks all in the same script? According some references using this: expect <<'END' was the correct form to reach my goal but now i´m stuck how pass argument to bash and then expect. – spwlite Aug 27 '15 at 14:10
  • You won't need to. With a little tweaking this should be a fully functioning expect script. However, if you really want to, you can run an expect script from a bash script http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command See this post for an example – NickLamp Aug 27 '15 at 14:20