Results 1 to 6 of 6
Hi guys, it's my first time here, and unfortunatelly isn't for help someone, but i'll soon!
My issue is:
I'm doing a script to make some task but I would ...
- 02-12-2011 #1Just Joined!
- Join Date
- Feb 2011
- Posts
- 3
[Solved] Function to close the script
Hi guys, it's my first time here, and unfortunatelly isn't for help someone, but i'll soon!
My issue is:
I'm doing a script to make some task but I would like to know how can I do the part to check the bu.log ? Cause if the files exists it will send me an email with the log, but when the file doesnt exist it will send a blank emal
that why i would like to 'check the file' and if there isnt will end it. 
How can I edit the funcion to check if there isnt an arq.log and close the script?#!/bin/sh
x="$HOME/bu.log"
if [ $x ]; then #if the file exists will do
for file in *.log
do
i=${file%.*} #Will get me the name of the log without extension and send as subject
cat bu.log | mail -s "$i" MYEMAILANDMYPROVIDER
rm $HOME/arq.log
done
fi
ThanksLast edited by tiagokb; 02-13-2011 at 02:41 PM. Reason: solved
- 02-12-2011 #2Just Joined!
- Join Date
- Dec 2010
- Posts
- 16
First a remark: For my taste, I would not use the word file as a name for a variable because there is a Unix command with the same name. But it will not cause any trouble if the script is written correct. So, this is just a matter of taste.
Regarding your question to check whether a file exists, the simplest test is:
I would enclose the variable in quotes just in case the path or filename contains spaces.Code:if [ -e "$x" ]; then
The [ ] are a nicer looking equivalent for test. In bash, test is a builtin command, but it also exists as standalone program: /usr/bin/test. Therefore, this line would be equivalent the line above:
The options for test are documented in the manpage for test. Here is an excerpt from man test:Code:if `test -e "$x"`; then
Your script shall be executed by the old bourne shell sh. I am not very familiar with the differences between sh and bash but I think the [ ] cannot be used in sh. You may not get an error because on many Unixes all sh-scripts are executed by bash which knows the [ ]. To make things clean, I recommend to have your script officially executed by bash. So, I would write as first line:Code:-e FILE FILE exists -f FILE FILE exists and is a regular file ... -r FILE FILE exists and read permission is granted -s FILE FILE exists and has a size greater than zero
Another point: Are you sure that this line is really what you want:Code:#!/bin/bash
It is enclosed in the for-loop. So, bu.log will be sent by e-mail in each iteration of the for-loop. I guess you want to send each logfile. Then the line should be:Code:cat bu.log | mail -s "$i" MYEMAILANDMYPROVIDER
Code:cat "$file" | mail -s "$i" MYEMAILANDMYPROVIDER
I am sorry, I don't understand this question.
Anyway, I do not understand why arq.log is removed in each iteration of the for-loop. After the first iteration, arq.log will not be there any more and therefore rm will report an error in each following iteration.
- 02-12-2011 #3Linux Newbie
- Join Date
- Dec 2009
- Posts
- 241
Your script has some points I don't get:
$x is the name of the file bu.log
If I am right you forgot a parameter:
UNIX man pages : test ()
if [ -e $x ]; then #if the file exists will do
As far I know [ ] is the same as write test ... just more common to write []
Then with the "for" you read all lines of all .log files where the script gets executed ...
And for each line in this file you will send a mail with the line as subject
The text in the mail will always be bu.log
Thats what you looking for?!Code:#!/bin/sh #x="$HOME/bu.log" list="$HOME/mail.logs" #File with list of logs to be send for x in list do #For all entrys in this list if [ -e "$x" ]; then #if the file exists will do i=${file%.*} #Will get me the name of the log without extension and send as subject cat "$x" | mail -s "$i" MYEMAILANDMYPROVIDER #rm $HOME/arq.log fi done
Ps I haven't tested this script thatfor I've no idea if it's accually working.
- 02-12-2011 #4Just Joined!
- Join Date
- Feb 2011
- Posts
- 3
Thanks guys, i'll be more espefic

I did that but still one issue
This script gets a .log from a software and that same software give me a file called DIFFERENTNAME.txt - That's why as my subject the script just gets the DIFFERENTNAME; then I need to make a decision first :
If the file exists it will execute the script and send me the email with the name of .txt +the log content.
If the script doesn't find the log will just end.
And as the software makes a new log I need the function rm to remove it, thats why I added it
This script will be on crontab and executing in 10 min.
Edit2: Guys It's working but when I put it on contrab in the second time that crontab runs it just gives as subject on email blank space - should give the name of .txt
#!/Bin/bash
x="$HOME/softwarelog.log"
if [ -e "$x" ]; then
for file in *.txt
do
i=${file%.*}
cat $HOME/softwarelog.log | mail -s "$i" MYEMAILMYPROVIDER
rm $HOME/softwarelog.log
rm $HOME/*.txt
done
fiLast edited by tiagokb; 02-13-2011 at 01:45 AM.
- 02-13-2011 #5Just Joined!
- Join Date
- Dec 2010
- Posts
- 16
It has to be:
Code:#!/bin/bash
The softwarelog.log is removed during the first iteration of the for-loop. Therefore, you get an empty e-mail in all following iterations. This problem would be solved by moving the done line up. But then the softwarelog.log would be removed no matter whether the for-loop was entered or not. So, a softwarelog.log may be removed without being sent out as e-mail.
Therefore, let me doubt the for-loop. It sends an e-mail for each file ending with .txt with the contents of softwarelog.log in the mail-body. So, the e-mails differ only in the subject-line. Is this really what you want to do?
If it is enough to send the contents of softwarelog.log only once with all filenames of txt-files as subject line, then do this:
Code:#!/bin/bash x="$HOME/softwarelog.log" if [ -e "$x" ]; then mailsubject=$(ls $HOME/*.txt | sed -e 's/.txt//' -e 's/.*\///' | xargs) if [ "$mailsubject" ]; then cat "$x" | mail -s "$mailsubject" MYEMAILMYPROVIDER rm "$x" rm $HOME/*.txt fi fi
- 02-13-2011 #6Just Joined!
- Join Date
- Feb 2011
- Posts
- 3
Thank you so much Lahntaler, thar's what I'm looking for, one log multiple names as subject, worked great because the software log makes one .txt - each log generate only one .txt.
Thank you so much, cause I was tring to do as loop and it was wrong, in that way will just make one task and end the script


Reply With Quote
