ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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
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
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.
#!/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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.