How to handle sub-command errors in bash script? [duplicate] - bash

This question already has answers here:
Why does "local" sweep the return code of a command?
(2 answers)
Why is bash errexit not behaving as expected in function calls?
(4 answers)
Closed 4 years ago.
I want to know how best to exit a script when an error occurs within a sub-command - specifically, in an assignment (i.e., of the form MYVAR="$(...)").
The minimal example of my problem is the following bash script.
#!/bin/bash
set -e
fail() {
echo "Some error" >&2
exit 1
}
main() {
local my_val="$(fail)"
echo 'Success!'
}
main
This will output the following:
Some error
Success!
What I am trying to figure out is how best to detect and handle the failure which occurs so that the Success stage is never reached.

Related

Calling a function in shell script having its name in a variable [duplicate]

This question already has answers here:
Difference between ${} and $() in Bash [duplicate]
(3 answers)
Invoke function whose name is stored in a variable in bash
(3 answers)
Closed 5 months ago.
I have a function like this
function test(){
local param1=${1}
local param2=${2}
echo "Hey ${param1} ${param2}"
}
And then, in another part of the script I have this
echo ${command}
$(command)
echo "Completed"
The output of that execution is
test param1 param2
Completed
So, as you can see, the function test was not executed.
I was looking a way to do that if possible in my shell script.
The good practice for this is described below:
command=(test a b)
echo ${command}
"${command[#]}"
echo "Completed"
The above code will use an array to store the command and the parameters, and it will call the function appropriately.
To specifically fix the expected behavior of your code, you could call the function using ${command} or $command, which is different from $(command). But the preferred practice, and more secure way of doing that is to follow the previous suggestion.
#!/bin/bash
function test(){
local param1=${1}
local param2=${2}
echo "Hey ${param1} ${param2}"
}
command="test a b"
echo ${command}
${command}
echo "Completed"

What does "not foundh" mean from a .sh script? [duplicate]

This question already has answers here:
Error With Bash Script [duplicate]
(5 answers)
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
I'm trying to run a script called autogen.sh on my Ubuntu system (WSL) which begins with
#!/bin/sh
set -xe
type autoreconf > /dev/null || exit 1
type pkg-config > /dev/null || exit 1
You can get the full script by running git clone https://github.com/universal-ctags/ctags.git
And it gives me
mark#surface-19:~/ctags$ ./autogen.sh
: not foundh: 2: ./autogen.sh:
./autogen.sh: 3: set: Illegal option -
I'm having a hard time understanding the error message "not foundh". It seems to be a typo of "not found" but what exactly is not being found?

Stop process as soon as a file contains "Success" [duplicate]

This question already has answers here:
How do I use a file grep comparison inside a bash if/else statement?
(5 answers)
How to kill a background process created in a script
(2 answers)
Closed 5 years ago.
I'm trying to stop a long running process as soon as the file /status contains the string Success.
I tried this without success:
cat & while [ `grep -q Success /status` ]; do sleep 1; done; kill %1
cat is the long running process that needs to be stopped when /status contains Success.
Cheers

Linux - Capture exit code of a ruby script [duplicate]

This question already has answers here:
Exit Shell Script Based on Process Exit Code [duplicate]
(9 answers)
Is it possible to get the exit code from a subshell?
(3 answers)
Closed 8 years ago.
I have a simple ruby script which uses the abort function to exit with a non-zero exit code
#!/usr/bin/env ruby
puts "I ran"
abort "Exiting"
How can I capture the exit code when I execute this command in bash?
I have tried exit_code=./test or exit_code=ruby test to no avail.
Thanks
Try this:
./test
echo $?
The special shell variable $? contains the exit code of the last terminated program.
It does not matter whether your program is a ruby program. All Unix programs have an exit code which is handled alike in the starting shell.
The exit code of the last program that ran is stored in $?
You find the exit code from the previously executed command in the variable $?.

Exit code of command substitution in bash local variable assignment [duplicate]

This question already has answers here:
Why does "local" sweep the return code of a command?
(2 answers)
Closed 6 years ago.
How can I check the exit code of a command substitution in bash if the assignment is to a local variable in a function?
Please see the following examples. The second one is where I want to check the exit code.
Does someone have a good work-around or correct solution for this?
$ function testing { test="$(return 1)"; echo $?; }; testing
1
$ function testing { local test="$(return 1)"; echo $?; }; testing
0
If you look at the man file for local (which is actually just the BASH builtins man page), it is treated as its own command, which gives an exit code of 0 upon successfully creating the local variable. So local is overwriting the last-executed error code.
Try this:
function testing { local test; test="$(return 1)"; echo $?; }; testing
EDIT: I went ahead and tried it for you, and it works.

Resources