-2
FROM_PROJECT=false
if [[ -z $INV_NAME ]]; then
    if [[ -z $PRO_NAME ]]; then
            echo "error: variable not set" >&2
            exit 1
    elif [[ -z $INV_FILE ]]; then
            echo "error: variable INV not set" >&2
            exit 1
    fi
    FROM_PRO=true
elif [[ -n $PRO_NAME ]] || [[ -n $INV_FILE ]]; then
    echo "error: variable INV_NAME is not compatible with PRO_NAME and             INVENTORY_FILE" >&2
    exit 1

i am not sure what these "-z" and "-n" doing here?

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
ATP3530
  • 1
  • 1
  • http://explainshell.com/ will give you an explanation of any single line of shell; `[[ -z $INV_NAME ]]` or `[[ -n $PRO_NAME ]]` can be used there. – Charles Duffy Feb 06 '17 at 21:31
  • BTW, using `true` and `false` strings as conditionals is very bad practice. Code that does so typically also does something like `if $FROM_PROJECT; then ...`, and thereby performs semi-arbitrary code execution (if your variables' contents can be given arbitrary values, adding something a security review needs to check for). Instead, use empty/non-empty state to make a boolean determination. – Charles Duffy Feb 06 '17 at 21:34
  • (One can also use `0` and `1` as booleans easily enough; `if (( some_value ))` will be truthy if `some_value` holds a positive integer value, or the name of a variable which itself holds a positive integer value). – Charles Duffy Feb 06 '17 at 21:36
  • @CharlesDuffy - Just FYI, [explainshell.com](http://explainshell.com) barfs on `[[ -z $INV_NAME ]]` "parsing error! unexpected token '-z' (position 3)" – Andrew Cottrell Feb 06 '17 at 21:38
  • Note also http://stackoverflow.com/questions/18096670/what-does-z-mean-in-bash – Charles Duffy Feb 06 '17 at 21:40
  • @AndrewCottrell, thank you -- filed as https://github.com/idank/explainshell/issues/174 – Charles Duffy Feb 06 '17 at 21:43

1 Answers1

2

-z is used to test if a string is empty.

-n is the opposite.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101