LinuxQuestions.org
Visit Jeremy's Blog.
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 10-30-2009, 02:13 PM   #1
hellfire73
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Rep: Reputation: 15
bash scripting passing information between two files.


Hello everyone;

I am wondering if this situation is possible to do:

I have a bash script that upon completion mails the user to tell it is done, that is easy. Now however the boss wants to make the message sit out side the script as I currently have it in a echo pipe. the boss would like to have it so we have a generic message that is followed by the scripts name.

Right now I am have the file with the message and I can write the scripts name to the file and then read it in to mail however the scripts file name stays in the text file that has the generic message so each time I run it appends the file name so you get a bunch of file names after it is ran more than once.

I hope this is clear.
 
Old 10-30-2009, 02:28 PM   #2
bubnoff
Member
 
Registered: Jun 2009
Location: Northwest
Distribution: Slackware
Posts: 43

Rep: Reputation: 16
Pony up some code

It's difficult to know exactly what's going on if you don't
post the code in question and what exactly you want.

You could have your script rewrite the message each time or if
you have multiple scripts using the same template use a sed statement
to remove the appended file name. If the file name is always the last thing
appended than it's as easy as

Code:
sed '$d' filename
filename, of course, being the name of the file with your message. This
will delete the last line from whatever file you name.


Bubnoff

Last edited by bubnoff; 10-30-2009 at 02:31 PM. Reason: clarification
 
Old 10-30-2009, 02:42 PM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: SLED, openSUSE
Posts: 1,009

Rep: Reputation: 171Reputation: 171
Where msg is the file that contains your generic message:

Code:
#!/bin/bash

mailthis=$(cat msg)
mailthis="$mailthis \n\nScript to which this message relates $(basename $0)"


echo "$mailthis" | mail someone@somewhere.net
Maybe. Like previous poster said, hard to be sure based upon what you said.
 
Old 10-30-2009, 02:52 PM   #4
bubnoff
Member
 
Registered: Jun 2009
Location: Northwest
Distribution: Slackware
Posts: 43

Rep: Reputation: 16
Another thing you can do

If your message is a separate template you can just simply not
write to it in the first place. So the template remains unchanged and you
just send the output to mail.

Here's an example with sed where my template remains unchanged though
the output to mail contains my appended text:

Code:
sed '$a Sincerely,\nBuck Rogers' template | mail -s test fakeemail@fake.com
The words are appended after the last line of the template and sent to mail.

original file:
Code:
Hello kind sir,
Output to mail:

Code:
Hello kind sir,
Sincerely,
Buck Rogers
Does this help?

* UPDATE *
Above poster points out similar tactic ...different method.

Last edited by bubnoff; 10-30-2009 at 02:58 PM. Reason: My post is similar to one above.
 
Old 10-30-2009, 03:28 PM   #5
hellfire73
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Original Poster
Rep: Reputation: 15
You both are on the right track as to what I am trying to do...


the code is as follows for the script I am working on:

#!/bin/ksh
. /u/ucommands/u2tat
rm /tmp/blah.dat >/dev/null 2>&1

range1=1
range2=1
filename=$0
echo "This script will query the database for a range of projects and return the projects units that are marked as a A or I and the address of the unit."
echo ""
echo "Please enter the first project, NOTE if you only wish for one project to be displayed enter the same project number at both prompts."
read range1

echo "Enter the other project number."
read range2



if test -s /tmp/blah.dat
then
echo "File write successful!"
echo $filename
sed '$filename test' ./good.txt | mail -t myemail@mail.ca
else
echo "File write Failure..zero length file or no data to write"
fi


now the good.txt file is

the following program has ran: "this is where the name of the script is going to be"






bobnoff what is the $a in your statement I assume it the scripts name? when I run I get and error on the sed statement.

Last edited by hellfire73; 10-30-2009 at 03:29 PM.
 
Old 10-30-2009, 03:33 PM   #6
bubnoff
Member
 
Registered: Jun 2009
Location: Northwest
Distribution: Slackware
Posts: 43

Rep: Reputation: 16
Append

The 'a' is sed's "append" command and the dollar sign
refers to the last line of the file.

So it finds the last line then appends your text after the
last line.

The $0 variable is the name of your script.

Last edited by bubnoff; 10-30-2009 at 03:38 PM.
 
Old 10-30-2009, 03:40 PM   #7
hellfire73
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Original Poster
Rep: Reputation: 15
I keep getting the following when running the sed command you recommend:

./mailtest
sed: command garbled: $a test
 
Old 10-30-2009, 03:45 PM   #8
bubnoff
Member
 
Registered: Jun 2009
Location: Northwest
Distribution: Slackware
Posts: 43

Rep: Reputation: 16
You'd need to post the line it's complaining about.
It may be that I'm using BASH and you are using KSH.

I'll confess that I think the other poster's solution may
be the more elegant solution. I'll post code soon.

Bub
 
Old 10-30-2009, 03:48 PM   #9
hellfire73
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Original Poster
Rep: Reputation: 15
yea I don't use sed at all so I have a hard time with it past the very basics.

sed -a 'test' ./good.txt | mail -t andrew.nickless@crhc.ab.ca
 
Old 10-30-2009, 03:52 PM   #10
bubnoff
Member
 
Registered: Jun 2009
Location: Northwest
Distribution: Slackware
Posts: 43

Rep: Reputation: 16
Theoretically this should work. $0 contains the name of the script ( full path ).
Should append the name of the script to the end of your message if good.txt
is the message.

Code:
sed "$a $0" ./good.txt | mail -t myemail@mail.ca
edit:
escape the first dollar sign ( eg. sed "\$a $0" ./good.txt ) so it
gets interpreted correctly in a script.

Last edited by bubnoff; 10-30-2009 at 04:42 PM. Reason: forgot to escape the dollar sign for scripting purposes
 
Old 10-30-2009, 03:57 PM   #11
hellfire73
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Original Poster
Rep: Reputation: 15
./mailtest
Unrecognized command: ./mailtest
is what I get as an error..
 
Old 10-30-2009, 04:16 PM   #12
bubnoff
Member
 
Registered: Jun 2009
Location: Northwest
Distribution: Slackware
Posts: 43

Rep: Reputation: 16
This works for me:
Code:
#!/bin/bash
 echo "hello" >> msg
sed "\$a $0\n" < msg | mail -s test fake@fake.com
Using $(cat msg) like the above poster mentions is a good idea and will
get you the results you want as well.

Last edited by bubnoff; 10-30-2009 at 04:39 PM.
 
Old 10-30-2009, 04:23 PM   #13
hellfire73
LQ Newbie
 
Registered: Mar 2006
Posts: 23

Original Poster
Rep: Reputation: 15
Thank you for your help I have come to the conclusion that the version of the shell I am using is old enough to not let what you suggest work. I have to use what arizonagroovejet shared.

Thank you for bouncing some Ideas to me I thought I might have to play around with strings but it has been long enough for me doing it that I keep thinking I have to code like I would in C++!!!

you both helped me a lot.

Last edited by hellfire73; 11-02-2009 at 11:40 AM.
 
  


Reply

Tags
bash


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
Bash Scripting - Configuration files piercey Programming 8 10-17-2008 10:41 AM
how to burn/write files in a cd-r/rw using bash scripting jaepi Linux - Newbie 7 05-06-2007 07:50 PM
Bash Scripting: Editing external files trek413 Linux - Software 1 11-02-2006 04:11 PM
bash scripting - editing files brian0918 Linux - Newbie 2 07-01-2003 02:27 PM
bash scripting - editing files brian0918 Programming 1 06-30-2003 06:16 PM


All times are GMT -5. The time now is 10:38 PM.

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