LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script (http://www.linuxquestions.org/questions/programming-9/bash-script-519327/)

coolbeansdude51 01-14-2007 09:11 PM

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!

tidiman07 01-14-2007 09:45 PM

1. study bash scripting. link

2. draft expermental script.

3. test, then post Qs at LinuxQuestins

matthewg42 01-14-2007 09:55 PM

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.

coolbeansdude51 01-14-2007 11:11 PM

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.

coolbeansdude51 01-15-2007 12:20 AM

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


igorc 01-15-2007 12:22 AM

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.

coolbeansdude51 01-15-2007 12:32 AM

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.


matthewg42 01-15-2007 12:35 AM

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?

coolbeansdude51 01-15-2007 12:41 AM

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

gsr_kashyap 01-17-2011 05:10 AM

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??

Nominal Animal 01-18-2011 12:16 PM

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


All times are GMT -5. The time now is 02:48 AM.