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.
Related
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)
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 1 year ago.
I need to read some properties from a config file and execute some commands if the properties match certain values.
But when I try to compare the value with a string in if condition, it doesn't work.
Below is a small version of what I am trying to do.
#!/bin/bash
source config.file
echo "mode: $mode"
if [ "$mode" = "slow" ];
then
echo "Mode is slow"
fi
The config file looks like this.
###config###
mode=slow
user=admin
password=pwd
###end###
The echo statement prints the values as "slow" but the if condition is never satisfied.
What am I doing wrong?
It works for me. Maybe you have MSWin line ends in the config file?
Run
dos2unix config.file
or
fromdos config.file
to convert them to the *nix standard.
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:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 7 years ago.
I am trying to run this simple bash script:
file_name=deploy
echo "Init File NAme $file_name"
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
echo "Current Time : $current_time"
new_fileName="${file_name}${current_time}.zip"
echo "New FileName: $new_fileName"
#echo $new_fileName ./app/code/community ./app/code/local ./app/design/frontend/indigo ./app/design/frontend/default
#zip $new_fileName ./app/code/community ./app/code/local ./app/design/frontend/indigo ./app/design/frontend/default
But for some reason I receive:
Init File NAme deploy
Current Time : 2015.10.01-16.04.02
./ManualPack.sh: line 5: $'\r': command not found
.zip5.10.01-16.04.02
I know its very very simple to any beginner in bash, but I tried for a decent amount of time, with many Stack Overflow threads to make it work, but the output remains the same.
You seem to have Windows styled line terminators in your file (\r\n). Converting the file with dos2unix should help.
This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 8 years ago.
For example, I'm writing a bunch of iptables rules in a bash script. When I run the script, shell says iptables: No chain/target/match by that name. I don't know what's going on so I copy and paste every line into shell and run them separately to figure out which line is causing trouble. BTW. it turns out that I put "OUTUT" instead of "OUTPUT" in one rule.
Is there anyway that shell can tell me like [line 53]: iptables: No chain/target/match by that name., so I know where the problem is?
I'm not bash expert but what I was doing is adding echo with information regarding the progress and read (wait until keypressed) that will let me do the process step by step.
Nearly all programs return success and error conditions. You can include error checking for each program your script calls, and take appropriate action on error (like undoing previous work, exiting out, etc). This is particularly useful if line 4 should never execute if line 3 fails.
The exit status of the program you just called is stored in $? .
Example (pseudocode - you'll need to modify the syntax to be correct)
Iptables foo bar baz; if ($? != 0) echo 'failed to update iptables' && exit 1; fi
additionally, you can turn on various levels of tracing with set -f , set -v , and set -x . See the links below for full details.
http://tldp.org/LDP/abs/html/exit-status.html
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html