Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

null value variable

Status
Not open for further replies.

ozi403

Technical User
Apr 29, 2002
54
TR
Hi,

I have variable like this:

MB=`grep "$cli:" /var/adm/scripts/deneme|
grep "level="|grep -v "*"|cut -d"," -f2|grep mb`

If MB is a null value, how can I make MB=0 ?
 
[ -z "$MB" ] && MB=0

can be replaced by

: ${MB:=0}

Another solution is to use the following substition syntax when using your variable :

${MB:-0}

Parameter Substitution

${parameter}
The value, if any, of the parameter is substituted.
Braces are required only when parameter is followed by
a letter, digit, or underscore that is not to be
interpreted as part of its name. If parameter is * or
@, all positional parameters, starting with $1, are
substituted (separated by spaces). Parameter $0 is set
from argument zero when the shell is invoked.

${parameter:-word}
If parameter is set and is non-null, substitute its
value; otherwise substitute word.

${parameter:=word}
If parameter is not set or is null, set it to word; the
value of the parameter is then substituted. Positional
parameters cannot be assigned to in this way.

${parameter:?word}
If parameter is set and is non-null, substitute its
value; otherwise, print word and exit from the shell.
If word is omitted, the message ``parameter null or not
set'' is printed.

${parameter:+word}
If parameter is set and is non-null, substitute word;
otherwise substitute nothing.


Jean Pierre.
 
Another method in one run:
MB=`awk -F, '/\*/{next}
$0~"'$cli:'" && $0~"level=" && $2~"mb"{mb=(mb?mb"\n"$2:$2}
END{print(mb?mb:0)}
' /var/adm/scripts/deneme`

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top