I want to schedule a file copy from sftp server. public key is not allowed so I know only one way to use expect.
Problem is that file name is changing every day, but file name is actually date+.csv so I can schedule it.
But unable to set variable file name...
I want to do something like this in 2 scripts.
file name=Date %Y%m%d
connect to sftp server get file name.
My bast efforts are like as below.
my *.sh script =
#!/bin/sh
file=$(date --date='-2 days' +%Y%m%d.csv)"
# looking for something like this (20121031.csv)
export file
expect /home/desk4/task/sftp.exp
My expect script located on "/home/desk4/task/sftp.exp"
spawn /usr/bin/sftp user@server.com
expect "user@server.com's password:"
send "password"
send "\r"
expect "sftp>"
spawn "get $file \r"
expect "sftp>"
send "bye \r"
Error
./sftp.sh: 3: export: 20121031.csv: bad variable name
#!/bin/sh
file=$(date)
export $file
expect /home/harshit/Desktop/1/sftp.exp
======================================= Error = ./sftp.sh: 3: export: 2: bad variable name
--end--
Thanks for your replay..
The "bad variable name" error is due to you exporting the value of the variable, not the name of the variable: export $file
is wrong, use export file
In expect, you access environment variables through the global env
array, so use $env(file)
not $file
Go ahead and issue export and variable in the same line:
$export filename=`date +'%F' | sed 's/[-]//g'`.csv
$echo $filename
20121103.csv
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.