1

I'm new to bash scripting and how it works. My following problem: Since the value returned from my function get_value is stored in a variable either (value1 or value2) my error message is not printed out to the user.
I only want the script to continue running if both value1 and value2 receives a valid value from the get_value function, but I would also like to notify the user which of them are missing and needs to be created.

I've simplified my two functions:

get_value()
{
  VALUE=$(*RUNNING COMMAND* 2> /dev/null)
  
  if [ -z $VALUE ]; then
     echo "ERROR Could not find value"
     exit 1
  else
     echo $VALUE    // Returning the value
  fi
}

print_value()
{
    VAR1="not_exists"
    VAR2="exists"    

    value1=$(get_value "$VAR1")
    value2=$(get_value "$VAR2")
    
    echo "$value1 and $value2"
}

Current output: (blank).
I know the value from the if-statement is saved in the variable that is calling the function, however I do not want the Error-message and the exit to be stored into the variable but rather printed out to the screen to notify the user.

N. J
  • 398
  • 2
  • 13
  • 2
    This is exactly what standard error (aka stderr) is for. Use `echo "ERROR Could not find value" >&2` (the `>&2` redirects it from stdout to stderr). Also, you have a bunch of variable references that should be double-quoted to avoid weird parsing; [shellcheck.net](https://www.shellcheck.net) is good at pointing out problems like this. Finally, you should probably use `return 1` (which returns from the function with an error status) rather than `exit 1` (which tries to exit the entire script with an error status, but doesn't because it's running in a subshell). – Gordon Davisson Nov 08 '22 at 19:39
  • It is not clear why you run get_value twice. Is that two different commands or the same command used to obtain a value for each of value1 and value2 ? – Eric Marceau Nov 08 '22 at 21:13
  • @EricMarceau, I was wondering if I should loop instead, however, this is a simplification of my actual script, where I need to get the two values `if` they exists. I've just pointed out a "sub-part" of what I want to accomplish. – N. J Nov 08 '22 at 21:45
  • Wow, @GordonDavisson. My head did not think about redirecting stdout to stderr, I only think about redirect as "get the stdout or stderr away from me", meaning `/dev/null` is usually my solution. THANKS! However, I don't really have use for the `return 1` that you point out, maybe that is best practice? I just want the script to end, because there's nothing more to be done. – N. J Nov 08 '22 at 22:11
  • Are you saying that there are scenarios where the two values would be reported for a single run ? If only one value from each run, why both value1 and value2 ??? Also, if you are not familiar with "trap" you may wish to consider its use for case dependent return of results. – Eric Marceau Nov 08 '22 at 22:30
  • @N.J Since the function is in `$( )`, it runs in a subshell, and therefore *cannot* exit the main script. If you want to exit the script if it fails, I'd recommend `return 1` in the function, and then use something like `value1=$(get_value "$VAR1") || exit $?` in the main script to actually do the exit. – Gordon Davisson Nov 08 '22 at 22:45
  • @EricMarceau, this script is actually meant to be looking up two values or secrets, with distinct name (e.g `secret1_for_backup`, and `secret2_for_config`). So I'm getting these values by running the `get_value()` function twice. There's no need for me to continue the script if one of these do not exists. – N. J Nov 09 '22 at 10:07
  • @GordonDavisson, I really like this. Do you have any resources? I would really like to build a bash script the way you describe and also based on best practices. – N. J Nov 09 '22 at 10:08
  • @N.J The main resources I recommend are [shellcheck.net](https://www.shellcheck.net), and the [BashFAQ](http://mywiki.wooledge.org/BashFAQ), [BashGuide](http://mywiki.wooledge.org/BashGuide), and [BashPitfalls list](http://mywiki.wooledge.org/BashPitfalls). SO's [bash tag info page](https://stackoverflow.com/tags/bash/info) also lists a lot of common questions and links some additional resources. I don't know of a good single source on best practices -- I've seen several that purport to be such, but they tend to recommend things like `set -e` that I consider bad ideas. – Gordon Davisson Nov 09 '22 at 10:35

0 Answers0