This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 1 year ago.
I have been trying to write a program that will take the volume adjustment stat in Sox, and store it in a variable.
To do this you have to do
sox *your-audio-file* -n stat,
and the final line will show the stat that I want.
However, when I try to store the whole output of that command in the variable INITSTAT, it remains blank, and the line of code that should be storing the output in the variable is just printing the output to the terminal. This is what I have:
INITSTAT=`sox $audioFilePath -n stat`
echo $INITSTAT
Where "$audioFilePath" is the path to the audio file I am trying to get the information about.
If anybody knows what is wrong, any help would be appreciated.
I suggest to redirect stderr to stdout:
INITSTAT=$(sox "$audioFilePath" -n stat 2>&1)
Related
This question already has answers here:
print the output of strace command in a text file
(1 answer)
How to redirect and append both standard output and standard error to a file with Bash
(8 answers)
Closed 1 year ago.
I am trying to send all printed output of a strace command to a file. straces have a lot of console output since they print all the system calls of a command.
I have tried the most popular ways that I can find or think of, such as command > file.txt which does print output but then the output is not written to a file, same with command | tee file.txt Output is always printed but never written to a file. Is it because there is too much output or something? The first command definitely suceeds.
But my real question is what is the best way to do this and how can I get it done?
Many many thanks,
Milan
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 an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 3 years ago.
I am trying to automate the installation of nginx on multiple servers, and I have a shell script. It runs a version check if nginx is already installed and its version.
Trying to assign TMP=$(nginx -v), and instead of storing it in the variable it prints the results to console. printf "$TMP" prints an empty string
The problem is that your command does not print to STDOUT but to STDERR.
Using:
TMP=$(nginx -v 2>&1)
will solve your issue, see here for more details.
This question already has answers here:
How to store the output of a command in a variable at the same time as printing the output?
(4 answers)
Capture stdout to a variable but still display it in the console
(5 answers)
Closed 3 years ago.
I am writing bash script and inside I am executing a command. I want to save the output of the command to variable but also want to print the output of the command to the standard output. I dont want to print the variable once the command is completed. How can I achieve this ?
Try the following, it will print the output of your command and assign to variable.
VAR="$(your_command| tee /dev/tty)"
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.