Attaching a logfile to the Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Attaching a logfile to the Script
# 1  
Old 06-10-2009
Attaching a logfile to the Script

Hello guys.
I've recently written a basic utilities script just for home use.
and i want to attach a logfile to it that will record all the commands that where executed in that script. Then just so i can add the d%b%y% and make each logfile unique and i can look back in each logfile to see what i did.

I no it can be done i did it a long time back but have totally forgotten.

Thanks.
# 2  
Old 06-10-2009
Putting a set -x in the script just below the shell selection (e.g. #!/bin/sh) will cause what is run to be echoed out, then you call the script thus:
Code:
LOGFILE=/path/`date '+%d%b%y'`.logfile.log
$ ./script.sh 2>&1 >> ${LOGFILE}

Is that what you are after?

Last edited by TonyFullerMalv; 06-10-2009 at 02:49 AM.. Reason: Formatting
# 3  
Old 06-10-2009
bash: ${LOGFILE}: ambiguous redirect

I get this errror. This is waht my script is looking like...

#!/bin/bash
set -x
LOGFILE=/home/$USER/logfiles/`date '+%d%b%y'`.logfile.log;

At the start of the Shell Script


I dont want everything to be echo'ed because it's a menu driven script and once i run it, it becomes hectic echoing everything.

Last edited by matt02; 06-10-2009 at 03:07 AM..
# 4  
Old 06-10-2009
put redirect after the commands (inside the script) of which you want the output to be appended to the logfile.
# 5  
Old 06-10-2009
ok..

#!/bin/bash
LOGFILE=/home/$USER/logfiles/`date '+%d%b%y'`.logfile.log;
./matt.sh 2>&1 >> ${/home/$USER/}

That's what i've gotten at the start of the script..
But it still isn't giving me a file.

Im a novice, so your going to have to be lil user friendly here.
sozxxx

Thanks.
# 6  
Old 06-10-2009
One way is to echo the command within your script to the logfile like this:

Code:
#!/bin/ksh
.
.
.
echo "Give your choice: "

read answer

case "$answer" in
  1) echo "command1" >> logfile
     command1
     ;;
  2) echo "command2" >> logfile
     command2
     ;;
  3) ...
     ;;
  4) ...
     ;;
  * ) echo "Other choice..."
     ;;
esac

# 7  
Old 06-10-2009
Can't say i reall get it...
Code:
#!/bin/bash
while :
do
 clear
 echo "   M A I N - M E N U"
 echo "1. Make a New Directory"
 echo "2. Safely Delete a File"
 echo "3. List Out the Contents of a File"
 echo "4. Display the Full name of Any User"
 echo "5. Display The Current Date"
 echo "6. Display the Disk Usage"
 echo "7. Show who is logged on"
 echo "8. Show User Processes"
 echo "9. Display The logfile"
 echo "0. Exit"
 echo -n "Please enter option [0 - 9]"
 read opt
 case $opt in
  1) 'pwd'
     echo "The Above Directory is where your file will be created.";
     echo "Make a New Directory, Enter a New DIR NAME please $USER";
     read "dir_name";
     mkdir "$dir_name";
     echo "Press Enter key to Continue";
     read enterKey;;

  2) "$(ls -l)"
     echo "Which File Do you want to delete?";
     read fname;
     if [ -d $fname ];
     then echo "Sorry But this file is a Directory and Must be deleted Manually";
     fi;
           
      if [ -f $fname ];
      then
      cp $fname /home/$USER/dsave/$fname$$;
      fi;
      rm -r "$fname"
      echo "Please press the enter Key to continue..."
      read enterKey;;

  3) "$(ls -l)"
     echo "Which File Do you want list the Contents of?";
     read response2;
     ls -l "$response2";
     echo "Press Enter Key To Continue";
     read enterKey;;
 
  4) echo "Listing all Users now";
     cat /etc/passwd | grep "/home" |cut -d: -f1
     echo "All users have now been Printed"; 
     echo "Press Enter Key to continue";
     read enterKey;;

  5) echo " Todays date and time is '$(date)'";
     echo " Press the Enterkey to Continue";
     read enterKey;;

  6) "$(df -ah)";
     echo " Press the Enterkey to continue";
     read enterKey;;

  7) echo "This Is Who Is Currently Logged";
     who | more;
     echo "Please press enter key to continue..";
     read enterKey;;

  8) echo "Displaying all processes for Users";
     "$(ps -afu)";
     echo "Press the Enterkey to continue";
     read enterKey;;

#  9) LOGFILE=/home/$USER/logfiles/`date '+%d%b%y'`.logfile.log;;

  0) echo "GoodBye";
     exit l;;
esac
done

So where abouts would i redirect it to the logfile?
And what kind of options is it going to record, Just the text on the line?
Theres no way of recording everything that goes on in this script simply..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Logfile monitoring with logfile replacement

Bonjour, I've wrote a script to monitor a logfile in realtime. It is working almost perfeclty except for two things. The script use the following technique : tail -fn0 $logfile | \ while read line ; do ... some stuff done First one, I'd like a way to end the monitoring script if a... (3 Replies)
Discussion started by: Warluck
3 Replies

2. Shell Programming and Scripting

on manual script need to add logfile

hi all, 1.I am new to linux shell scripting. 2.i had an rman.sh script which will take backup all database using CATALOG DATABASE. 3.While executing that script my catalog DB gets shutdown. 4.The rman.sh script are executed manually. 5.so i need log for rman.sh .And also it is very useful... (1 Reply)
Discussion started by: jp_linux
1 Replies

3. Shell Programming and Scripting

generate logfile in a shell script

Unix Gurus, I have a shell script which has few "echo" statements. I am trying to create a logfile where all the outputs of the echo statement sare stored. I will have to add this as the final step in the existing script so that everytime the script runs, a logfile is generated with all the... (1 Reply)
Discussion started by: shankar1dada
1 Replies

4. Shell Programming and Scripting

Script for Logfile Inserting into the Database

Here's the problem. I have a shell script running for every one minute as cronjob that outputs to a log file. I want to be able to take the data from the log file and insert it into a mysql database. The script needs to run at a set period of time and remove the log file data once the insert is... (7 Replies)
Discussion started by: kgrvamsi
7 Replies

5. Shell Programming and Scripting

Script to monitor errors in logfile

Hi, I would like to implements monitoring tool which looks at the logfile and sends me an email with error messages from the logfile. The runtime errors are printed in logfile. The logfile also contains some warning and debugging messages. For errors, the line will start with “Error”., the error... (1 Reply)
Discussion started by: LinuxLearner
1 Replies

6. Shell Programming and Scripting

attaching file in mail command from shell script

Hi all, Currently in shell script i am using the following command to send mail echo "Certain assets are not loaded properly. PFA the result of the DataLoad" | mail -s "Weekly DataLoad - Failure Notification !!" $MAILINGLIST I need to attach a file along with that. How should i handle this... (7 Replies)
Discussion started by: ananthi_ku
7 Replies

7. Shell Programming and Scripting

Logfile monitor script

Hi, I'm trying to write a logfile monitor script that reads the logfile and then emails out once there is an error with SQL in. Here is my attempt below which does not work. I'm not much of a scripter as you can probably see but some pointers in the right direction would be much appreciated. ... (3 Replies)
Discussion started by: elmesy
3 Replies

8. Shell Programming and Scripting

Logfile rotation script.

I'm trying to find or create a Perl script that: Checks for and creates these files: notes notes.1 notes.2 notes.3 notes.4 The first represents the current log file and the others are older versions. Each time the script runs it would check for the existence of notes.3 and, if it exists,... (3 Replies)
Discussion started by: HardyV2
3 Replies

9. Shell Programming and Scripting

Script to manipulate logfile text

Hi guys, I was wandering if a Shell guru could give me some advice on tackling a problem. I have used a mixture of grep, cut and awk to get data from a log file in the following format: 14/11/08 10:39: Checking currenly : Enabled 14/11/08 10:39: Records allocated : 221... (11 Replies)
Discussion started by: rosspaddock
11 Replies

10. Shell Programming and Scripting

perl script to filter logfile

i was wondering if anyone can help me, how could i write in perl a a script that would look through a log file and print onscreen the contents of the log file excluding lines that contain '192.168.1.' and entries that contain directory paths that arent in the directory /usr/local/httpd/htdocs/ i... (4 Replies)
Discussion started by: norsk hedensk
4 Replies
Login or Register to Ask a Question