![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to check null variable | sachin.gangadha | Shell Programming and Scripting | 2 | 12-06-2007 04:59 PM |
| How to check a string in the variable | josephwong | Shell Programming and Scripting | 1 | 06-26-2006 12:14 AM |
| Script to check for a file, check for 2hrs. then quit | mmarsh | UNIX for Dummies Questions & Answers | 2 | 09-16-2005 02:46 PM |
| check for NULL variable | esham | Shell Programming and Scripting | 2 | 03-20-2005 03:31 AM |
| Check if variable is a number | handak9 | UNIX for Dummies Questions & Answers | 2 | 03-01-2005 08:27 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Check if variable is set in script
I want to check to see if a variable is set - and if not set it to something
ie. variable name test I want to check if $test is set then if there is nothing set against this currently - then set it to 0 Whats the best / shortest way of doing this in a script? |
|
|||||
|
Use ksh. ksh has shell builtin's to check for those conditions.
From man ksh under the Parameter sub-heading. Code:
Modifiers can be applied to the ${name} form of parameter substitution:
${name:-word}
if name is set and not null, it is substituted, otherwise word
is substituted.
${name:+word}
if name is set and not null, word is substituted, otherwise
nothing is substituted.
${name:=word}
if name is set and not null, it is substituted, otherwise it is
assigned word and the resulting value of name is substituted.
${name:?word}
if name is set and not null, it is substituted, otherwise word
is printed on standard error (preceded by name:) and an error
occurs (normally causing termination of a shell script, function
or .-script). If word is omitted the string ?parameter null or
not set? is used instead.
In the above modifiers, the : can be omitted, in which case the condi-
tions only depend on name being set (as opposed to set and not null).
If word is needed, parameter, command, arithmetic and tilde substitu-
tion are performed on it; if word is not needed, it is not evaluated.
Vino |
|
||||
|
Code:
VAR=${VAR:=default_value}
Another less elegant but maybe more clear way is: Code:
[ "$VAR" ] || VAR=default_value Last edited by hadarot; 08-31-2005 at 04:05 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|