Nov 21, 2009

Running Shell Script And Redirect To Log File?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Operating Systems > Linux

Running Shell Script And Redirect To Log File?

jedipi
I have a shell script like this:

while true
do
sh a.sh
sh b.sh
done

now I want the script not just call and run a.sh and b.sh, but also output some log files.
here is what I did:

while true
do
sh a.sh
sh a.sh >>log
sh b.sh
sh b.sh >>log
done

I know this is not a good way....
is there any better way to do it??
Thank you

Comment/Reply (w/o sign-up)

qwijibow
that IS a good way, thats te way you are meant to do it wink.gif

you may want to tweak what shell output can, and cannot be logged.

CODE
sh a.sh | grep LOG >> log


this will only log lines with the word "LOG" in them.. for example
"LOG: 1:00pm, and all is well."

EDIT:
just noticed
QUOTE
while true
do
sh a.sh
sh a.sh >>log
sh b.sh
sh b.sh >>log
done


So, you want the script to print to the console, AND write to the log..
yes, there is a much better way, you dont need to run the script twice.

use a variable to store the output..

this code example run the porgram "uptime" and outputs its result to the scree, and a logfile.
the variable name is called CURRENT_UPTIME

CODE
#!/bin/bash

CURRENT_UPTIME=`uptime`

# print to screen.
echo $CURRENT_UPTIME

#print to log
echo $CURRENT_UPTIME >> log


EDIT2:
it not clear on this forum, but those ` marks are the single back tilted quotes... using the normal ' will not work.

on my keyboard, the ` (correct mark) is the one below the ESC key.

in BASH shell programming, all 3 different quote marks have different meanings.

 

 

 


Comment/Reply (w/o sign-up)

signatureimage
Dear jedipi,

I coudn't help but notice in your example that you executed the shell commands two times:
CODE
sh a.sh
sh a.sh >>log


I suppose that the first execution was done to obtain the output on the console screen, and the second execution was done to obtain the output inside a file - what you call a log-file.

The danger here is that in some cases, it could be possible that the result would not be exactly the same - for instance when a time-dependent value was produced by the shell command.

There exists a shortcut to obtain the desired result by executing the bash script just once.

CODE
sh a.sh | tee log


The tee command is one of those odd little specialized Unix commands that come in handy at the strangest times. It both displays the command output on the screen, and stores it in a file. Use tee -a to append, instead of overwrite.

So, for the second bash script, we will use:

CODE
sh b.sh | tee -a log



I hope that I have interpreted your question correctly, and that my proposal will help you.

Comment/Reply (w/o sign-up)

qwijibow
lol, looks like we both posted at the same time, with different solutions.

you should probably use signatureimage's solution, same results, minus the need for variables.

been using bash for years, and thats the first time ive heard of that command (and yep, its installed on my system)

Comment/Reply (w/o sign-up)

jedipi
QUOTE (qwijibow @ May 19 2005, 11:46 PM)
lol, looks like we both posted at the same time, with different solutions.

you should probably use signatureimage's solution, same results, minus the need for variables.

been using bash for years, and thats the first time ive heard of that command (and yep, its installed on my system)
*


thanks for you guys solutions.

With this tee command, I can split the output stream.
so that it is both displayed on screen and also re-direct it to a file.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : running, shell, script, redirect, log, file

  1. Linux Basic Command - For Storing Compilation Error To File
    (1)
  2. How To Copy File & Folders From Linux To Windows?.
    (14)
    Many face problem while transfering file from Linux to windows and Vise versa. My solution for
    this is "winscp405". Winscp is a windows freeware tool used to copy file from & to Linux machine.
    To get this tool just google for " winscp405" tool and download this small package and install.
    Enjoy working with Linux and windows /tongue.gif" style="vertical-align:middle" emoid=":P"
    border="0" alt="tongue.gif" />....
  3. Simple Ubuntu Based Home File Server | For Newbies
    (1)
    This tutorial explains how to turn an old PC with additional hard disks into a simple home file
    server. The file server is intended for home use. The home file server is accessible by Windows and
    Linux computers in the home network. http://www.howtoforge.com/ubuntu-home-fileserver
    _________________________________ http://dserban01.googlepages.com/linkedin....abap.basis.html ....
  4. File Rename Script, Sentence
    Large collection of files to rename! (7)
    Hello there, Today I've encoded lots of mp3's for my portable mp3 player (which only has
    128mb ... /blink.gif" style="vertical-align:middle" emoid=":blink:" border="0" alt="blink.gif" />),
    Anyway I encoded them with Lame using mlame script (for those don't know it handles more files
    then one). Now mlame appends "-new-converted-file" to the filename. And renaming every individual
    file with the hand is A LOT of work /mellow.gif" style="vertical-align:middle" emoid=":mellow:"
    border="0" alt="mellow.gif" />. I tried to write a script to remove that "-new-conv....
  5. Help: How To Dual Boot Ubuntu And Windows ME
    I am running into some troubles (5)
    I recently recieved a Ubuntu distro (Breezy Badger, 5.10) and installed it on my laptop,
    uninstalling Windows in the process as I could not resize the FAT32 partition without erasing all
    data and I loved it! After several days without the internet as I couldn't get the modem
    working I decided to reinstall Windows ME on my laptop. I resized the Linux partition and created a
    FAT32, enough for Windows and any programs, games and documents I would like, ample space really.
    The partition was created successfully and the Linux partition resized. However, I inserted the Wi....
  6. Gnome Running In The Background In Kde
    Something is not right.... (14)
    Hi, recently, a few days ago, I logged into GNOME for a spell and then logged out and didn't go
    back to it again. I started using KDE but it's been slowing down a bit these days. I gave the
    command 'ps -e' and it shows GNOME and Nautilus running in the background. Here's a
    sample output: QUOTE PID TTY          TIME CMD     1 ?        00:00:04 init     2 ?       
    00:00:00 keventd     3 ?        00:00:00 ksoftirqd_CPU0     4 ?        00:00:00 kswapd     5 ?     
      00:00:00 bdflush     6 ?        00:00:00 kupdated   10 ?        00:00:00 mdrecoveryd....
  7. Linux/unix For My Purpose
    Running Linux/Unix as dedicated server (2)
    I'm thinking to start my own server and have never used any Linux or Unix OS before. I'm
    looking for the easiest, least code/programming, most buttons-click-customizing server friendly to
    MySQL, Apache, webmail, pop3, smtp, videostreaming and all that Linux/Unix OS out there. It must be
    free and easy to download. All suggestions are welcome! Thank you.....
  8. Linux & Midi
    Running MIDI Applications on linux (2)
    I would like to setup a music studio, and I will need multi channel sound inputs, as well as MIDI.
    What hardware / Linux ver would be the best for ease, and stability for this kind of professional
    recording project? I would like a setup that would rival any professional recording studio. The
    Original YB....
  9. Complie Mplayer With Gcc4.0
    Patch file is provided (1)
    GCC 4.0 comes with the new Fedora core 4. MPlayer cannot be compiled in this new environment. If you
    have Fedora core 4 or upgraded to GCC4 and have problems on compiling MPlayer, now worry, follow the
    steps: 1. Download all neccessry files. They are: * the source code of mplayer, I prefer
    the latest release: MPlayer-1.0pre7.tar.bz2 * mplayer-1.0_pre7-gcc4.patch . As you can see, this is
    a patch file for MPlayer 1.0pre7, In order the compile mplayer under gcc-4.0 environment, this
    patch must be applied This patch file can be download here: http://www.mag....
  10. No Such File Or Directory While Trying To Open Abl
    (5)
    dear all, Yesterday, I modified the /etc/fstab file in order to mount my usb thumb driver
    atomaticly. dev is sba1 and the mount point is /mnt/usb. I cannot remember what other parameters
    had been used, as I got something emegene to do. I just use "halt -n" comman to shutdown the PC.
    Today, I found that it cannot boot into the system. when it checking root filesystem:
    fsck.ext3:ABEL=/: The superblock could not be read or does not describled a coorect ext2 file
    system. If the device is valid and it realy contains an ext2 filesystem (and not swap or ufs or
    something else)....
  11. Why Do Normal Cannot Use Fat32 Volumn
    please, here is my fstab file (3)
    The following is my fstab. /dev/hda1 and /dev/hda5 is fat32 format. root can use after login, but
    nor the normal users. why? and how can I allow normal users to read the files inside fat32?? #
    /etc/fstab: static file system information. # # proc /proc proc defaults 0 0 /dev/hda6 /
    reiserfs notail 0 1 /dev/hda7 none swap sw 0 0 /dev/hdc /media/cdrom0 udf,iso9660 ro,user,noauto 0 0
    /dev/hda1 /mnt/winc vfat rw,user,auto 0 0 /dev/hda5 /mnt/wind vfat rw,user,auto 0 0 /dev/sda1
    /mnt/usb auto rw,user,auto 0 0....
  12. Gentoo! (windows Device Drivers Running Native
    on Gentoo !!!!) (7)
    Linux used to be too hard for the common user, didnt support enough hardware, and didnt have enough
    desktop tools to replace windows tools. currently the only 2 remaining complaints about linux are
    that (a) rare hardware / winModems and some wireless lan cards dont always support linux. (B) Most
    commercial software is never ported to linux. the wine proect has been working on problem (B) and
    is doeing extremely well. they can even get IE (which is very tightly integrated into windows) to
    run on Linux ! problem (a) can only be sorted with hardware hacking, reverse en....
  13. Help ! Can't Install Rpm File
    (2)
    I just installed redhat 9. and try to install some software. I use the following comman to do so:
    rpm -ivh file.rpm After I press Enter key, error message was shown: "can't create transaction
    lock" what should i do?....
  14. Running Linux on slow computers
    (17)
    I'm wondering if Mandrake 9.2 with X can be used on slow machine like: Pentium 350, 64mb ram,
    2mb graphic card. I don't want to install any servers (ssh maybe).....

    1. Looking for running, shell, script, redirect, log, file

See Also,

*SIMILAR VIDEOS*
Searching Video's for running, shell, script, redirect, log, file
advertisement



Running Shell Script And Redirect To Log File?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com