Problems with expect and set variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problems with expect and set variables
# 1  
Old 07-15-2010
Problems with expect and set variables

I'm writing a script that'll send a time-stamp to my backup server. I create a file with the name of the current date, send it to my server with scp and rm the file from the local computer.

Individually these commands work fine and with a set name the expect scripts also work fine. The problem is when I want to set a variable file_name with the name of the file. Here is the code:

Code:
#!/usr/bin/expect -f
file_name=`date +"%F"`.txt
send -- "\r"
touch $file_name
send -- "\r"
set timeout -1
spawn scp $file_name admin@XXX.XXX.X.XXX:/share/destinatino
match_max 100000
expect -exact "admin@XXX.XXX.X.XXX's password: "
send -- "password\r"
expect eof
send -- "\r"
rm $file_name

I've tried many different things like "set file_name `date +"%F"`.txt" or put the whole `date +"%F"`.txt instead of the variable alltogether. This is just what it currently looks like, I'm hoping the problem is straightforward enough so I don't have to print all the errors I've got over the last hours.

For this code it says this:

[root@backup2 script]# ./script.exp
invalid command name "file_name=`date"
while executing
"file_name=`date +"%F"`.txt"
(file "./script.exp" line 3)
[root@backup2 script]#

Different things I've also tried is calling spawn for that set, also removing all the expect code makes it create a file with the right name and delete it. I've also tried putting the variable declaration infront of the including of expect at the begining but it gives the same problem.

I think I used to have spawn for for the touch command and rm at the end but removed it at some point during the testing. They should probably be put ack, doing so and removing all the send-commands and changing all the calls to file_name to just a string "file_name" makes it work but obviously not creating a file with the current date.

That makes me almost 100% certain, and I can't find how it should be, that I'm doing that file_name= thing wrong...

EDIT: Also witing that date-thing instead of the call to the $file_name sort of works but it creates two files like this:

[root@backup2 script]# ./script.exp
spawn touch `date +"%F"`.txt
spawn scp `date +"%F"`.txt admin@XXX.XXX.X.XXX:/some_server
admin@XXX.XXX.X.XXX's password:
`date 100% 0 0.0KB/s 00:00
+"%F"`.txt 100% 0 0.0KB/s 00:00
spawn rm `date +"%F"`.txt
[root@backup2 script]#

I know it's a problem wit the darn quotes but I can't figure out which ones it wants, nothing seem to make it execute the date function.

EDIT 2: Never mind, apparently it wats the entire file dedicated to expect, so I just put the expect part in one script and call it in the middle of creating and removing the file in another script.

Last edited by Ktesh564; 07-15-2010 at 07:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to set file transfer in binary mode in SFTP using Expect?

The below is my script. /usr/bin/expect<<EOD spawn /usr/bin/sftp -o Port=$PORT $USER@$HOST expect "sftp>" expect "password:" set timout 15 send "$password\r" expect "sftp>" send "lcd $remotedir\r" expect "sftp>" ... (1 Reply)
Discussion started by: Anilsaggu9
1 Replies

2. Shell Programming and Scripting

Problems with expect and sftp in bash

I'm having trouble with some automated sftp pulls. I'm using expect inside bash scripts and spawning SFTP. Some times the expect seems bog down. I have tried to put sleeps in my code to give everything time to work before I move on to next step but I till continue to get issues. For example when... (2 Replies)
Discussion started by: gosteen
2 Replies

3. Shell Programming and Scripting

Expect - bash and variables

I was wondering if anyone could provide some assistance. I trying to run an expect script within bash and get the results of a variable called RESULT. I Have tried a few things but none of them have worked. I know that the child process (the expect script) in this instance cannot set a variable... (6 Replies)
Discussion started by: ylafont
6 Replies

4. Shell Programming and Scripting

Expect and variables

I'm trying to make an expect function that will pass through a variable. /usr/bin/expect<<EOD spawn su - expect "Password: " send "$psswd\r" expect "#" send "/etc/init.d/network restart >>$log\r" expect "#" send "exit\r" EOD The $log passes through but my $psswd fails I know... (1 Reply)
Discussion started by: Lotheovian
1 Replies

5. Shell Programming and Scripting

Problems with expect

Hi, I am writing a script to enter into a list of servers and take the backup of the cronjobs on that servers... ============================== -bash-3.00$ expect spawn.exp ssh rcdn6-vm59-33 spawn ssh servers1 /CODE]==================== -bash-3.00$ cat spawn.exp #!/usr/bin/expect eval... (1 Reply)
Discussion started by: satishkumar432
1 Replies

6. Shell Programming and Scripting

Expect script Execution Problems .. Help!!!

Hi Guys, I am writing the expect script which take input from the txt file and check whether that file is present over the Sftp or not.If yes then delete other wise check the next one.. I am able to do comparison online for single file... Please also note still i didn't try to implement... (1 Reply)
Discussion started by: hackerdilli
1 Replies

7. UNIX for Dummies Questions & Answers

How to pass variables in Expect command?

Hi All, I need to frame a unix script to logon to a unix box. The credentials needs to be obtained from a property file on the same location. I am trying to use 'expect' and 'spawn' command to meet this req. When I am passing values, these commands are working fine. but when I am trying to... (3 Replies)
Discussion started by: mailkarthik
3 Replies

8. Shell Programming and Scripting

set Net:SSH:Expect timeout and set it again.

SSHing into a machine can take a few seconds, but after I'm in, the commands return quickly. I was wondering if the timeout setting can be changed once I'm logged into the machine. Does anyone know if this can be set on the fly? The problem here is, if I have to set timeout = 10, it'll take 10... (1 Reply)
Discussion started by: mrwatkin
1 Replies

9. Shell Programming and Scripting

problems installing expect utilty

Hi all, I am trying to install expect utility on RHEL 5. It is showing that we also need to install tcl also in order to run it. I have downloaded expect from Tucows Tucows Download - Download Expect 5.32.1 and tcl from SourceForge.net: Downloading ... Can someone please tell me... (5 Replies)
Discussion started by: vikas027
5 Replies

10. Shell Programming and Scripting

Expect script with file input problems

Hello, I am trying to write an expect script that will ssh into a large number of Cisco routers and add some commands via the cli. The script I wrote works great for one host however I have over 350 routers that this needs to be added to. The problem is I cannot get the script to read the file of... (1 Reply)
Discussion started by: meberline
1 Replies
Login or Register to Ask a Question