LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2018, 05:47 AM   #1
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Rep: Reputation: 30
Check if something isn't in an array


Sorry for the basic question but I have an array that's an exclusion list e.g.

Code:
exclusionList[0]='item 1'
exclusionList[1]='item 2'
While iterating over a larger list I want to check whether the current item is in the array and skip it if it is. I tried using the following

Code:
if [ '$exclusionList[*]' != '$currentItem' ]; then
but it never seems to match, what am I missing?
 
Old 10-23-2018, 06:43 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
What language? (Appears to be bash, just not sure)

And if it is bash, have you turned on debug?
 
Old 10-23-2018, 06:50 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
If bash, I think the notation should be:
Code:
'${exclusionList[*]}'
Because if you go to echo that array, you have to do it like this:
Code:
echo ${exclusionList[*]}
 
Old 10-23-2018, 06:54 AM   #4
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
In bash, I would say something like:
Code:
for item in ${#exclusionList[@]}
do
   if [[ $item != $currentItem ]]
   then
      <put_code here>
   fi
done
Of course you need to adapt it to your needs.
 
Old 10-23-2018, 07:08 AM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
that's how I'd do it, loop through the array and check against it. if 'true' then else 'not [true]' fi
but
Code:
$array[0]=H 
$array[1]=B 
$array[2]=J
 
curr=B

#this way I get no return 
$for d in ${#array[@]} ;  do if [[ $d == $curr ]] ; then echo got it $curr ; fi; done

for d in ${#array[@]} ;  do if [[ ${array[$d]} == $curr ]] ; then echo got it $curr ; fi; done

#here I get a return

for d in ${!array[@]} ;  do if [[ ${array[$d]} == $curr ]] ; then echo got it $curr ; fi; done
got it B
I'd like to point out.

Last edited by BW-userx; 10-23-2018 at 07:25 AM.
 
Old 10-23-2018, 09:35 AM   #6
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
Didn't know bash had debug flags. Googled it and found +x and -x which helped a lot.

Currently it looks like this:
Code:
exclusionList[0]='item 1'
exclusionList[1]='item 2'
exclusionList[2]='item 3'

testInput = 'item 1'

for item in $(echo "$testInput")
do
	for (( item=0; item<=2; item++ ))
	do
		if [[ $exclusionList[$item] == $item ]]
		then
			match=True
			break;
		fi
	done
	if [ $match==True ]
	then
		echo 'Excluding' $item
		break;
	else
		echo 'Including ' $item
	fi
done
From the debug I can see it only seems to be evaluating the testInput against the first array entry?
 
Old 10-23-2018, 09:51 AM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Mark_667 View Post
Didn't know bash had debug flags. Googled it and found +x and -x which helped a lot.


From the debug I can see it only seems to be evaluating the testInput against the first array entry?
Code:
testInput = 'item 1'
#needs to be
testInput='item 1'
no spaces between =

what are you trying to match the names or the number of the element?

what is this for
Code:
#for item in $(echo "$testInput")
#do
looky here
Code:
#!/bin/bash

set -x

exclusionList[0]='item 1'
exclusionList[1]='item 2'
exclusionList[2]='item 3'

testInput='item 1'

item=0
match=false

for item in $(echo "$testInput")
do
#	for item in ${!exclusionList[@]}
for (( item=0; item<=2; item++ ))
	do
		if [[ $exclusionList[$item] == $item ]]
		then
			match=True
			echo $item
			echo ${exclusionList[$item]}
			break;
		else
			echo "no match"
		fi
	done
	
	if [ $match == 'True' ]
	then
		echo 'Excluding' $item
		break;
	else
		echo 'Including ' $item
	fi
	

done
output set -x
Code:
└──╼ $./checkarrays
+ exclusionList[0]='item 1'
+ exclusionList[1]='item 2'
+ exclusionList[2]='item 3'
+ testInput='item 1'
+ item=0
+ match=false
++ echo 'item 1'
+ for item in $(echo "$testInput")
+ (( item=0 ))
+ (( item<=2 ))
+ [[ item 1[0] == 0 ]]
+ echo 'no match'
no match
+ (( item++  ))
+ (( item<=2 ))
+ [[ item 1[1] == 1 ]]
+ echo 'no match'
no match
+ (( item++  ))
+ (( item<=2 ))
+ [[ item 1[2] == 2 ]]
+ echo 'no match'
no match
+ (( item++  ))
+ (( item<=2 ))
+ '[' false == True ']'
+ echo 'Including ' 3
Including  3
+ for item in $(echo "$testInput")
+ (( item=0 ))
+ (( item<=2 ))
+ [[ item 1[0] == 0 ]]
+ echo 'no match'
no match
+ (( item++  ))
+ (( item<=2 ))
+ [[ item 1[1] == 1 ]]
+ echo 'no match'
no match
+ (( item++  ))
+ (( item<=2 ))
+ [[ item 1[2] == 2 ]]
+ echo 'no match'
no match
+ (( item++  ))
+ (( item<=2 ))
+ '[' false == True ']'
+ echo 'Including ' 3
Including  3
you're checking numbers against text, what is inside of the elements.

you reference arrays like this
Code:
${exclusionList[$item]}
to check the value within the array elements
Code:
if [[ ${exclusionList[$item]} == $testInput ]]

Last edited by BW-userx; 10-23-2018 at 10:10 AM.
 
1 members found this post helpful.
Old 10-23-2018, 10:08 AM   #8
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
Thanks, am trying to match the names so if the testInput contains a value in the array it'll be excluded.
The testInput contains multiple values, the outer loop iterates over each one. The inner loop checks this against each entry in the array to see if it should be processed. Sorry if this wasn't clear.
Quote:
you're checking numbers against text
Didn't know what you meant by this until I saw the code you posted. You reused the $item change the outer loop to:
Code:
for currentItem in $(echo "$testInput")
and the following if to:
Code:
if [[ $exclusionList[$item] == $currentItem ]]

Last edited by Mark_667; 10-23-2018 at 10:17 AM. Reason: Clarified numbers vs text
 
Old 10-23-2018, 10:18 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,695

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Code:
for item in $(echo "$testInput")
Code:
for (( item=0; item<=2; item++ ))
You have two loops with the same variable name.

Last edited by michaelk; 10-23-2018 at 10:24 AM.
 
Old 10-23-2018, 10:22 AM   #10
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by michaelk View Post
You have two loops with the same variable.
Sorry, that was a copy and paste fail on my part that gives a misleading idea of what I'm working with. See my last post for the correction.
 
Old 10-23-2018, 10:26 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by Mark_667 View Post
Thanks, am trying to match the names so if the testInput contains a value in the array it'll be excluded.
The testInput contains multiple values, the outer loop iterates over each one. The inner loop checks this against each entry in the array to see if it should be processed. Sorry if this wasn't clear.

Didn't know what you meant by this until I saw the code you posted. You reused the $item change the outer loop to:
Code:
for currentItem in $(echo "$testInput")
and the following if to:
Code:
if [[ $exclusionList[$item] == $currentItem ]]
your code says your first loop is redundant. the second loop is trying to match the number of the loop cycle against the name (value) within the array element.

your test var contains only 1 (one) value. It looks like you're wanting to match that value to one of the elements within the array, if no match then your 'match' var value=false.



https://www.unix.com/shell-programmi...ll-script.html

Code:
#!/bin/bash

set -x
#initiate array
exclusionList[0]='item 1'
exclusionList[1]='item 2'
exclusionList[2]='item 3'

#initiate variables
testInput='item 1'
item=0
match=false

	#same as second for condition
	#for item in ${!exclusionList[@]}

	for (( item=0; item<=2; item++ ))
	do
	 #looks at value within each element of array
	 #then compares it against the value within
	 # testInput var
	if [[ ${exclusionList[$item]} == $testInput ]]
		then
			match=True
			echo $item
			echo ${exclusionList[$item]}
			#if match break out of loop
			break;
		else
			echo "no match"
		fi
	done
	
	#then check the value of the match
        #flag
	if [ "$match" == 'True' ]
	then
		echo 'Excluding' $item
	 
	else
		echo 'Including ' $item
	fi


#run this within your script to see what
# item is doing

for item in ${!exclusionList[@]}
do
	echo $item
done

for (( item=0; item<=2; item++ ))
do
	echo $item
done
how array's and loops work
https://www.cyberciti.biz/faq/bash-for-loop-array/

Last edited by BW-userx; 10-23-2018 at 10:32 AM.
 
1 members found this post helpful.
Old 10-23-2018, 01:05 PM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
using space in items make it hard to handle.
Code:
for item in $(echo "$testInput")
# can be replaced by:
for item in $testInput
I suggest you to use associative arrays, that makes it much more easy:
Code:
exclusionList["item 1"]=1
exclusionList["item 2"]=1
exclusionList["item 3"]=1
...
You only need to check if exclusionList["anything"] exists.

Otherwise I suggest you to try www.shellcheck.net to check your script.
 
1 members found this post helpful.
Old 10-23-2018, 01:44 PM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Untested:
Code:
declare -A Excludes
Excludes['this']=1
Excludes['that']=1
if [ -n "${Exludes["$testvalue"]}" ]; then
 ...
fi
Oh I was late

Last edited by NevemTeve; 10-23-2018 at 01:45 PM.
 
Old 10-23-2018, 01:45 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,695

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
To elaborate on pan64's post.
Code:
testInput='item 1'

for currentitem in $(echo "$testInput")
do
  echo $currentitem
done
The for loop uses IFS to split the string which uses a space by default so the value of $currentitem will be
item
1
Code:
testInput='item 1'

for currentitem in "$testInput"
do
  echo $currentitem
done
However, the above loop will not split the string and so $currentitem will only loop once and be the value of
"item 1"

These are probably only test cases and may not be the real values. You can change the default value of IFS but that could also cause other problems if not careful with your loops.
 
Old 10-23-2018, 03:09 PM   #15
Mark_667
Member
 
Registered: Aug 2005
Location: Manchester, England
Distribution: Ubuntu 20.04
Posts: 383

Original Poster
Rep: Reputation: 30
Thanks, wasn't aware of the gotcha with spaces. I based it on another script which had the same set of inputs consisting of strings containing spaces so I didn't suspect that.
 
  


Reply

Tags
array, bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Check if a program is running, if it isn't start it? NotAComputerGuy Linux - Newbie 11 06-29-2013 02:22 PM
[SOLVED] perl check array qrange Programming 21 11-22-2012 03:27 PM
Array and check strings mech123 Linux - Newbie 6 05-03-2010 07:48 AM
Check if program is running, if not start it and enter a password... easy, isn't it? darkrad Linux - Server 11 07-17-2008 12:35 PM
How to check if array[0] doesn't exist? flamesrock Programming 3 11-05-2004 10:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration