If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to Log

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-31-04, 08:55
ducasio ducasio is offline
Registered User
 
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
How to Log

How can I write a script in bash so I can decide another log file instead of the default one? For treating the options I am using getopts.
e.g.

./myscript.sh logs to the default log file

and

./myscript.sh -l newlog logs to newlog file

Thank you in advance,
ducasio
Reply With Quote
  #2 (permalink)  
Old 09-02-04, 05:46
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
You're not really giving enough information here but I'll have a go.

I'll assume that the 'default' log is standard out/err. If you actually have a 'default log' coded in your scripts, then you can modify the code below.

Code:
exec 3>&1
while getopts ":l:" opt; do
  case ${opt} in
    l) exec >${OPTARG} 2>&1;;
    *) echo "Invalid argument supplied" >&3; exit 1;;
  esac
done
shift $((OPTIND-1))

echo Stdout message >&1
echo Implicit stdout message
echo Stderr message >&2
echo Safe message to terminal >&3
e.g. yourScript -l aLogFile

Last edited by Damian Ibbotson; 09-02-04 at 05:48.
Reply With Quote
  #3 (permalink)  
Old 09-03-04, 09:47
ducasio ducasio is offline
Registered User
 
Join Date: Aug 2004
Location: Rome, Italy
Posts: 81
how to log

hi Damian,
in what I see it is very useful. Actually I must use different options with the script and every option should be valid in long and short form e.g.

./myshell.sh [[-? | --usage] | [-h | -history] [-l file | --log=file] ]

The redirection inside the script is something like this:

......
while getopts ":hl:" opt
do
done

echo 'hello' 2>&1 |tee -a $LOGFILE

And after, how can I menage the long options? How can I print ERR in the begining of every line of error(stderr) and LOG in the begining of every line of output(stdout)?

Thank you for you help
ducasio
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On