variable declaration


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable declaration
# 1  
Old 05-09-2011
variable declaration

how to check

1. If variable is declared or not
2. If any value if assigned to variable or not.

in UNIX shell script
# 2  
Old 05-09-2011
read man page on test command
# 3  
Old 05-09-2011
Quote:
Originally Posted by ace_friends22
how to check

1. If variable is declared or not
2. If any value if assigned to variable or not.

in UNIX shell script
Have a read of your man page of test.



Edit: 116@434 was faster! Smilie
# 4  
Old 05-09-2011
hi, i have gone thru it.
One of my file contains variable and its value (A to E )
param.ksh
----------
Code:
IF [ $1 = "MYDB" ]
then
 export A=123
 export B=345
 C=789
 D=
fi

Now this script i am invoking from other shell scropt
Code:
 
. param.ksh

here, i need to check that all 5 (A,B,C,D,E) variables should be declared and assigned values to it, in order to proceed further. If any one of the variable is not declared or value is not assigned to it. process should stop and exit
# 5  
Old 05-09-2011
use an and function

Code:
if [ $A="" -a $B="" -a ... etc ] then echo "error" exit error_code else do something... fi

if [ $A="" ... ] checks if value is NULL

not tested with more than 2 variables but it should work
# 6  
Old 05-09-2011
@metal005 Your script as posted contains syntax and logic errors. Needs to be an OR test and all variables should be quoted because we need to look at empty variables.

Maybe you meant:

Code:
# Error if parameter not set
set -u
# Error if parameter empty
if [ -z "$A" -o -z "$B" -o -z "$C" -o -z "$D" -o -z "$E" ]
then
        echo "One or more parameters is empty"
        exit
fi

This User Gave Thanks to methyl For This Post:
# 7  
Old 05-09-2011
ofcourse sorry my bad

it has to an or function...
wassnt thinking clear

your way should work great though... good programming
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

bash variable string declaration

Hello, Why is this not working in a script? files="test.fsa" echo $files for file in $files do if then echo "$file does not exist." fi run a command done I get an error saying (3 Replies)
Discussion started by: verse123
3 Replies

2. Shell Programming and Scripting

variable declaration

Hi Guys, What does this mean actually ? Can somebody give me any explanation ? x=${x:=1} Thanks (2 Replies)
Discussion started by: amit.behera
2 Replies

3. Emergency UNIX and Linux Support

Merge Static and dynamic parts in variable declaration

Dear Unix experts Moved from "Shell Programming and Scripting " I want to define a variable which contains dynmic and static part, daynamic part is the first field. Sample of data dddd aaaa sssss 12345 ssss 2323 234234 4242 dddd 3223 34234 54353 ssss 24234 3434 42342 dddd rwrw 423423... (2 Replies)
Discussion started by: yahyaaa
2 Replies

4. Shell Programming and Scripting

Doubt in Perl Variable declaration

Anyone please say what is the difference between $var and ${var} in perl Sometimes $var used and sometimes ${var} used in same program. Thanks in Advance, Prabhu ---------- Post updated at 09:34 AM ---------- Previous update was at 05:59 AM ---------- Any one please clarify (1 Reply)
Discussion started by: prsampath
1 Replies

5. Shell Programming and Scripting

Merge Static and dynamic parts in variable declaration

Dear Unix experts I want to define a variable which contains dynmic and static part, daynamic part is the first field. Sample of data dddd aaaa sssss 12345 ssss 2323 234234 4242 dddd 3223 34234 54353 ssss 24234 3434 42342 dddd rwrw 423423 werwer nawk 'BEGIN {FS=" "}{... (4 Replies)
Discussion started by: yahyaaa
4 Replies

6. Shell Programming and Scripting

Perl variable declaration

what is the meaning of this particular line of code in perl. my %global_port2lanid = (); (2 Replies)
Discussion started by: suvenduperl
2 Replies

7. Shell Programming and Scripting

Global variable declaration in shell scripting

Hi all, How to print variable value which i have assigned inside for loop because that variable scope is local, but i want to use that variable outside for loop. Awaiting your great help Thanks, Susil (1 Reply)
Discussion started by: susilgandhi
1 Replies

8. Shell Programming and Scripting

double variable declaration

i have variables with different values as below in KSH AU_Holiday=1 SG_Holiday=0 KR_Holiday=1 JP_Holiday=0 $country_cd is the second variable which be one of 'AU' 'SG' 'KR' 'JP' which comes in a for loop. form that i need to derive the variable like first one and check whether it is equal... (3 Replies)
Discussion started by: kotasateesh
3 Replies

9. Shell Programming and Scripting

Help with variable declaration

I declared a variable x that gets the count(*) from a table. The table name is also defined as a variable. What's wrong with this statment : X=” select count(*) from ${table_name}“ then y = `${X}${table_name}' echo ${y} It throws an error saying count not found. Please... (1 Reply)
Discussion started by: dsravan
1 Replies

10. Programming

Variable declaration

what does this mean when a variable is declared as register int i; Thanks. :confused: (2 Replies)
Discussion started by: laila63
2 Replies
Login or Register to Ask a Question