Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all, I am working on a bash script in which I have to check for some command & want the output of that command to be saved in a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    7

    Unhappy Problem Generating Log


    Hi all,

    I am working on a bash script in which I have to check for some command & want the output of that command to be saved in a file.

    suppose if I fire a command wget w3.google.com (I am not allowed to post URL in forum) then i ll get some log printed on console .. I want to save that log in my log file.

    Please help me solve this issue

    Thanks in advance.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,390
    Just direct standard out to a file.
    wget wwww.google.de > /tmp/logfile
    or the standard error
    wget wwww.google.de 2> /tmp/logfile
    or both
    wget wwww.google.de > /tmp/logfile 2>&1
    You must always face the curtain with a bow.

  3. #3
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,390
    You could also utilize the existing syslog.
    Have a look at
    Code:
    man logger
    You must always face the curtain with a bow.

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Posts
    7
    try use the tee command..

  5. #5
    pdk
    pdk is offline
    Just Joined!
    Join Date
    Aug 2008
    Posts
    7
    put set -x in the script and then direct the output to a file
    myprog > log
    Peter

  6. #6
    Just Joined!
    Join Date
    Sep 2010
    Posts
    7

    Red face

    thnx for you support..

    I have tried out standard output to my file & but the problem is something else

    my command (I have written wget here instead ) shows full loading process, i mean 0% to 100% task completion & at the end it shows done successfully or failed..

    I just want that status to be appended in the file ..

    Can you please help me bit more as I am totally new to shell scripts.

    Thanks again

  7. #7
    pdk
    pdk is offline
    Just Joined!
    Join Date
    Aug 2008
    Posts
    7
    When a job completes in a shell there is a variable that stores a 1 or 0 depending on pass or failure. I think it is the '?'
    if [ "?" == 1 ]
    then
    echo "task failed"
    fi
    something like the above. I write so few scripts these days and forget the exact format. Hope this helps.
    You will have to read man bash to find the correct variable.
    Peter

  8. #8
    Just Joined!
    Join Date
    Oct 2010
    Posts
    6
    Expanding on pdk said, $? is the variable that holds the exit code of the last command. 0 means okay, anything else means failed.
    Code:
    if [ $? -eq 0 ]
    then
    echo "task completed" > log.log
    else
    echo "task failed" > log.log
    fi

Posting Permissions

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