Results 1 to 3 of 3

Thread: How to Log

  1. #1
    Join Date
    Aug 2004
    Location
    Rome, Italy
    Posts
    81

    Unanswered: 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

  2. #2
    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 06:48.

  3. #3
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •