1
  • Script1 uses echo (and other commands) to standard output.

  • Script1 also runs other scripts, which also use echo (and other commands) to standard output.

  • Script1 is called by a scheduler that stores the standard output from Script 1 as report.

Is there a way for Script1 to copy its own standard output to a file?

(A possibility would be for another script, say Script0, to run Script1 with output directed to a file and printing that file, or use tee to do both)

6
  • Just run your script1 like script1> outFile May 14, 2015 at 16:25
  • If you instead want script1 to always copy its output to a file (and not by specifying something on the command line), you would need to change each line to either use > or tee. May 14, 2015 at 17:01
  • 1
    > will replace the contents in that output file. use >> if you want to copy to the output file without replacing the content.
    – Sooraj S
    May 14, 2015 at 17:17
  • You probably won't need a wrapper script, just use sh directly: replace the call to the script with sh -c '/path/to/script "$@" | tee log-file' _ arg1 arg2 ..
    – muru
    May 14, 2015 at 17:40
  • Thanks for your answers. I knew I could run the script directing the output to file but what I wanted to know is if a script could direct its own output to file without running it from an outside script. May 15, 2015 at 9:08

1 Answer 1

0

An example as an answer

Two scripts in the same path

  • script1

    #!/bin/bash
    ./otherScript
    echo "script1" | tee script1.log
    
  • otherScript

    #!/bin/bash
    echo "otherScript"
    

Lets start script1 and check the output

% ./script1       
otherScript
script1

and the logfile script1.log, created in script1

% cat script1.log 
script1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .