How to parse the error given by a bash script? [duplicate] - bash

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 2 years ago.
Given this script:
#!/bin/bash
COMMAND=$(echo test | jq)
if [[ $COMMAND == *"error"* ]]; then
echo failed
else
echo success
fi
Since the output of $(COMMAND) is the following: parse error: Invalid literal at line 2, column 0, I'd expect to have as output of that script failed, but I'm having success.
How can I parse the error that I got on scripts?

jq prints errors to stderr so you'd have to do this:
COMMAND=$(echo test | jq 2>&1)
But checking if command failed can be done using much easier method in
Bash:
#!/usr/bin/env bash
if ! echo test | jq '.'
then
echo failed
else
echo success
fi

Related

bash echo is doing commands and not adding what I need [duplicate]

This question already has an answer here:
echo bash code to .sh file to execute mixed up the variables
(1 answer)
Closed 3 months ago.
I need to create a new file name test.sh.
I need to write those lines with echo or somethings else but cant open new file manually and write it.
That is, I have to write down some things in the bash such a way that the following file is created:
TEST_VALUE=$1
if [[cat data | grep $TEST_VALUE]]; then
exit 1
fi
exit 0
But, when I do that by echo the result is:
TEST_VALUE=
if [[]]; then
exit 1
fi
exit 0
I need the file as I write it with $1 and not the argument and with the grep.
I tried to grep each row but it is doing the command and not copied it as I want.
How do I do it?
Thank You
Ignoring that the content you're trying to copy is buggy, the best way to do this is with a quoted heredoc:
cat >file <<'EOF'
TEST_VALUE=$1
if [[cat data | grep $TEST_VALUE]]; then
exit 1
fi
exit 0
EOF
But that content is buggy! A better version would look like:
cat >file <<'EOF'
#!/bin/sh
test_value=$1
! grep -q -e "$test_value" <data
EOF
echo 'TEST_VALUE=$1
if [[cat data | grep $TEST_VALUE]]; then
exit 1
fi
exit 0' > test.sh
You need to use quotes, in any other case your command will be executed.

Exit Code of command is 0, but binaries exit code is 3 [duplicate]

This question already has answers here:
Pipe output and capture exit status in Bash
(16 answers)
Closed 8 years ago.
I made a simple script:
$ more test.bash
#!/bin/bash
echo test
exit 1
When I run the script , the exit status should be 1
$ /tmp/test.bash
echo $?
1
But when I run this as the following
/tmp/test.bash | tr -d '\r' 1>>$LOG 2>>$LOG
echo $?
0
The exit status is 0, (not as expected 1)
It seems that the exit status comes from tr command.
But I what I want is to get the exit status from the script - test.bash.
What do I need to add/change in my syntax in order to get the right exit status from the script, and not from the command after the pipe line?
Use the PIPESTATUS array:
$ ls foo | cat
ls: foo: No such file or directory
$ echo ${PIPESTATUS[0]} ${PIPESTATUS[1]}
2 0
Note: PIPESTATUS is a bashism (i.e. not POSIX).

Automatically exit when bash command produce return code non zero [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Error handling in Bash [closed]
(15 answers)
Closed 4 years ago.
In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
There are lots of problems with using set -e. Just join the commands with &&, and test the result with an if statement.
if cd /something_something && mv file_a /somedir/file_a; then
echo $?
exit
fi
echo "Both cd and mv worked"

What is && and || in bash? [duplicate]

This question already has answers here:
What is the purpose of "&&" in a shell command?
(9 answers)
Closed 4 years ago.
I am trying to understand following pice of code:
for f in *.out; do sort -cg <$f &>/dev/null && res="sorted" || res="unsorted"; echo "File $f is $res."; done
For loop iterates through all .out files and gives each one as a parameter to sort, and the output of the sort is redirected into "nothing". But can someone explain what: && res="sorted" || res="unsorted" does?
A command after the && will only be executed if the previous command exited with a 0 (zero) status code. The other || works in the opposite way - a command after a || will only execute if the previous command exited with a non-zero exit code.
Here is a small example:
cat /some/file/that/is/missing && echo 'Found the file!' # doesn't print
cat /some/file/that/is/missing || echo 'Unable to find the file!' # will print
With the first line, the echo command will not execute since the cat command failed (because the file doesn't exist)
With the second line, we WILL see the echo command because the cat command failed.

What does “1>&2” mean in bash? [duplicate]

This question already has answers here:
What does " 2>&1 " mean?
(19 answers)
Closed 6 years ago.
What does 1>&2 mean in a bash script?
For instance, what does the following line from a bash script do?
echo "$1 is not a directory!" 1>&2
I use MacOS X. My bash script is:
if [ ! -d $1 ]; then
echo "$1 is not a directory" 1>&2
exit 1
fi
It outputs the message to stderr. Therefore 1>&2 means redirect stdout to stderr.
3. All about redirection

Resources