Check if variable is set in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check if variable is set in script
# 1  
Old 08-17-2005
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  
Old 08-17-2005
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  
Old 08-17-2005
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  
Old 08-19-2005
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 05:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

2. Shell Programming and Scripting

how to set/get shell env variable in python script

greetings, i have a sh script that calls a python script. the sh script sets an env variable BIN: export BIN=bin64i need to get that BIN variable's value and use it within this python script. anyone know how to do this? thanx in advance. (5 Replies)
Discussion started by: crimso
5 Replies

3. Shell Programming and Scripting

Perl Variable Check Script

I am working on a perl script that is used to update a list of hosts to a certain file but I am having an issue when I try to perform a check to make sure the user enters valid information. The following is what I have currently written for the script: IPINPUT: print "Enter IP Address: ";... (2 Replies)
Discussion started by: Takau
2 Replies

4. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

5. Shell Programming and Scripting

KSH script eval(?) to set variable

first of all, thanks to all on this board, it has been a huge resource to answer most of my questions! I am stuck on something that should really be simple, and was looking for some help.. I am using KSH on solaris and working on a script to move containers from server to server. Where i am... (4 Replies)
Discussion started by: tksol
4 Replies

6. Shell Programming and Scripting

In a csh script, can I set a variable to the result of an SQLPLUS select query?

Can someone tell me why I'm getting error when I try to run this? #!/bin/csh -f source ~/.cshrc # set SQLPLUS = ${ORACLE_HOME}/bin/sqlplus # set count=`$SQLPLUS -s ${DB_LOGIN} << END select count(1) from put_groups where group_name='PC' and description='EOD_EVENT' and serial_number=1;... (7 Replies)
Discussion started by: gregrobinsonhd
7 Replies

7. Shell Programming and Scripting

Help with Script: If diff between files is nothing then set a variable

how about a script that compares 2 files using diff (or similar) and if they are then same then set a variable, v1=int else v1=0. thanks (2 Replies)
Discussion started by: tuathan
2 Replies

8. Shell Programming and Scripting

[bash] Check if variable is set or blank

Hello guys. In my script, i have the following code: echo "The tarfile contains these directorys" tar -tf file.tar > tarlist.txt cat tarlist | awk -F/ '{print $1 "/" $2}' | nl echo "Enter path of the directory you want to extract or just press enter to extract everything: " read path... (1 Reply)
Discussion started by: noratx
1 Replies

9. Shell Programming and Scripting

problem in getting the path of environment variable set in bashrc in my shell script

hi all i have joined new to the group. i have set an variable in my bashrc file. .bashrc PROGHOME=/home/braf/braf/prog export PROGHOME but while using it in my shell script its path is not taken and i had to explicitly give the export command to set the path. in my script... (8 Replies)
Discussion started by: krithika
8 Replies

10. Shell Programming and Scripting

Getting value of variable set in subprocess script

I am writing a shell script that executes another script by fetching it over the network and piping its contents into sh (ftp -o - $script | sh; or wget -O - |sh). Since this bypasses putting the script on the filesystem, this means I can't source the script directly (using . ), but rather it... (1 Reply)
Discussion started by: hadarot
1 Replies
Login or Register to Ask a Question