The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-17-2005
frustrated1 frustrated1 is offline
Registered User
  
 

Join Date: Aug 2003
Location: Ireland
Posts: 278
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?
  #2 (permalink)  
Old 08-17-2005
azmathshaikh azmathshaikh is offline
Registered User
  
 

Join Date: Mar 2005
Location: Hyderabad, India
Posts: 22
Initialize the variable to some value say 'x'.

var="x"

if(var ne "x") { the variable contains some value other than 'x' }
else { the variable is not changed }
  #3 (permalink)  
Old 08-17-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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.
Have a look at examples given in the orielly book Learning the Korn Shell

Vino
  #4 (permalink)  
Old 08-19-2005
hadarot hadarot is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 33
Code:
VAR=${VAR:=default_value}
if VAR is already set (and not null), will be reset with its own value; else will be set to "default_value". In your case, you can put 0 for this 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..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0