0

I am trying to get url from the yml file. So if $NODES is null then return ENV variable URL or else the URL which is coming from yml file return that.

Here if is not working for me. If the condition is breaking. Also, local NODES variable value it prints null(when I did echo "$NODES")

My Code is something like this

SOURCE="$(dirname $0)/test.yml"

function query() {
  cat "$SOURCE" | yq -r "$@"
}

function get_urls() {
  local ENV="$1"
  local NODES=$(query ".env[\"$ENV\"].urls")
  if [ -z "$NODES" ];
  then
     echo $ENV_URL
  else
     query ".urls.$NODES[]" | paste -sd "," -
  fi
}

Here if I change if [ -z "$NODES" ] to if [ "$NODES" == "null"] or if [ "$NODES" == null ] Then script works.

But how to make it work using -z?

  • 3
    The string 'null' is not the empty string. – William Pursell Aug 07 '20 at 17:50
  • It's not harmful here, but do note that the `ENV` variable has special meaning (https://www.gnu.org/software/bash/manual/bash.html#Bash-Variables) -- see https://stackoverflow.com/q/28310594/7552 – glenn jackman Aug 07 '20 at 18:16
  • You could write `[ -z "${NODES/null/}" ]` to remove the string "null", but that seems needlessly complex. – glenn jackman Aug 07 '20 at 18:17
  • can you please tell me something on ${NODES/null/} slashes in double quotes? – Dharmesh Mehta Aug 07 '20 at 18:36
  • In Java terms, your question is "I have four letter string `String s="null";` I want to use `if(s.equals(""))` to recognize it but the comparison fails. I know I can use `s.equals("null")`, but how do I make it work with `""`?" The suggestion therefore is to use `if(s.replaceAll("null", "").equals(""))` to turn "anulled" into "aed" and "nullified" into "ified" or "null" into "", so that the comparison succeeds. It's obviously a very strange thing to do, but since you ask how to make the four letter string `"null"` compare to the zero letter empty string `""`, then that's what you have to do. – that other guy Aug 07 '20 at 19:03
  • Note that a lot of this syntax is only compatible with `bash`, not `sh`. In particular, a `sh` script shouldn't use the `function` keyword at all (and it's not even very good practice for a bash script, see https://wiki.bash-hackers.org/scripting/obsolete) – Charles Duffy Aug 07 '20 at 19:32
  • @Charles Duffy, Read again. It only says that `function` without curlies is obsolete (`function NAME { CMDS; }` is listed an acceptable replacement.) The OP is using curlies. – ikegami Aug 07 '20 at 19:35
  • @ikegami, afraid _you_ need to read again. `function` is present in more than one table in that page. `function funcname() {` is the _worst_ practice, being incompatible with *both* POSIX sh and legacy ksh, but `function funcname {` is still incompatible with POSIX sh with no compensatory benefit. – Charles Duffy Aug 07 '20 at 19:38
  • @Charles Duffy, I know it's not compatible with POSIX shell. I said it's not obsolete. `function NAME { CMDS; }` is only discouraged for compatibility reasons (which is a good reason to avoid it). – ikegami Aug 07 '20 at 19:39
  • ...how should a practice that breaks compliance with the standards document that's been controlling for POSIX-compliant `sh` implementations ever since there _was_ a POSIX standard for how `sh` should behave -- a practice which bash permits only for compatibility with pre-POSIX ksh -- not be considered obsolete? – Charles Duffy Aug 07 '20 at 19:40
  • I'm just going by the doc you linked. I only commented on what you claimed it says. – ikegami Aug 07 '20 at 19:41
  • The document I linked says this: *This table lists features that are used only if you have a specific reason to prefer it over another alternative. These have some legitimate uses if you know what you're doing, such as for those with specific portability requirements, or in order to make use of some subtle behavioral differences. These are frequently (mis)used for no reason. Writing portable scripts that go outside of POSIX features requires knowing how to account for many (often undocumented) differences across many shells.* – Charles Duffy Aug 07 '20 at 19:42
  • Older versions of bash would sometimes allow more flexibility on non-POSIX-compliant function names when the `function` keyword was used, so that's such a specific use, but it's much better practice to use standard-compliant function names and avoid the need (and new versions of bash have the same enforcement in either form). – Charles Duffy Aug 07 '20 at 19:43
  • @ikegami, ...anyhow, something that should only be used within unusual and typically-backwards-compatibility-related circumstances is syntax I feel well-founded in describing as "obsolete" – Charles Duffy Aug 07 '20 at 19:45

2 Answers2

0

You're missing the fi closing tag.

Example:

if [ -z "$var" ]
then
      echo "\$var is empty"
else
      echo "\$var is NOT empty"
fi

This works for me (it echos "ABC").

ENV_URL="ABC"
NODES=""

if [ -z "$NODES" ]; then
  echo $ENV_URL
else
  echo "FOO"
fi
aerickson
  • 96
  • 5
  • fi is there I missed here while writing – Dharmesh Mehta Aug 07 '20 at 18:23
  • 1
    Note that `echo "$ENV_URL"` should have a quote around the expansion; see [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo), and [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Aug 07 '20 at 21:00
0

how to make it work using -z?

-z checks if the length of the string is zero.

The length of null is 4, not zero. -z will not help you here.

ikegami
  • 367,544
  • 15
  • 269
  • 518