If condition with array


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If condition with array
# 1  
Old 09-14-2015
If condition with array

hi,

say if my array has the following integer values.
Code:
array=( 5 10 15 20 )

and A assigns to 5.
Code:
A=5

How do I if condition to check against if value A is in this array or not, in shell script?

thx

Last edited by rbatte1; 09-14-2015 at 01:09 PM.. Reason: Code tags
# 2  
Old 09-14-2015
I'd make a helper function:

Code:
 in_array() {
   local n=$1 h
  
   shift
   for h; do
     [[ $n = "$h" ]] && return
   done
  
   return 1
 }
  
 if in_array "$A" "${array[@]}"; then
   ... do something about it ...
 fi

This User Gave Thanks to neutronscott For This Post:
# 3  
Old 09-14-2015
That's a nice function that hides the loop complexity!
--
bash-4 has associative arrays.
The assignment needs a loop (I think), but the lookup is easy
Code:
declare -A ARRAY
# assignment
for i in 5 10 15 20
do
  ARRAY[$i]=
done
A=5
# lookup
if [ -n "${ARRAY[$A]+x}" ]; then
  ... found, do something
fi

# 4  
Old 09-14-2015
Try also
Code:
while [[ ($i -lt ${#array[@]}) && (${array[$i]} != $A) ]]; do (( i++)); done ; echo $(( $i == ${#array[@]} ))

# 5  
Old 09-14-2015
Code:
for i in "${array[@]}"
do
  if [ "$i" = 5 ]; then
    echo yes
    break
  fi
done

For a numerical comparison, replace [ "$i" = 5 ] with [ "$i" -eq 5 ]
# 6  
Old 09-14-2015
thanks all. got it working now.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh : Building an array based on condition result

I want to build an Errorlog. I would like to build an array as I move through the if statements and print the array once all error conditions have been defined. The results need to be comma delimited. tsver will be static "1.9.6(2)" other vars $prit $lt $rt can have the same or a different... (1 Reply)
Discussion started by: popeye
1 Replies

2. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 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

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

5. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. HP-UX

Difference between [condition] and [[condition]] and ((condition)) when used with if condition

Executed the following if conditions .. and got different results . only (( )) gave correct o/p with all scenarios . Can anybody please let me know what is the difference between and ] and ((condition)) when used with if condition. And why each condition gave different result. 1.... (2 Replies)
Discussion started by: soumyabubun
2 Replies

7. Shell Programming and Scripting

Perl Array Condition

Hello, I want to check if all element of an array have the same value regardless the length of the array. example: @array1 = qw(44 44 44 44); @array2 = qw(55 55 55 55 55 55 55); Please advice, Ahmed (11 Replies)
Discussion started by: ahmed_zaher
11 Replies

8. Shell Programming and Scripting

IF condition against a ARRAY in shell script

Hi, I want to check a particular string inserted by User to be checked against the values i already have in a ARRAY string using IF condition. Is this possible? if yes how to do that. example : i have a,b,c,d,e,f values in a array called values i asked user to enter a value: user entered... (2 Replies)
Discussion started by: kukretiabhi13
2 Replies

9. Shell Programming and Scripting

awk - array elements as condition

Hi, can I use array elements ( all ) in conditional statements? the problem is ,the total number of elements is not known. e.g A is an array with elements - 1,2,3 now if i want to test if the 1 st field of input record is either 1,2 or 3, i can do something like this if ( $1 ~... (1 Reply)
Discussion started by: shellwell
1 Replies

10. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies
Login or Register to Ask a Question