Log successful unix conditionals


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Log successful unix conditionals
# 1  
Old 12-17-2007
Log successful unix conditionals

Dears,

Can anyone advise how to log successful conditionals (unix commands) from any ksh script?

For example, the code is as follows:

if <command>
then
Block1 of statements
else
Block2 of statements
fi

What needed exactly is to log "Block1 of statements" if its conditon is met.

Thanks for your feedback
# 2  
Old 12-17-2007
I can suggest something for BASH, hope this helps.

...
LOGFILE="./logf.out"
NUM=$1

f_LOG() {
echo "`date`:$@" >> $LOGFILE
}

f_INFO() {
echo "$@"
f_LOG "INFO: $@"
}

f_ERROR() {
echo "$@"
f_LOG "ERROR: $@"
}


if [ $NUM -eq 4 ]; then
f_INFO "4 is my lucky number"
f_INFO "Thanks for using my script"
else
f_ERROR "You are not good in choosing a lucky number"
fi

Executing:

$ ./logg.sh 3
You are not good in choosing a lucky number

$ cat logf.out
Mon Dec 17 19:04:19 IST 2007:ERROR: You are not good in choosing a lucky number

$ ./logg.sh 4
4 is my lucky number
Thanks for using my script

$ cat logf.out
Mon Dec 17 19:04:19 IST 2007:ERROR: You are not good in choosing a lucky number
Mon Dec 17 19:04:41 IST 2007:INFO: 4 is my lucky number
Mon Dec 17 19:04:41 IST 2007:INFO: Thanks for using my script

//Jadu
# 3  
Old 12-17-2007
After executing a command, the result code can be obtained by using the variable $?

for example

command1
RES=$?

if [ $RES -ne 0 ]
then
echo "Error launch command"
else
echo "Command was succesful"
fi
# 4  
Old 12-17-2007
Or :
if [[ $? -eq 0 ]] ; then
echo "Success " >> $LOG
else
echo $? " return value of $? Some failure " >> $LOG
exit 1
fi
# 5  
Old 12-17-2007
I find it's handy to create a Log() function, to keep the messges format consistent and also allow output to several places and/or options.

I write something like:
Code:
#!/bin/ksh
# myscript

# Globals.
PROG=${0##*/}
LOGFILE=/var/tmp/mylog.txt

# Function to do logging.
Log()
{
  timestamp=`date '+%Y%m%d.%H%M%s'`
  echo "[$PROG.$timestamp] $*" >> $LOGFILE
}

# your code...
if [ ..... ]
then
  Log "It was successful"
else
  Log "Oh no!"
fi

You can then have logic in the Log function to raise OpenView alerts, send emails, write to syslog, or whatever, without having to trawl through your entire script looking at each echo command.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script no conditionals

Hello, pro scripters, noob here, I am complete noob in this and I have to write a program which: Calculates the modulus of two numbers which the user enters with keyboard. Number interval 1-9. These two numbers that were entered and modulus which was calculated are stored in a separate file.... (1 Reply)
Discussion started by: IrmantasID
1 Replies

2. Shell Programming and Scripting

mysql help : query with 2 conditionals

Hi there, I have a table that stores multiple records for many different servers, each of which is timestamped ... I wanted to write a query that would enable me to only output the "latest" record (based on timestamp) for each "unique" server. So for example my main table looks like this ... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

3. UNIX for Dummies Questions & Answers

How to get successful/unsuccessful FTP logs in UNIX

Hi, We have one UNIX Server (Sun Solaris), and the files coming to this server from another server. The problem is, that server is continously sending files to our server via FTP. But the observation is that some files missing in our Server but in that server it shows the files FTPed... (2 Replies)
Discussion started by: vikash.rastogi
2 Replies

4. Shell Programming and Scripting

Multiple conditionals in a while loop

Hi was wonderring about the syntax for the following peice of code: while ] ; do This is apparently incorrect. I want the loop to continue WHILE $life is not equal to zero or $mask is not equal to $word. Should ONE of these conditions fail to be met, loop should break out. Any ideas? ... (8 Replies)
Discussion started by: santeria
8 Replies

5. Shell Programming and Scripting

Using arrays with conditionals

Pardon the pseudo code below, but I'm wondering how I would do something along the lines of this... #!/bin/sh excludeList="A B" theItem="C" if then echo $theItem fi I know I could loop through the array and check against each value, but was wondering if there was an alternate way... (1 Reply)
Discussion started by: myndcraft
1 Replies

6. Shell Programming and Scripting

Multiple Conditionals

Yet another question. I want to make something like this: if ||&& then ... fi but that apparently is not right. I want either the first condition to be true OR the second AND third conditions to be true for the "then" to be processed. How can I do this? (1 Reply)
Discussion started by: jeriryan87
1 Replies

7. Shell Programming and Scripting

ideas for perl script - strings,conditionals..etc

I have a matrix , how do I compare all the elements of a column , lets say I want to check if the columns contain the alphabets "S","H","A","R","A","T". and not "X"s. Lets say matrix looks something like this .. SSSXSH HHXXHA AAXXAT RRRXRS AAXTAR TTTTTA I can hard code it where... (4 Replies)
Discussion started by: sharatz83
4 Replies
Login or Register to Ask a Question