LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-02-2012, 10:48 AM   #1
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Rep: Reputation: Disabled
How to "export" out put of command "date"?


Hi,

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..
 
Old 11-02-2012, 11:08 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 742
Blog Entries: 2

Rep: Reputation: 193Reputation: 193
file_name=$(date +%Y%m%d)

export file_name # no dollar

Last edited by linosaurusroot; 11-02-2012 at 11:09 AM.
 
1 members found this post helpful.
Old 11-02-2012, 11:20 AM   #3
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by linosaurusroot View Post
file_name=$(date +%Y%m%d)

export file_name # no dollar
Thanks,

By this way my 1st script executing successfully.
but getting error in 2nd script.

Error is as below.

###start###

sftp> can't read "file": no such variable
while executing
"spawn "get $file \r""
(file " /home/desk4/task/sftp.exp" line 7)

###end###

Thanks for your replay.
 
Old 11-02-2012, 12:02 PM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
If you want to list out files that changing daily & saved in date.csv format then you use:
Quote:
day=$(date +%d) OR echo "Enter day: "; read day
month=$(date +%m)
year=$(date +%Y)
format=$($year$month$day)
Then you can use this format variable and search for approprite files, for any required day:
Quote:
find /path/to/file -name "<file>.$format* -type f -exec ls -la {} \; > /tmp/fileslist.txt
And so on...
 
Old 11-02-2012, 12:10 PM   #5
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
If you want to list out files that changing daily & saved in date.csv format then you use:

Then you can use this format variable and search for approprite files, for any required day:

And so on...
Thanks for your replay..

My task is to copy a file every day using crontab.
Where file name is variable, but it will be name in date format like YYYYMMDD.csv.

My task is to create a script what can copy yesterdays file from sftp server to local maching.
And this script need to run every day.

cant use key, so have to use expect.
 
Old 11-02-2012, 01:16 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
If you just want to copy files of specified format then do as:
Quote:
#!/bin/bash
day=$(date +%d) # It can be used to take files for today
prevdate=$(date --date="1 day ago" | cut -d" " -f3) # It can be used to select files for previous day
month=$(date +%m)
year=$(date +%Y)
format=$($year$month$day) OR format=$($year$month$prevday)
fileslist=/tmp/fileslist.txt
orgpath=/path/to/file # Specify location of those files to be copied
find /path/to/file -name "<file>.$format* -type f -exec ls -la {} \; > $fileslist
while read -r file
do
cp -p $file /path/to/copy # Specify copy path here in /path/to/copy
echo "Copy for $file done!"
done < $fileslist
Then test the script and then add an entry for this script in cron.
(Note: This is a sample script, please make changes according to your working environment wherever needed.) Hope it will help you!
 
Old 11-03-2012, 04:14 AM   #7
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Okay Thanks for your efforts,

BTY
I got solution, is as below.
I will have to say Thanks to Rakesh for this support.


####start####
#!/usr/bin/expect -f
set DATE [exec date --date=yesterday +"%Y%m%d".csv]
spawn sftp user@11.22.33.44
match_max 100000
expect -exact "user@11.22.33.44's password:"
send -- "abcdedfh"
expect "sftp>"
send "get $DATE \r"
expect "sftp>"
send "bye \r"
interact
####end####
 
  


Reply

Tags
date, expect, export, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 06:41 PM
How to change " setenv IDLUTILS_DIR ~/idl/idlutils" to "export"?? msbstar Linux - Newbie 3 02-05-2011 04:23 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 09:29 PM


All times are GMT -5. The time now is 06:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration