Getting error while using date as a variable in expect

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Getting error while using date as a variable in expect
# 1  
Old 08-03-2018
Getting error while using date as a variable in expect

Hi
getting error when i am saving date command output in a variable and then using the same variable in expect
Code:
#!/usr/bin/expect
set DATE [exec date -d "1 day ago" +%Y_%m_%d]
spawn sftp AUT#XX.XX.XX.XX
spawn echo $DATE
expect "password:"
send "AT123\n"
expect "sftp>"
send "cd /home/ \r"
expect "sftp>"
send "LOGS_$DATE.tar.gz \r"
expect "sftp>"
send "quit \r"

below is the error I am getting
Code:
spawn sftp AUT@XX.XX.XX.XX
spawn echo 2018_08_02
2018_08_02
send: spawn id exp5 not open
    while executing
"send "AT123\n""
    (file "./ns.sh" line 6)


pls let me know where I am doing wronge
# 2  
Old 08-03-2018
Does it work without echoing the $DATE variable?

Does it work when echoing the $DATE variable before the sftp command?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-03-2018
script is not working whether I do
Code:
echo

or not.
I have using echo in my script to just check if I m using the date command
Code:
set DATE [exec date -d "1 day ago" +%Y_%m_%d]

correct or not.


although it is working and can be seen in error o/p
Code:
2018_08_02

------ Post updated at 03:31 PM ------

Hi


also I have noticed if I hash out below command and then try to sftp the another file it is working successfully
Code:
set DATE [exec date -d "1 day ago" +%Y_%m_%d]

------ Post updated at 03:44 PM ------

Hi Rudic,


your suggestion works.
Code:
 Does it work without echoing the $DATE variable?
 Does it work when echoing the $DATE variable before the sftp command?

when I hash out the
Code:
 spawn echo $DATE

script start working.
can you tell me why this was causing the problem
initially I misunderstood your suggestion.
# 4  
Old 08-03-2018
Sorry, no, I can't. Just guessing I'd say that the second spawn closes the first (sftp) one, and echo doesn't expect an answer / input.
# 5  
Old 08-03-2018
HI RUDIC,
thx for help.


I have noticed another issue while running the script.
transferring of file getting closed in between. see below
Code:
LOGS_2018-08-02.tar.gz                23%   10MB   1.7MB/s   00:18 ETA

why this happening so I am not sure .. still there is a problem.
as I can transfer the file 100% when do manually . see below
Code:
LOGS_2018-08-02.tar.gz                100%   42MB 909.5KB/s   00:47

# 6  
Old 08-03-2018
Network error / glitch? Is that reproducible? Any error messages?
# 7  
Old 08-06-2018
Hi


I have resolved the above error by setting timeout time.
now the script is running fine but when I tried to run through it is showing error and state file not found


script mentioned below
Code:
#!/usr/bin/expect
set DATE [exec date -d "1 day ago" +%Y-%m-%d]
spawn sftp AUT#XX.XX.XX.XX
#spawn echo $DATE
expect "password:"
send "AT123\n"
expect "sftp>"
send "cd /home/ \r"
expect "sftp>"
send "LOGS_$DATE.tar.gz\r"
set timeout 2000
expect "sftp>"
send "quit \r

getting below error when script run through cron
Code:
# log
+ log
spawn sftp AUT@xx.xx.xx.xx
Connecting to xx.xx.xx.xx...
AUT@xx.xx.xx.xx's password: 
sftp> cd /home 
sftp> put LOGS_2018-08-05.tar.gz
stat LOGS_2018-08-05.tar.gz: No such file or directory

although the file is present on the path

Last edited by scriptor; 08-06-2018 at 07:43 AM.. Reason: typo
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 replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Shell Programming and Scripting

Expect Redirecting o/p to an Variable

I have a password reset expect script which stores all the op to an file. I need to check the whether password is successfully changed by greping out the file and storing the o/p to a variable. But we try to print the variable , its shows only the command instead of its o/p. ... (2 Replies)
Discussion started by: sudharson
2 Replies

3. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

4. Shell Programming and Scripting

Python get the expect value from a variable

the value of the variable is yes group=bsp_16 keyword="82599" test_var="-a xxx -b yyy" I want to get output with -a xxx -b yyy (0 Replies)
Discussion started by: yanglei_fage
0 Replies

5. Shell Programming and Scripting

LINUX ---> Add one date to a date variable

Hi All, I am trying to add one day to the busdt variable i am extracting from a file (DynamicParam.env). I am deriving the busdt as below. Is there any function which I can use to derive as below. If busdt=20120910, then the new derived variable value should be 20120911 If... (2 Replies)
Discussion started by: dsfreddie
2 Replies

6. Shell Programming and Scripting

How to pass variable with spaces from shell to expect?

I need call expect script from shell script and pass values some of which could contain space. How to make expect to treat such values as one variable? (1 Reply)
Discussion started by: urello
1 Replies

7. Shell Programming and Scripting

Assigning last line to variable in expect

As part of an expect script, I have to convert a strange user ID to a conventional UNIX ID. To do this, I read the contents of a file and do a little awk magic. Here's that bit of the expect script: send "awk 'NF == 10 && \$NF == strange_user_id {print \$(NF-2)}' file_with_my_info\r" expect... (0 Replies)
Discussion started by: treesloth
0 Replies

8. Shell Programming and Scripting

passing variable to expect

Please tell me how to pass variable "a b c" to expect from the shell script 1/ example of input file # cat in-file var1 var2 a b c var4 2/ # this is my script - how to pass "a b c" as single variable ? cat in-file | while read x do my-expect x done 3/ # expect script - how to... (0 Replies)
Discussion started by: breaktime123
0 Replies

9. Shell Programming and Scripting

Transfer variable to an expect function

Hi There, I try to transfer a variable from the script to a function which use expect, but I don't succed. #!/bin/sh HPPASS1="$2" send_command() { echo "spawn ssh login@10.10.10.10" echo 'set password ' echo 'sleep 1' echo 'expect "*assword:*"'... (5 Replies)
Discussion started by: sylvainkalache
5 Replies

10. Shell Programming and Scripting

How to check date variable according to the current date?

Hi folks, I need to write a script that should activate a process according to the current hour. The process should be activatet only if the hour is between midnight (00:00) and 07:00. How should I create the condition? Thanks in advance, Nir (2 Replies)
Discussion started by: nir_s
2 Replies
Login or Register to Ask a Question