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.
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.
$? 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.
$? - 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With