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.

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:

LOGFILE=/path/`date '+%d%b%y'`.logfile.log
$ ./script.sh 2>&1 >> ${LOGFILE}

Is that what you are after?

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.

put redirect after the commands (inside the script) of which you want the output to be appended to the logfile.

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.

One way is to echo the command within your script to the logfile like this:

#!/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

Can't say i reall get it...

#!/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..

For the 1st 2 options it should be:

      .
      .
      .
  1) 'pwd'
     echo "The Above Directory is where your file will be created."; >> /home/
     echo "Make a New Directory, Enter a New DIR NAME please $USER";
     read "dir_name";
     echo mkdir "$dir_name" >> logfile
     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 rm -r "$fname" >> logfile
      echo "Please press the enter Key to continue..."
      read enterKey;;
      .
      .
      .
#!/bin/bash
LOGFILE=/home/$USER/logfiles/`date '+%d%b%y'`.logfile.log
./matt.sh >> ${LOGFILE} 2>&1

-----Post Update-----

In which case you need to do as Franklin52 has suggested.

Thank you very much Franklin, it worked :slight_smile:
Turns out to be pretty simple in the end doesnt it haha.

Thank you.