Test variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test variable
# 1  
Old 04-18-2008
Test variable

hi,
i store a constant in a variable and want to test whether it is M or Z.
how can i achieve this?? please help
Code:
variable='M'
if [ $variable = 'M' ]
then
ehco 'success'
fi;

is this right ??

Last edited by Yogesh Sawant; 04-18-2008 at 04:58 AM.. Reason: added code tags
# 2  
Old 04-18-2008
Quote:
Originally Posted by javeed7
hi,
i store a constant in a variable and want to test whether it is M or Z.
how can i achieve this?? please help
Code:
variable='M'
if [ $variable = 'M' ]
then
ehco 'success'
fi;

is this right ??
i will let somebody else answer that. i prefer case
Code:
case $variable in 
 "M" ) echo "yup " ;;
 * ) echo "nope" ;;
esac

# 3  
Old 04-19-2008
Quote:
Originally Posted by javeed7
hi,
i store a constant in a variable and want to test whether it is M or Z.
how can i achieve this?? please help
Code:
variable='M'
if [ $variable = 'M' ]
then
ehco 'success'
fi;

is this right ??

Like ghostdog, I prefer to use case, but yours will work (almost).

The variable should be quoted or you will get a syntax error if it is empty.

Code:
variable=M
if [ "$variable" = M ]
then
  echo success
fi

#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Files test How to recover a specific variable

Hi everyone !;) I'm Arnaud and I'm new on this forum, I begin in the Shell. Let me explain, I want to test the presence of file, so far so good. Below, my code : if && && ; then Pgsql ; else echo \nLe fichier $fichier n\'existe pas ; I would like to recover in a... (16 Replies)
Discussion started by: Arnaudh78
16 Replies

2. Programming

Oracle Variable Passing Test

Hi, I am trying to get the oracle variables and pass the values in sql placed in procedure. VARIABLE vstat='ASDS,FGDS,VCGD,VCXC' Query : select distinct dept from College where section in ('C','D') AND CODES ='' AND NAMES IN ('RAJ','SAM'); I want CODES values to be taken from vstat... (1 Reply)
Discussion started by: Perlbaby
1 Replies

3. Shell Programming and Scripting

Value of variable is NULL, but test doesn't seem to recognize

Hello, Unix-forums! My problem: read -p "Enter any number, please" number sleep 1 echo $number | tr -d 0-9 test -z $number && echo "Thank you" || echo "This is not a number"Test always displays "This is not a number". It doesn't matter if I entered a or 1. But if I order echo... (2 Replies)
Discussion started by: intelinside
2 Replies

4. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

5. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

6. UNIX for Dummies Questions & Answers

Ksh How to test if variable is numeric??

I'm trying to test and see whether a variable that is being extracted from a line in a file is numeric or not. I have tried everything that I can think of and I cannot figure out how to get it to work. I am trying to test and see if the string extracted contains 5 numeric digits. This is what I... (8 Replies)
Discussion started by: developncode
8 Replies

7. Shell Programming and Scripting

variable numerical test

Hi I have a variable which should be any number between 1 and 50. It could also be any string/empty string. I have a code written below. The point is when the variable contains string. I don't want the code below to error out. Instead fall in the else bucket. && dothis_1 || dothis_2 (1 Reply)
Discussion started by: tostay2003
1 Replies

8. Shell Programming and Scripting

Test whether absolute path in variable

I'm using the following line in bash to test whether an argument supplied is an absolute path or not: if echo $1 | grep '^/' > /dev/null then absolute=1 else absoute=0 fi The line appears to work but seems somewhat unprofessional looking to me. Is it an acceptable... (2 Replies)
Discussion started by: dkieran
2 Replies

9. Shell Programming and Scripting

test Null variable

hi forum i beginning with script and i want test un null variable in a schell i just don t know the syntax here is a litle example y=test echo $y unset y echo $y (so here Y = Null) if Y=Null then echo "y is null" exit fi (1 Reply)
Discussion started by: kykyboss
1 Replies

10. Shell Programming and Scripting

How to test if a variable is in the right format

How can I test a variable to see if its in the right format? The format for the variable would be 'DDMMYYYY HH:MM:SS' and is passed from a command line argument. Any help would be appreciated (2 Replies)
Discussion started by: dbrundrett
2 Replies
Login or Register to Ask a Question