简体   繁体   中英

"if [ -z $variable ]" in a shell script is not recognising my variable

When I run the following script it keeps defaulting to the else statement and I'm not sure why. I have even included an echo $workflow which showed me the name that I was expecting to see but it still keeps defaulting to the else statement.

I've tried including an export namecheck=$workflow to see if that was the issue but it still gave the same end result

#!/bin/sh

read -p "Enter the workflow name: " workflowName

workflowName=${workflowName}

curl --request GET --url https://website.com > workflowjson.txt

workflow=$(jq -r .metadata.name workflowjson.txt)

echo $workflow

if [ -z "$workflow" ] then
    echo "Workflow is Valid" 
else
    echo "Workflow is not valid" 
fi

man test shows

-z STRING the length of STRING is zero

The code outputs Workflow is Valid when the workflow is empty.

Switch the then and else parts, or negate the condition (use -n instead of -z ).

try taking the quotes out around workflow and put then in a separate line

if [ -z $workflow ]
then
  echo "Workflow is Valid" 
else
  echo "Workflow is not valid"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM