PDA

View Full Version : cron scripting/bash scripting



coolbeansdude51
15th January 2007, 03:53 AM
Hello all,

I am wondering how to script something. I think this is possible.

Ok so 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.

Can anyone give me some direction of where I could look. I looked though the forums but I can't seem to find anything.

Thanks for the help!

bmvbab
15th January 2007, 07:08 AM
#!/bin/bash
DSKFILE="/home/user/Desktop/filename"
cat $DSKFILE > /tmp/file
date >> /tmp/file

HOST=192.168.50.2
ftp $HOST<<ENDOFUPLOAD
uname
password
put /tmp/file
bye
ENDOFUPLOAD


That should help...as an alternative to ftp u could try scp or rsync
Please refer these http://www.shelldorado.com/

coolbeansdude51
15th January 2007, 07:35 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?

bmvbab
15th January 2007, 08:22 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 /tmp/file_name.txt
bye
ENDFTP


Does that work?

More like the below:

#!/bin/bash
DATE=$(date +%y/%m/%d_%r);sed 's/Date Updated.*$/Date Updated '$DATE'/g' /tmp/dummy/stat.txt > /tmp/file_name.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

Oh btw...u are transferring an ascii file...why would u need to do a 'bin'? u could remove that.

coolbeansdude51
15th January 2007, 06:17 PM
ok.

Now here is a problem. The program that creates the text file deletes the date when ever it updates that file. So how can I put in an if then statment so that if the date updated is not found then place it in there.

Thanks so much for your help so far! Its gonna be sweet when it works!

erroneus
16th January 2007, 04:04 AM
Well since the file is the same format each time, I would suggest something more simple:

Copy down just the first few lines of the file that will not contain a date (which is always at the end) and then always append the date as you require.

coolbeansdude51
16th January 2007, 04:10 AM
This is what I did and it works great



#! /bin/bash
DATE=$( date "+%A %B %d %r")
echo -n Last uploaded at: $DATE > /folding/unitinfo2.txt
cat /folding/unitinfo2.txt >> /folding/unitinfo.txt
ftp -inv hostipaddress <<ENDFTP
user username password
cd /www
lcd /folding
put unitinfo.txt
bye
ENDFTP


I set that to run every hour and it uploads it to my server and then I use an iframe to format it so that it looks sweet in a html document.

Thanks for the help!

bmvbab
16th January 2007, 04:13 AM
ok.

Now here is a problem. The program that creates the text file deletes the date when ever it updates that file. So how can I put in an if then statment so that if the date updated is not found then place it in there.

Thanks so much for your help so far! Its gonna be sweet when it works!


#!/bin/bash
DATE=$(date +%y/%m/%d_%r);sed 's/Date Updated.*$/Date Updated '$DATE'/g' /tmp/dummy/stat.txt > /tmp/file_name.txt
if [ `grep -c "Date Updated" /tmp/file_name.txt` = 0 ]
then
echo "Date Updated $DATE" >> /tmp/file_name.txt
fi
/bin/ftp -inv ip_address_of_target_station<<ENDFTP
user user_name user_password
cd folder_on_target_station
lcd folder_on_local_station
put file_name.txt
bye
ENDFTP


The if statement checks if the resultant file contains "Date Updated". If it doesnt then the file is appended with the required data.