简体   繁体   中英

How to “export” out put of command “date”? (linux / expect / sftp)

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.

script 1 (sh script)

file name=Date %Y%m%d

export file name

script 2 (exp script)

connect to sftp server get file name.

exit

My bast efforts are like as below.

my *.sh script =

Start

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

End

My expect script located on "/home/desk4/task/sftp.exp"

Start

!/usr/bin/expect

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"

End

Error

./sftp.sh: 3: export: 20121031.csv: bad variable name

When I have update for testing purpose sh script like below...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM