LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2007, 09:11 PM   #1
coolbeansdude51
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Rep: Reputation: 0
Question Bash script


First off Hello forum. Nice to meet you all.

My first question! W00T

OK so here we go.

This is what I want to do ... I want to get a text file from my desktop then edit the text file by adding the current time and date. Then ftping the file to a host. This needs to happen like every hour or so. Of course I am going to have to use a corn job but I have no idea how to start.

Thanks for the help in advance!
 
Old 01-14-2007, 09:45 PM   #2
tidiman07
Member
 
Registered: Apr 2006
Distribution: Kubuntu 8.04
Posts: 129

Rep: Reputation: 15
1. study bash scripting. link

2. draft expermental script.

3. test, then post Qs at LinuxQuestins
 
Old 01-14-2007, 09:55 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
OK there are three things - modifying the file, automated ftp, cron jobs.

Some questions about the file modification of the file:
  1. What sort of file is it - a plain text file?
  2. Do you want to append the date, or replace an existing date?

The FTP transfer would be easier to automate using scp. This would also be much more secure, although there is a CPU load which is noticeable for very large files if you have a very fast network. Does it absolutely have to be FTP?

Once you've solved the first two parts, it's just a matter of putting the commands to do them in a file, making it executable, and adding a cron entry to run it. Plenty of examples of that in these forums or elsewhere.
 
Old 01-14-2007, 11:11 PM   #4
coolbeansdude51
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42
OK there are three things - modifying the file, automated ftp, cron jobs.

Some questions about the file modification of the file:
  1. What sort of file is it - a plain text file?
  2. Do you want to append the date, or replace an existing date?

The FTP transfer would be easier to automate using scp. This would also be much more secure, although there is a CPU load which is noticeable for very large files if you have a very fast network. Does it absolutely have to be FTP?

Once you've solved the first two parts, it's just a matter of putting the commands to do them in a file, making it executable, and adding a cron entry to run it. Plenty of examples of that in these forums or elsewhere.
1. It is a plain text file.
2. Replacing a date.

And ... it has to be ftp.
 
Old 01-15-2007, 12:20 AM   #5
coolbeansdude51
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by tidiman07
1. study bash scripting. link

2. draft expermental script.

3. test, then post Qs at LinuxQuestins
Code:
#!/bin/bash
DSKFILE="/folding/unitinfo.txt"
cat $DSKFILE > /folding/unitinfo.txt
date >> /folding/unitinfo.txt

HOST=72.9.255.178
ftp $HOST<<ENDOFUPLOAD
jo bob
password
put /
bye
ENDOFUPLOAD
 
Old 01-15-2007, 12:22 AM   #6
igorc
Member
 
Registered: May 2005
Location: Sydney, Australia
Distribution: Ubuntu 5.04, Debian 3.1
Posts: 74

Rep: Reputation: 15
Hi,

Here is how to automate the ftp in a script:

#!/bin/bash

/bin/ftp -inv ip_address_of_target_station<<ENDFTP
user user_name user_password
cd folder_on_target_station
bin
lcd folder_on_local_station
put file_name.txt
bye
ENDFTP


You should substitute all the file names, addresses and path to the ftp for your box.

For the date replacement you should supply more info about the date format you want to replace. Also read the man pages for sed and tr commands and might be able to add the date replacement part in the above script by your one.

To schedule the script in the crontab every hour, type this as root or logged in as the user that should run the script:

# crontab -e

and enter something like this:

00 * * * * full_path_to_the_script > /dev/null 2>&1

or to run the script as a root:

00 * * * * su - root -c full_path_to_the_script >/dev/null 2>&1

I hope this helps.

Last edited by igorc; 01-15-2007 at 12:23 AM.
 
Old 01-15-2007, 12:32 AM   #7
coolbeansdude51
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks alot.

Ok so here is the date command that I want to use.
Code:
Date=$(date +%y/%m/%d_%r)
Here is copy of the text file.

Current Work Unit
-----------------
Name: p2124_lambda_5way_melt_4_10011
Download time: January 9 01:34:51
Due time: April 29 01:34:51
Progress: 71% [|||||||___]

Date Updated 11/07/06 12:06 AM -- THIS IS THE PART I WANT TO EDIT


So here is the script I have so far.
Code:
#!/bin/bash
date=$(date +%y/%m/%d_%r)
awk '/Date/ $Date' /tmp/dummy/stat.txt
/bin/ftp -inv ip_address_of_target_station<<ENDFTP
user user_name user_password
cd folder_on_target_station
bin
lcd folder_on_local_station
put file_name.txt
bye
ENDFTP
Does that work?

Quote:
Originally Posted by igorc
Hi,

Here is how to automate the ftp in a script:

#!/bin/bash

/bin/ftp -inv ip_address_of_target_station<<ENDFTP
user user_name user_password
cd folder_on_target_station
bin
lcd folder_on_local_station
put file_name.txt
bye
ENDFTP


You should substitute all the file names, addresses and path to the ftp for your box.

For the date replacement you should supply more info about the date format you want to replace. Also read the man pages for sed and tr commands and might be able to add the date replacement part in the above script by your one.

To schedule the script in the crontab every hour, type this as root or logged in as the user that should run the script:

# crontab -e

and enter something like this:

00 * * * * full_path_to_the_script > /dev/null 2>&1

or to run the script as a root:

00 * * * * su - root -c full_path_to_the_script >/dev/null 2>&1

I hope this helps.
 
Old 01-15-2007, 12:35 AM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Is the date on the last line of the file? If so, we have a nice easy job identifying the line... We could use sed to do the replacement. Here's a little shell script which would do it, assuming the date is on the last line, is the first date on that line, and is on YYYY-MM-DD format (my favorite date format - no confusion between British and American "day-or-month-first" silliness, plus dates in this format sort in dictionary sort order, which is super-useful!):

Code:
#!/bin/bash

file=$HOME/Desktop/file_to_work_on
today=$(date +%Y-%m-%d)
sed -r -i "$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/$today/" "$file"
Some explanation:

The file= line should be obvious - we're going to use the filename several times - we might as well put the name in a variable, and then if we need to change it, there's only one place to modify.

The today= line calls the date command with a format string to get YYYY-MM-DD format, and assigns the result to the "today" variable. In general, if you want to take the output of a command, and use it as part of another command, that's how you do it - using the $(command) syntax. You might also see `backticks` used for this purpose, but I prefer $() because it's less easily confused (people always confuse ` with ' - they're very different to the shell! Also $() is nestable.

Finally, the hard bit. sed.

Sed is the stream editor. It's a pretty simple program, but is phenomenally useful. It reads input, does some operations on it a line at a time, and prints the output (or when the -i option is used, as we have, saves the changes to the input file).

Firstly, before sed it actually run, the shell looks at the arguments to sed,
Code:
-r
-i
"$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/$today/"
"$file"
and expands anything it sees which needs expanding. This includes variables with the $ symbol before them, so $today takes the value of the variable (which we set in the previous line), and file as well. Quotes are stripped, but the shell has already separated the arguments, so spaces in the arguments stay in the arguments - they do not become delimiters (that was the purpose of quoting them in the first place)

So the arguments become:
Code:
-r
-i
$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/2007-01-15/
/home/username/Desktop/file_to_work_on
Sed's syntax (which you can read about the in the sed manual page) is such that first argument after any options is treated as a sed command to perform on the input, which is read from files named in any subsequent arguments.

The argument beginning "$ s/" means "on the last line of the input file, substitute the first occurrence of four digits, followed by a hyphen, followed by two digits, followed by a hyphen followed by two digits, with 2007-01-15". This may seem cryptic. If you want to understand it (and it IS worth it), google for "regular expressions", and "sed tutorial".

So the question is - does this fit your needs?
 
Old 01-15-2007, 12:41 AM   #9
coolbeansdude51
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
#!/bin/bash
today=$(date +%Y-%m-%d-%r)
sed -r -i "$ s/[0-9]{4}-[0-9]{2}-[0-9]{2}/$today/" "$file"
/bin/ftp -inv 192.168.1.1<<ENDFTP
user user_name user_password
cd /www/
bin
lcd /folding
put unitinfo.txt
bye
ENDFTP

How is that?
 
Old 01-17-2011, 05:10 AM   #10
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Rep: Reputation: 15
I was trying to put the
"/bin/ftp -inv 192.168.1.1<<ENDFTP
user user_name user_password
cd /www/
bin
lcd /folding
put unitinfo.txt
bye
ENDFTP "

in a function and trying to call it in for loop but i get syntax error:unexpected end of file
wht could be the problem??
 
Old 01-18-2011, 12:16 PM   #11
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
ENDFTP cannot be indented, it must start at the first column. If you use <<-ENDFTP then you can indent the ending ENDFTP with tabs (but not spaces).

For the date update, I'd recommend using (if format +%y/%m/%d_%r is the desired one)
Code:
sed -e '/update/ s|^.*$|Date updated '"`date '+%y/%m/%d_%r'`"'|' -i /folding/unitinfo.txt
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 06:20 AM.
 
  


Reply

Tags
bash, cron, script



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
Bash script hangs upon starting another script in the background masea2 Linux - Software 4 11-13-2006 05:18 AM
building a bash script from an install script paranoid times Programming 6 07-29-2006 03:24 AM
Bash script - executing a script through subdirectories bubkus_jones Programming 5 04-24-2006 05:05 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration