This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In the bash shell, what is “ 2>&1 ”?
#echo "[+] Creating $count files"
_remount
create=$((time ./dirtest $testdir $count $size) 2>&1)
It means that the standard error descriptor stderr (2) is to be redirected to the standard output descriptor stdout (1). Notice that the & ensures that the 1 is interpreted as a file descriptor and not a filename.
It redirects standard error to the standard output. This way the result written to stderr will be stored in the variable.
Related
This question already has answers here:
Output not captured in bash variable [duplicate]
(1 answer)
How to store standard error in a variable
(20 answers)
Closed 2 years ago.
I have the following:
a=$(grep pattern file_not_exist)
echo $a. #turns out a is empty
But I can see the grep complaining: grep: file_not_exist: No such file or directory.
Why is the error messages from grep not assigned to be the value of shell variable a? And if we want this kind of redirection, how to do it?
I am a shell green hand and just started. It seems stdout output are assigned to the shell variable. Could you point me to the documentation describing this kindly?
Thanks!
a will contain the output of the command returned to stdout. The error will be returned to sterr and so to get the error in the variable, you will need to redirect sterr to stout and so:
a=$(grep pattern file_not_exist 2>&1)
Here, 2 represent stdrr and 1 stdout.
This question already has answers here:
Capture stdout and stderr into different variables
(21 answers)
Closed 2 years ago.
I need to ensure a command is outputting the correct text. I currently have:
command_stdout="$(mycommand --flag 2>/dev/null)"
command_stderr="$(mycommand --flag 2>&1 1>/dev/null)"
Instead of having to run the same command twice, is there any way I can run it once but still be able to save stdout and stderr's output to their appropriate variables?
Redirect stderr to a file, then set the second variable to the file contents.
command_stdout="$(mycommand --flag 2>/tmp/stderr.$$)"
command_stderr="$(</tmp/stderr.$$)"
rm /tmp/stderr.$$
This question already has answers here:
What are the uses of the exec command in shell scripts? [closed]
(2 answers)
Closed 3 years ago.
Just editing a bash script and the last line is:
exec <&-
Hoping someone can explain. I'm guessing it has to do with the exit code of the previous command?
exec <&- closes standard input (filedescriptor 0).
exec with just redirections but not other argument applies the redirections to the current process.
A redirection to or from &- means close.
With numerical descriptors it doesn't matter if you do 0<&- or 0>&- — either version will close filedescriptor 0 (standard input). If you ommit the number > redirection means "use fildescriptor 1and<` means "use filedescriptor 0".
This question already has answers here:
Redirect stderr and stdout to separate files in Bash?
(2 answers)
Separately redirecting and recombining stderr/stdout without losing ordering
(2 answers)
Closed 4 years ago.
My script runs some function/external program. I need to print its output normally. Hovever, I also need to save stdout and stderr separately to two variables/files. Let's assume files beacuse that would probably be easier in a shell.
Here is a template of my script:
#!/bin/sh
print_things()
{
echo 'Some normal output'
echo 'Now errors' 1>&2
echo 'Normal output again'
echo 'And more errors' 1>&2
}
print_things | <do magic here>
<possibly a few more lines of magic>
# there will be the rest of the script
exit 0
I would like a solution that preserving division into stderr and stdout but it would still be acceptable if they get merged at some point, as long as they are saved separately.
The output has to preserve order of lines so it has to look like that:
Now errors
And more errors
Some normal output
Normal output again
After execution of the "magic" there should be two files in the folder. One that contains the whole stdout and one that contains stderr.
Bonus points for preserving the exit status without saving it to a file.
I'm looking for a posix compliant solution.
This question already has answers here:
How to store standard error in a variable
(20 answers)
Closed 6 years ago.
I have command
store=`stat /some/path`
echo ${store}>>file.txt
then I echo the variable result in a file
Sometimes the file doesn't exists and I get an error as stat: cannot stat xxxx
However, when this error occurs, it is echoed to the stdout than stored in my variable. I would like even the error to be printed out. How can I capture the same?
You don't.
You don't "capture" the error message I mean. Instead you check if the file exist before doing all this:
my_path="/some/path"
if [ -f "$my_path" ]; then
stat "$my_path" >> file.txt
fi
It should be noted that there is a race-condition here: The file could be removed between the check and the stat command. The chances for that happening are small, but it can still happen.