2

While running shell scripts how we can redirect the output of shell scripts into two different files. i.e.STDOUT and STDERR files. If there is some error logs should go to STDERR file and if script successfully runs then logs should be generated under STDOUT file

marked as duplicate by Romeo Ninov, αғsнιη, GAD3R, Satō Katsura, Kusalananda Oct 11 '17 at 12:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Try this:

echo "test" 1>STDOUT 2>STDERR

Replace echo "test" with any command or script.

Simple example:

Create script.sh with content:

#!/bin/bash

du -shc /*

Add execution permission:

chmod u+x script.sh

And run it:

./script.sh 1>STDOUT 2>STDERR

Then see each file:

# cat STDOUT
8,6M    /bin
39M     /boot
0       /dev
4,1M    /etc
1,1G    /home
0       /initrd.img
0       /initrd.img.old
231M    /lib
4,0K    /lib64

# cat STDERR
du: cannot access `./proc/7422/task/7422/fd/4': No such file or directory
du: cannot access `./proc/7422/task/7422/fdinfo/4': No such file or directory
du: cannot access `./proc/7422/fd/4': No such file or directory
du: cannot access `./proc/7422/fdinfo/4': No such file or directory

To set up redirection inside script use exec:

#!/bin/bash
exec 1>STDOUT 2>STDERR

du -shc /*

And simply run script:

./script.sh

Explanation:

1>filename
 Redirect stdout to file "filename."
1>>filename
 Redirect and append stdout to file "filename."
2>filename
 Redirect stderr to file "filename."
2>>filename
 Redirect and append stderr to file "filename."
&>filename
 Redirect both stdout and stderr to file "filename."
 This operator is now functional, as of Bash 4, final release.

M>N
 "M" is a file descriptor, which defaults to 1, if not explicitly set.
 "N" is a filename.
 File descriptor "M" is redirect to file "N."
M>&N
 "M" is a file descriptor, which defaults to 1, if not set.
 "N" is another file descriptor.

For more information see I/O Redirection

  • Thanks Egor..Helped me a lot..But when executing the script, output file is getting deleted and creating a new file.Could you please let me know how to create a file all the time when scripts execute. – Roshan Oct 11 '17 at 8:28
  • @Roshan, if you need to append data to log files but not recreate this files, you can use >> instead of > – Egor Vasilyev Oct 11 '17 at 8:57
  • ,thanks but I need log files should generate every time when my script will run.As of now my Script will be running in every two mins and I need log files should be generated all the time when my scripts is executed. doesn't want to append in single file but also I want logs files to be created all the time. – Roshan Oct 12 '17 at 2:51
  • @Roshan, try to add timestamp to each filename and script will be generated new files everytime it starts. – Egor Vasilyev Oct 12 '17 at 5:25
0

Example shell script:

#!/bin/bash
echo "Good"
# and something bad I can't do under ordinary user
touch /root/something

Run as:

$ test.sh 1>/tmp/STDOUT 2>/tmp/STDERR

And the content is:

$ cat /tmp/STDOUT 
Good
$ cat /tmp/STDERR 
touch: cannot touch '/root/something': Permission denied
  • Can I pass the line inside the script,if yes then how? I mean If I am executing the scripts without passing the stdout and stderr parameters, is there any way that I will only execute the scripts and the script will create two files named as stdout and stderr files with respective messages. – Roshan Oct 11 '17 at 7:24
  • Actually 1>/tmp/STDOUT and 2>/tmp/STDERR aren't parameters in this case. It's parsed by shell before the execution and it redirects all outputs of executed script (or binary). – Jaroslav Kucera Oct 11 '17 at 7:27
  • okk..So actually my requirement is I don't need to pass on shell..I want it to be written inside my shell scripts – Roshan Oct 11 '17 at 7:29
  • Well, it's not really common. External redirect is proper way. However yes, you can add the 1>>/tmp/STDOUT 2>>/tmp/STDERR to each command you execute. In this case there is >> instead of >, which means not rewrite, but append. – Jaroslav Kucera Oct 11 '17 at 7:36
  • 1
    @Roshan, use exec command inside script. See my answer – Egor Vasilyev Oct 11 '17 at 7:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.