check a list of vars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check a list of vars
# 1  
Old 09-17-2008
check a list of vars

I have about 20 different variables that I need to check for null values then replace with a specific string if they are null. I've been doing this via 20 different if then statements like this:

Code:
if [ "$VAR1" = "" ]; then
WIND="UUU"
fi

Is there a more elegant way to do this? The vars aren't sequential in their name by the way. Thanks for the suggestions as always!
# 2  
Old 09-17-2008
Quote:
Originally Posted by audiophile
The vars aren't sequential in their name by the way.
I don't think you have any other choice.

Regards
# 3  
Old 09-17-2008
It's not clear what you are replacing but perhaps echo ${VAR1-UUU} and other shell variable substitution mechanisms would help here.
# 4  
Old 09-17-2008
Check if your shell supports the below syntax:

Quote:
${parameter:=word}
Assign Default Values. If parameter is unset or null, the
expansion of word is assigned to parameter. The value of
parameter is then substituted. Positional parameters and spe‐
cial parameters may not be assigned to in this way.
# 5  
Old 09-17-2008
Thanks for the replies, all. I'm using BASH for this script. That ${parameter:=word} syntax is foreign to me...
# 6  
Old 09-17-2008
Quote:
Originally Posted by audiophile
Thanks for the replies, all. I'm using BASH for this script. That ${parameter:=word} syntax is foreign to me...

That's a basic parameter expansion available in all Bourne-type shells (which includes bash and ksh).

Code:
: ${var1:=UUU} ${othervar:=XXX} ${myvar:=MEMEME}

# 7  
Old 09-18-2008
Thanks for the info, all. It works very well and in a single line of code!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing vars or params to function

How to pass the getopts processed variable "${@}" to a function? It contains a list of package names needed in various functions. Seems the issue I have is due to the order of the script, with the processed "${@}" falling after the unprossed "${@}". I've been manually parsing options in the... (3 Replies)
Discussion started by: Cody Learner
3 Replies

2. Shell Programming and Scripting

Read vars iteratively

Hello, I have a tab delimited list of 311 server & account names which I want to read those 2 variables and then connect to each server and get info on that particular job account. I've tried the following: while read server acct; do printf "********$server\t $acct***********\n" ... (3 Replies)
Discussion started by: mcbobolink
3 Replies

3. Shell Programming and Scripting

List of Shell Env Vars

Hia, echo ${!S*} gives me all those env vars starting with S like SHELL SECONDS SHELLOPTS SHLVL etc. is there any way to deflate the shell variables' range like echo ${!A-E*} OR echo ${!A..S*} to list all env vars starting within range of A till E. Thanks Regards, Nasir (1 Reply)
Discussion started by: busyboy
1 Replies

4. Shell Programming and Scripting

getting vars from external files

Hi I have an issue, I want to get variables from an external file. Variable file var1=test var2-test2 I want to get these vars from another shell script. Does any one know how? (5 Replies)
Discussion started by: digitalviking
5 Replies

5. Shell Programming and Scripting

Difference between use vars and our variable in PERL

What is the difference between defining the global variable through our and using use vars ? Is the variable created using our goes beyond even package scope? Thanks in Advance !!! (3 Replies)
Discussion started by: jatanig
3 Replies

6. Shell Programming and Scripting

awk operating with shell vars only

Hi all How do I use awk such that it does not require an input file? I have a situation where I need to process some shell vars within awk (passed into awk with "-v VAR1=$VALUE1, VAR2=$VALUE2" etc), but that processing does not require/use an input file. Any guidance? TIA JG (2 Replies)
Discussion started by: jgrogan
2 Replies

7. Shell Programming and Scripting

NEW: need help with nawk using -v vars

I'm trying to pass nawk a shell variable to be used in a pattern match. I can't get this work. I'm calling nawk from a /bin/sh I want that when somebody enters Trunk Group in variable TGR so it goes into nawk variable TG. echo "Enter TRUNK GROUP:" read TGR cat... (20 Replies)
Discussion started by: wakhan
20 Replies

8. Shell Programming and Scripting

Passing Vars between scripts

Im running a script that runs scripts within it self and i need to pass vars made in the original script to scripts run within it and the only way i can think to do it is right the string to a file and read the file in the script (4 Replies)
Discussion started by: rcunn87
4 Replies

9. Shell Programming and Scripting

need help with nawk using -v vars

I'm trying to pass nawk a shell variable to be used in a pattern match. I can't get this work. I'm calling nawk from a /bin/sh echo " Input file: \c" read var1 echo " Input: \c" read var2 nawk -F"|" -v x=$1 ' BEGIN $15 ~ /^'$var2'/ {print $2}' var1 {apary=$15; bparty=$23; time=$4;... (3 Replies)
Discussion started by: amon
3 Replies
Login or Register to Ask a Question