How can I redirect stdout and stderr with variant?

Qinsi

Normally, we use

sh script.sh 1>t.log 2>t.err

to redirect log.

How can I use variant to log:

string="1>t.log 2>t.err"
sh script.sh $string
Prasanna

You need to use 'eval' shell builtin for this purpose. As per man page of bash command:

   eval [arg ...]
          The  args are read and concatenated together into a single command.  This command is then read and exe‐
          cuted by the shell, and its exit status is returned as the value of eval.  If there  are  no  args,  or
          only null arguments, eval returns 0.

Run your command like below:

eval sh script.sh $string

However, do you really need to run script.sh through sh command? If you instead put sh interpreter line (using #!/bin/sh as the first line in your shell script) in your script itself and give it execute permission, that would let you access return code of ls command. Below is an example of using sh and not using sh. Notice the difference in exit codes.

Note: I had only one file try.sh in my current directory. So ls command was bound to exit with return code 2.

$ ls try1.sh try1.sh.backup 1>out.txt 2>err.txt
$ echo $?
2

$ eval sh ls try1.sh try1.sh.backup 1>out.txt 2>err.txt
$ echo $?
127

In the second case, the exit code is of sh shell. In first case, the exit code is of ls command. You need to make cautious choice depending on your needs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

From Java

How can I pipe stderr, and not stdout?

From Dev

On a Linux system, how would I redirect stdout to stderr?

From Dev

How do I redirect stderr to stdout and vice versa

From Dev

Can't redirect stdout and stderr in background

From Dev

How to redirect the raw_input to stderr and not stdout?

From Dev

How to redirect error output to both stdout and to stderr?

From Dev

Linux : how to redirect stdout & stderr to logger?

From Dev

How to redirect a part of stderr and stdout to /dev/null

From Dev

How to redirect stderr in a variable but keep stdout in the console

From Dev

In Chrome, how can I get the javascript console output to stdout/stderr

From Dev

How can I make subprocess get the stdout and stderr in order?

From Dev

In Erlang, how can I independently capture stdout and stderr of a subprocess?

From Dev

How can I view stdout/stderr of a startup application?

From Dev

How can I convert to uppercase stderr output but not stdout?

From Dev

How to redirect stdout and stderr to a file and display stderr to console?

From Java

Redirect stderr and stdout in Bash

From Dev

Redirect stdout to stderr in tcsh

From Dev

Permanently redirect stderr to stdout

From Dev

How to redirect stdout to a file, and stdout+stderr to another one?

From Dev

Redirect stdout and stderr to file and stderr to stdout

From Dev

Redirect stdout and stderr to file and stderr to stdout

From Dev

How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

From Dev

How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

From Dev

How can I redirect the STDOUT to STDIN in a C program

From Java

How to redirect and append both stdout and stderr to a file with Bash?

From Dev

How to redirect stderr and stdout to different files and also display in terminal?

From Dev

How to redirect stderr to a file and some echos (not stdout) to the same file

From Dev

How to redirect stderr/stdout of a Gtk.Window() call in python?

Related Related

  1. 1

    How can I redirect the stdout and stderr of a command to both the console and a log file while outputting in real time?

  2. 2

    How can I pipe stderr, and not stdout?

  3. 3

    On a Linux system, how would I redirect stdout to stderr?

  4. 4

    How do I redirect stderr to stdout and vice versa

  5. 5

    Can't redirect stdout and stderr in background

  6. 6

    How to redirect the raw_input to stderr and not stdout?

  7. 7

    How to redirect error output to both stdout and to stderr?

  8. 8

    Linux : how to redirect stdout & stderr to logger?

  9. 9

    How to redirect a part of stderr and stdout to /dev/null

  10. 10

    How to redirect stderr in a variable but keep stdout in the console

  11. 11

    In Chrome, how can I get the javascript console output to stdout/stderr

  12. 12

    How can I make subprocess get the stdout and stderr in order?

  13. 13

    In Erlang, how can I independently capture stdout and stderr of a subprocess?

  14. 14

    How can I view stdout/stderr of a startup application?

  15. 15

    How can I convert to uppercase stderr output but not stdout?

  16. 16

    How to redirect stdout and stderr to a file and display stderr to console?

  17. 17

    Redirect stderr and stdout in Bash

  18. 18

    Redirect stdout to stderr in tcsh

  19. 19

    Permanently redirect stderr to stdout

  20. 20

    How to redirect stdout to a file, and stdout+stderr to another one?

  21. 21

    Redirect stdout and stderr to file and stderr to stdout

  22. 22

    Redirect stdout and stderr to file and stderr to stdout

  23. 23

    How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

  24. 24

    How do I redirect a process' stdout, stderr and stdin to and from files in Groovy, like in the Bash shell?

  25. 25

    How can I redirect the STDOUT to STDIN in a C program

  26. 26

    How to redirect and append both stdout and stderr to a file with Bash?

  27. 27

    How to redirect stderr and stdout to different files and also display in terminal?

  28. 28

    How to redirect stderr to a file and some echos (not stdout) to the same file

  29. 29

    How to redirect stderr/stdout of a Gtk.Window() call in python?

HotTag

Archive