Log FILES in BASH scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Log FILES in BASH scripting
# 1  
Old 12-05-2007
Log FILES in BASH scripting

Hi
Im new to BASH scripting, I have created a simple script
Code:
#! /bin/sh
LOCAL=/home/scanner

NOW=$(date +"%b-%d-%y")

LOGFILE="log-$NOW.log"

cd $LOCAL

echo 'Start';

echo $LOGFILE
touch $LOGFILE




this creates a log file with the date as part of the name, how
can i place output of the script into this file ?
for example i need to have a simple mail script which i have done seperately
Code:
#! /bin/sh



LOCAL=/root/em_files


cd $LOCAL

for f in *.tif

do 
	if [ $f = "*" ]; then
		break #because of no files
	
	else 
	mutt -s "[$f]" -a $f e_address@yahoo.com < mail_body.txt
	rm -f $f
	fi
done

I need to be able to output to the log file if mail was successfully sent,
or an echo message if i add one ?
# 2  
Old 12-05-2007
You can leave out the touch command if you wish, just add either > $LOGFILE or >> $LOGFILE to the end of any command you want to record STDOUT from. ( in sh, 2> grabs STDERR)

>> creates the file if it doesn't exist and appends to it if it does
> creates the file if it doesn't exist and overwrites it if it does
# 3  
Old 12-05-2007
I use a base set of code for background jobs:

Code:
# Send all output to a logfile and supress input
typeset LOG="/tmp/${0##*/}.out"
mv $LOG ${LOG}.old >/dev/null 2>&1
[[ -t 1 ]] && echo "Writing to logfile '$LOG'."
exec > $LOG 2>&1
exec < /dev/null 2<&1

Note that the shell expression "${0##*/}" is the same as "basename $0". Also, the "[[ -t ]]" should return TRUE when the process is attached to a terminal. In this case, it just prints a message about the logfile (instead of having the user wonder if the script is even running).

I also close stdin since an unattended script should never need input. For instance, if you have "grep $FILENAME" or "cat $FILENAME" and a bug has left $FILENAME unset, then the script would normally hang waiting for input from stdin. If that input is already /dev/null then the script can continue.

Once you have run "exec" to redirect the output, you can't send messages to the terminal anymore unless you use "echo > /dev/tty".

I don't think this is exactly what you were looking for but I think you'll find it helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to move files with first part as new filenamevia bash shell scripting?

I am on linux red hat, want to use the logic within bash shell script file. I have the following type, named files in folder /staging, want to move with new filename just the firstpart upto underscore, example 20180904105056.dat, how can i get upto first part of filename and validate to chk if... (2 Replies)
Discussion started by: cplusplus1
2 Replies

2. Shell Programming and Scripting

How to join 2 text files using bash scripting?

Hi Guys, I want to combine 2 files and and put together in 1 file . See below desired output. Any help will be much appreciated. FILE AX 2134 101L 12345.00 22222.00 1 10 X 2134 101L 12345.00 22222.00 11 20 X 2134 101L 12345.00 22222.00 21 30 X 2134 111L 77777.00 ... (3 Replies)
Discussion started by: H.R
3 Replies

3. Shell Programming and Scripting

Bash script to copy apache log files to client directory

Our Apache log files are written to a location on the server that we as clients have no access. Don't ask. Every month, I have to e-mail the administrator to have him manually copy our Apache log files to a directory in our file space. You can probably guess how efficient it is to do things this... (3 Replies)
Discussion started by: gregraven
3 Replies

4. Shell Programming and Scripting

BASH scripting - Preventing wget messed downloaded files

hello. How can I detect within script, that the downloaded file had not a correct size. linux:~ # wget --limit-rate=20k --ignore-length -O /Software_Downloaded/MULTIMEDIA_ADDON/skype-4.1.0.20-suse.i586.rpm ... (6 Replies)
Discussion started by: jcdole
6 Replies

5. Shell Programming and Scripting

bash scripting help

have this code but when i run it i get this error ./pulse: line 2: and here is the code #!/bin/bash if ; then pulseaudio -k; fi what am i doing wrong thanks Adam (5 Replies)
Discussion started by: ab52
5 Replies

6. Shell Programming and Scripting

Help ME with Shell scripting log files

I currently have a script that was written a while back and it generates it's own log file. I had to recently alter this script so that it would execute another script in parallel 10 times such as the following example; echo "It is currently: ";date int=0; cap=10; while((int <... (2 Replies)
Discussion started by: afbuisiii
2 Replies

7. Shell Programming and Scripting

Bash scripting to compare N number of files located in two directories

I want to compare "N" (around 2000+) number of huge files located in a directory A against "N" files located in a different directory using Bash scripting. Please help me with any scripts available. Thanks. (2 Replies)
Discussion started by: Sangtha
2 Replies

8. Shell Programming and Scripting

Scripting help for rm log files from multiple dir

Hi, I'm quite new to scripting and need some help. I need to have one script that will check specific directories for files older than one month and then have the script delete them. I have written the script below but it only does one directory. I don't quite know how to make it so it... (7 Replies)
Discussion started by: morgadoa
7 Replies
Login or Register to Ask a Question