Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What means the -z value in an if expression on a Linux script?

In this script I found this if expression:

if [ -z $1 ]; then
    echo "Usage: createpkg.sh <rev package>"
    exit
else
    CURRENT_VERSION=$1
fi

My problem is that I can't find what exactly means this -z value.

From the content of the echo I can deduct that (maybe) $1 variable represents the sotware version. and that (maybe) -z is a void value. So if I execute the script without passing to it the version of the software that I would packing it print me the correct procedure to execute the script.

But I am not sure about the real meaning of the -z value.

like image 991
AndreaNobili Avatar asked Oct 21 '13 13:10

AndreaNobili


People also ask

What is a value in Linux?

Unix Command Course for BeginnersThe value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

What is if $? 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded.

What does $? Meaning in a bash script?

$? - It gives the value stored in the variable "?". Some similar special parameters in BASH are 1,2,*,# ( Normally seen in echo command as $1 ,$2 , $* , $# , etc., ) . Follow this answer to receive notifications. edited Jun 20, 2020 at 9:12. CommunityBot.

What does in an IF conditional mean in shell script?

We use if-else in shell scripts when we wish to evaluate a condition, then decide to execute one set between two or more sets of statements using the result. This essentially allows us to choose a response to the result which our conditional expression evaluates to.


2 Answers

From man test:

   -z STRING
          the length of STRING is zero

So the condition:

if [ -z $1 ]; then

means "if the variable $1 is empty". Where $1 is probably the first parameter of the script: if you execute it like ./script <parameter1> <parameter2>, then $1=parameter1, $2=parameter2 and so forth.

like image 72
fedorqui 'SO stop harming' Avatar answered Sep 30 '22 18:09

fedorqui 'SO stop harming'


help test tells:

String operators:

  -z STRING      True if string is empty.

In your example, the script would print Usage: createpkg.sh <rev package> and exit if an argument was not supplied.

like image 34
devnull Avatar answered Sep 30 '22 18:09

devnull