How to check the variable is present in array or not ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check the variable is present in array or not ?
# 1  
Old 01-04-2009
Question How to check the variable is present in array or not ?

Hi ,

I am trying to check wether the variable is present in the array. please see the below code .when ever i do this its taking only the first value of the array . please advise.


###Code Snnipet ###

#!/bin/ksh

set -xv

if [[ -n $1 ]]; then

echo " you have Specified the ORG ID - $1 "

else

echo " Please specify a valid three digit GNS-ORG-ID "

exit 1

fi


ORG=$1


set -A ORGID 211 233 244 300 143 234 222 564 586 810 975



#echo "${#ORGID[*] }"
#echo "${ORGID [2] }"

printf ${ORGID[*]}

if [[ $ORG == ${ORGID[*]} ]]; then <== Here its checking only with first value of the array ####
echo " Its a valid ORG ID "

else

echo " Its INVALID ORG ID - $1 "

echo " Please specify a valid ORG ID "

exit 1

fi
# 2  
Old 01-04-2009
Quote:
Originally Posted by padhu.47
Hi ,

I am trying to check wether the variable is present in the array. please see the below code .when ever i do this its taking only the first value of the array . please advise.

When posting code, please put it between [code] tags and post enough to demonstrate your problem, but no more.
Quote:
Code:
###Code Snnipet ###

#!/bin/ksh

set -xv

if [[ -n $1 ]]; then

echo " you have Specified the ORG ID - $1 "

else

echo " Please specify a valid three digit GNS-ORG-ID "

exit 1

fi


ORG=$1


set -A ORGID 211 233 244 300 143 234 222 564 586 810 975



#echo "${#ORGID[*] }"
#echo "${ORGID [2] }"

printf ${ORGID[*]}

if [[ $ORG == ${ORGID[*]} ]]; then    <== Here its checking only with first value of the array ####


case ${ORGID[*]} in
*"$ORG"*) echo valid ;;
*) echo Invalid ;;
esac
Quote:
Code:
echo " Its a valid ORG ID "

else

echo " Its INVALID ORG ID - $1 "

echo " Please specify a valid ORG ID "

exit 1

fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines present in first that match second variable. And ones that does not exist in second.

I have multi line input(var1) and reference(var2) variables. How to capture lines not present in var2 but present in var1? How to capture lines present var2 but not in var1? # configuration from server var1=""" Custom JAX-RS Custom Shared Web 2.0 """ # required configuration... (6 Replies)
Discussion started by: kchinnam
6 Replies

2. Shell Programming and Scripting

Perl script to verify that a value is present in an array (list)

I have 2 files , i need compare both files field by field, and in the fourth field some value will be interchaged and some value will be **. ex: file1 john|0.0|4|**:25;JP:50;UY:25 file2 john|0.0|4|JP:50;**:25;UY:25 (4 Replies)
Discussion started by: veeruasu
4 Replies

3. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

4. Shell Programming and Scripting

how can I check the first character of each variable stored in array

variables are stored in this format D0000, D12345, S1234, D12345 | KestrelPrdV2_0.build.177 - svn 141115 tag as V2.0.0.14 D0000, D12345, S1234, D12345 My question is how can I check the first character of each variable stored in array.It should be start with D or S. Please help... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

5. Shell Programming and Scripting

How read the name of present folder into a variable???

Dear all, Now I have a bunch of files need to be renamed. For instance, I have ten files in a folder with a name 'olefin', and I would like to change all the ten files name into 'olefin01,olefin02,...,olefin10'. I suppose it can be done with the command 'pwd'. However, the command 'pwd' will... (7 Replies)
Discussion started by: liuzhencc
7 Replies

6. Shell Programming and Scripting

check whether 3 files are present

I'm trying to check whether 3 files are existing and send 3 files as attachements. If only two are there then send those two files as attachments. if ; then elif ; then I tired the above given syntax and then it is giving me an error line 11: ' I tried with -a instead of && and... (3 Replies)
Discussion started by: Celvin VK
3 Replies

7. Shell Programming and Scripting

How to run a loop for assigning strings which are present in a file to an array

Hi Forum, I am struggling with the for loop in shell script. Let me explain what is needed in the script. I have a file which will conatin some strings like file1 place1 place2 place3 checkpoint some other text some more text Now what my requirement is the words ... (2 Replies)
Discussion started by: siri_14
2 Replies

8. Shell Programming and Scripting

Check whether the pattern is present or not?

I want to determine whether a specific pattern is present within a line or not e.g. The whole line is in a varaible called VALUE VALUE="(ABC, DEF, NMF, ABC, CLF, PAR, FHG, AGQSAs, sada, sa, ABC)" i want to set a flag to 1 if i find the presence of ABC in the above variable. Please... (8 Replies)
Discussion started by: skyineyes
8 Replies

9. UNIX for Dummies Questions & Answers

how to check if path is present?

following situation... - bourne shell script - sb. should entry a path and the script should look if the path exists, when not it should restart the Input ... echo "path\c" read Inp if ; then echo " path doesn't exist, try again... " (how to go back to the Inp?????) else echo "... (3 Replies)
Discussion started by: svennie
3 Replies
Login or Register to Ask a Question