Log file in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Log file in shell script
# 1  
Old 12-04-2015
Log file in shell script

hi all,

can you please let me know how to write a input and output in the log file for the below script. any help highly appreciated.
Code:
us>shell_scripts> cat clientcheck.ksh
#!/usr/bin/ksh
#client_check.ksh zxx
#set -x
kk=$1
hn=`hostname`
if [[ $hn = "us" ]]
then
  case $1 in
      ban)   client_home='/comps/banking'
             ;;
      ret)     client_home='/comps/retailer'
             ;;
       *)     client_home=$(echo /comps/"$1")
             ;;
   esac
else
client_home=$(echo /?home/$1"home/"$1"dba")
fi

if [ ! -d ${client_home} ]; then
   exit 1
else
   exit 0
fi

---------- Post updated at 03:09 AM ---------- Previous update was at 02:28 AM ----------

i have created a date file like the below format . any idea how to write the input and output of the following commands in the file.
Code:
## date format ##
NOW=$(date +"%F")
NOWT=$(date +"%T")
## Backup path ##
BAK="/systems/prkek/logfile/$NOW.$NOWT.txt"
touch $BAK

# 2  
Old 12-04-2015
I don't understand what you want. Your script doesn't produce any output (other than the exit code).

Add echo or printf or some other statements producing the output you want to include in your log file. Then, if all output produced by your script should be included in the log file, redirect the output of your script to that file or add an exec statement in your script to redirect all output (or output redirected to stderr) to your log file. Or, if you want some output produced by your script to go to standard output and some other output to go into your log file, just redirect the output from each output statement into the appropriate file.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-04-2015
thanks for your help don. i would need to assign the exit status[0,1] to a variable $var and redirect it to the log file.
# 4  
Old 12-04-2015
Quote:
Originally Posted by arun888
thanks for your help don. i would need to assign the exit status[0,1] to a variable $var and redirect it to the log file.
You can't write to a log file after you exit.

Why do you care if the exit status is stored in a variable before you write it?

What is the name of the log file?

Do you want to append data to the end of the log file, or do you want to replace the entire contents of your log file each time you run your script?

Do you really just want to write a 0 or 1 to your log file? Or, do you want the name of the program producing that line to be included in the log file?

If you just want a 0 or 1 in a file, why not just capture the exit status of your program after it completes instead of creating a file?

Do you want a timestamp showing when that entry was written to your log file? If you do, what format do you want to use? And, do you want to use local time or coordinated universal time (AKA GMT)?
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-04-2015
hi don,

is there any possibility in shell script to write $1,$2, exit status in the log file.

Code:
NOW=$(date +"%F")
NOWT=$(date +"%T")
## Backup path ##
BAK="/systems/logfile/$NOW.$NOWT.txt"
touch $BAK
var=$1
inp=$2
if [ ! -d ${comp_dir} ]; then
exit=1 
exit 1
else
exit=0
exit 0
fi

---------- Post updated at 04:50 AM ---------- Previous update was at 04:44 AM ----------

You can't write to a log file after you exit.

Code:
 need to print only the exit status and input given by the user.

Why do you care if the exit status is stored in a variable before you write it?
Code:
 Just to check whether it is correct

What is the name of the log file?
Code:
NOW=$(date +"%F")
NOWT=$(date +"%T")
## Backup path ##
BAK="/systems/logfile/$NOW.$NOWT.txt"

Do you want to append data to the end of the log file, or do you want to replace the entire contents of your log file each time you run your script?
Code:
 need to create a new file everytime

Do you really just want to write a 0 or 1 to your log file? Or, do you want the name of the program producing that line to be included in the log file?
Code:
  i need the exit status need to be redirected to the output and also along with the input

If you just want a 0 or 1 in a file, why not just capture the exit status of your program after it completes instead of creating a file?

yes

Do you want a timestamp showing when that entry was written to your log file? If you do, what format do you want to use? And, do you want to use local time or coordinated universal time (AKA GMT)?
yes local time

---------- Post updated at 04:57 AM ---------- Previous update was at 04:50 AM ----------

i have recieved the output. thanks for your help.

please close the request.
# 6  
Old 12-04-2015
Hi Arun888,

If you read this thread, you will see an example of a script and a logging utility script that can be plugged into any shell script - it should manage to deliver most of your functions without any modification.

Regards

Gull04
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script for continuously monitoring log file

Hi I have written below log monitoring script to egrep multiple words and redirect the output to a text file and its working fine but I want to add some more below given functionality to it, which is very advance and im not very good in it, so please help if you can :) I am egrepping all the... (1 Reply)
Discussion started by: scazed
1 Replies

2. Shell Programming and Scripting

Shell Script function to use script name for log file output

Hi Team - I"m very new to Shell Scripting so I have a rather novice question. My forte is Windows Batch Scripting so I was just wondering what the Shell Script equivalent is to the DOS command %~n? %~n is a DOS variable that dispayed the script name. For instance (in DOS): REM... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

3. Shell Programming and Scripting

Shell Script to grep Job File name and Log File name from crontab -l

Hello, I am new to shell scripting. I need to write a shell script where i can grep the name of file ie. .sh file and log file from crontab -l. #51 18 * * * /home/oracle/refresh/refresh_ug634.sh > /home/oracle/refresh/refresh_ug634.sh.log 2>&1 #40 17 * * * /home/oracle/refresh/refresh_ux634.sh... (1 Reply)
Discussion started by: guptra
1 Replies

4. Red Hat

Need help with a shell script for finding a certain pattern from log file

Hell Guys, Being a newbie, I need some help in finding a certain string from a log file of thousands of lines (around 30K lines) and have the output in a separate file. Below is the file output - 10.155.65.5 - - "POST... (15 Replies)
Discussion started by: rockf1bull
15 Replies

5. Shell Programming and Scripting

need a shell script to extract data from a log file.

If I have a log like : Mon Jul 19 05:07:34 2010; TCP; eth3; 52 bytes; from abc to def Mon Jul 19 05:07:35 2010; UDP; eth3; 46 bytes; from aaa to bbb Mon Jul 19 05:07:35 2010; TCP; eth3; 52 bytes; from def to ghi I will need an output like this : Time abc to def... (1 Reply)
Discussion started by: hitha87
1 Replies

6. Shell Programming and Scripting

Shell Script to continuously scan a log file

Hello members, I have some doubts on how to write a script that can reports success / failure of a batch job ? 1. Run a batch job: 2. Wait and search for a particular string in the Log file: tail -f log01*.txt | egrep -v "^SUCCESSFUL" echo "continue with the other tasks" ... (1 Reply)
Discussion started by: novice82
1 Replies

7. Shell Programming and Scripting

shell script to remove old files and write to a log file

Hi, I have a script that works on a unix box but am trying to get it working on a linux box that uses shell. I am not a programmer so this is proving harder than I imagined. I made some changes and ended up with the script below but when I run it I get the following messages. Any help would be... (4 Replies)
Discussion started by: yabai
4 Replies

8. Shell Programming and Scripting

shell-script which extract data from log file

give me a shell-script which extract data from log file on a server by giving date and time as input (for both start time and end time) and it will give the logs generated during the given time as output. (4 Replies)
Discussion started by: abhishek27
4 Replies

9. Shell Programming and Scripting

Shell script for parsing 300mb log file..

am relatively new to Shell scripting. I have written a script for parsing a big file. The logic is: Apart from lot of other useless stuffs, there are many occurances of <abc> and corresponding </abc> tags. (All of them are properly closed) My requirement is to find a particular tag (say... (3 Replies)
Discussion started by: gurpreet470
3 Replies

10. Shell Programming and Scripting

Help with a shell script for creating a log file

I have a schell script that runs continously on an AIX system. It is actually started from another shell script with the "ksh -x" command and then I just write the output to a log file. This causes the log files to be filled with mostly useless information. I would like to modify this script to... (2 Replies)
Discussion started by: heprox
2 Replies
Login or Register to Ask a Question