Iterate Through Array in Bash; Exit if No Matches are Found

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












In the following function, I can iterate through an array and, if a match is found, break out of the loop and continue with the rest of the program (as intended).



I'd like, though, for the function to exit the rest of the program if exactly no matches are found. How can I achieve that? (I can't have it exit the first time a match is not found, and placing an exit 1 at the end of the loop doesn't do what I intend).



I'm sure I'm overlooking something obvious, but what?



#!/usr/bin/env bash 

# Array
MATLAB_VERSION=(
MATLAB9.4.app
MATLAB9.3.app
MATLAB9.2.app
MATLAB9.1.app
MATLAB9.0.app
MATLAB8.6.app
MATLAB8.5.app
MATLAB8.3.app
MATLAB8.0.app
MATLAB7.5.app
MATLAB.app
)

matlab_check()
# is MATLAB*.*.app installed in /Applications?
# iterate through array & tell me what you find

for MATLAB in "$MATLAB_VERSION[@]";
do
if [ -d "/Applications/$MATLAB" ]; then
printf "%s\n" "FOUND $MATLAB IN /Applications, CONTINUING..."
break
else
printf "%s\n" "SEARCHING for $MATLAB in /Applications..."
fi
done


matlab_check









share|improve this question























  • you could set a variable like found=0 before the loop. if you do find a matlab version installed, then set found=1, then after your loop test it: (( $found == 0)) && exit 1
    – Tim Kennedy
    Aug 13 at 19:10














up vote
1
down vote

favorite












In the following function, I can iterate through an array and, if a match is found, break out of the loop and continue with the rest of the program (as intended).



I'd like, though, for the function to exit the rest of the program if exactly no matches are found. How can I achieve that? (I can't have it exit the first time a match is not found, and placing an exit 1 at the end of the loop doesn't do what I intend).



I'm sure I'm overlooking something obvious, but what?



#!/usr/bin/env bash 

# Array
MATLAB_VERSION=(
MATLAB9.4.app
MATLAB9.3.app
MATLAB9.2.app
MATLAB9.1.app
MATLAB9.0.app
MATLAB8.6.app
MATLAB8.5.app
MATLAB8.3.app
MATLAB8.0.app
MATLAB7.5.app
MATLAB.app
)

matlab_check()
# is MATLAB*.*.app installed in /Applications?
# iterate through array & tell me what you find

for MATLAB in "$MATLAB_VERSION[@]";
do
if [ -d "/Applications/$MATLAB" ]; then
printf "%s\n" "FOUND $MATLAB IN /Applications, CONTINUING..."
break
else
printf "%s\n" "SEARCHING for $MATLAB in /Applications..."
fi
done


matlab_check









share|improve this question























  • you could set a variable like found=0 before the loop. if you do find a matlab version installed, then set found=1, then after your loop test it: (( $found == 0)) && exit 1
    – Tim Kennedy
    Aug 13 at 19:10












up vote
1
down vote

favorite









up vote
1
down vote

favorite











In the following function, I can iterate through an array and, if a match is found, break out of the loop and continue with the rest of the program (as intended).



I'd like, though, for the function to exit the rest of the program if exactly no matches are found. How can I achieve that? (I can't have it exit the first time a match is not found, and placing an exit 1 at the end of the loop doesn't do what I intend).



I'm sure I'm overlooking something obvious, but what?



#!/usr/bin/env bash 

# Array
MATLAB_VERSION=(
MATLAB9.4.app
MATLAB9.3.app
MATLAB9.2.app
MATLAB9.1.app
MATLAB9.0.app
MATLAB8.6.app
MATLAB8.5.app
MATLAB8.3.app
MATLAB8.0.app
MATLAB7.5.app
MATLAB.app
)

matlab_check()
# is MATLAB*.*.app installed in /Applications?
# iterate through array & tell me what you find

for MATLAB in "$MATLAB_VERSION[@]";
do
if [ -d "/Applications/$MATLAB" ]; then
printf "%s\n" "FOUND $MATLAB IN /Applications, CONTINUING..."
break
else
printf "%s\n" "SEARCHING for $MATLAB in /Applications..."
fi
done


matlab_check









share|improve this question















In the following function, I can iterate through an array and, if a match is found, break out of the loop and continue with the rest of the program (as intended).



I'd like, though, for the function to exit the rest of the program if exactly no matches are found. How can I achieve that? (I can't have it exit the first time a match is not found, and placing an exit 1 at the end of the loop doesn't do what I intend).



I'm sure I'm overlooking something obvious, but what?



#!/usr/bin/env bash 

# Array
MATLAB_VERSION=(
MATLAB9.4.app
MATLAB9.3.app
MATLAB9.2.app
MATLAB9.1.app
MATLAB9.0.app
MATLAB8.6.app
MATLAB8.5.app
MATLAB8.3.app
MATLAB8.0.app
MATLAB7.5.app
MATLAB.app
)

matlab_check()
# is MATLAB*.*.app installed in /Applications?
# iterate through array & tell me what you find

for MATLAB in "$MATLAB_VERSION[@]";
do
if [ -d "/Applications/$MATLAB" ]; then
printf "%s\n" "FOUND $MATLAB IN /Applications, CONTINUING..."
break
else
printf "%s\n" "SEARCHING for $MATLAB in /Applications..."
fi
done


matlab_check






bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 13 at 20:05









ilkkachu

51.1k678141




51.1k678141










asked Aug 13 at 18:43









marshki

400129




400129











  • you could set a variable like found=0 before the loop. if you do find a matlab version installed, then set found=1, then after your loop test it: (( $found == 0)) && exit 1
    – Tim Kennedy
    Aug 13 at 19:10
















  • you could set a variable like found=0 before the loop. if you do find a matlab version installed, then set found=1, then after your loop test it: (( $found == 0)) && exit 1
    – Tim Kennedy
    Aug 13 at 19:10















you could set a variable like found=0 before the loop. if you do find a matlab version installed, then set found=1, then after your loop test it: (( $found == 0)) && exit 1
– Tim Kennedy
Aug 13 at 19:10




you could set a variable like found=0 before the loop. if you do find a matlab version installed, then set found=1, then after your loop test it: (( $found == 0)) && exit 1
– Tim Kennedy
Aug 13 at 19:10










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










You can return from the whole function immediately when you find a match. That way, you can have a "tail part" after the loop, that only runs if no matches were found. Something like this:



#!/usr/bin/env bash 
# You might not care for this declaration of the array contents,
# but it does the same thing, and keeps my example nice and short
MATLAB_VERSION=( MATLAB9.4..0,8.6,5,3,0,7.5,.app )

# RC 0 = found
# RC 1 = not found
matlab_check()
for MATLAB in "$MATLAB_VERSION[@]"; do
if [ -d "/Applications/$MATLAB" ]; then
echo "Found in $MATLAB"
return 0
fi
done

return 1


matlab_check
echo rc is $?


And if you didn't want to be outputting details of where found, the if..fi portion could be reduced down to just this. No need to use return 0 because at this point we know that $? must be 0, hence just return with no arguments.



[ -d "/Applications/$MATLAB" ] && return





share|improve this answer


















  • 1




    Ah, that's more like it. I'll test a bit later and follow-up thereafter.
    – marshki
    Aug 13 at 20:38










  • I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
    – marshki
    Aug 14 at 13:03










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462368%2fiterate-through-array-in-bash-exit-if-no-matches-are-found%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










You can return from the whole function immediately when you find a match. That way, you can have a "tail part" after the loop, that only runs if no matches were found. Something like this:



#!/usr/bin/env bash 
# You might not care for this declaration of the array contents,
# but it does the same thing, and keeps my example nice and short
MATLAB_VERSION=( MATLAB9.4..0,8.6,5,3,0,7.5,.app )

# RC 0 = found
# RC 1 = not found
matlab_check()
for MATLAB in "$MATLAB_VERSION[@]"; do
if [ -d "/Applications/$MATLAB" ]; then
echo "Found in $MATLAB"
return 0
fi
done

return 1


matlab_check
echo rc is $?


And if you didn't want to be outputting details of where found, the if..fi portion could be reduced down to just this. No need to use return 0 because at this point we know that $? must be 0, hence just return with no arguments.



[ -d "/Applications/$MATLAB" ] && return





share|improve this answer


















  • 1




    Ah, that's more like it. I'll test a bit later and follow-up thereafter.
    – marshki
    Aug 13 at 20:38










  • I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
    – marshki
    Aug 14 at 13:03














up vote
2
down vote



accepted










You can return from the whole function immediately when you find a match. That way, you can have a "tail part" after the loop, that only runs if no matches were found. Something like this:



#!/usr/bin/env bash 
# You might not care for this declaration of the array contents,
# but it does the same thing, and keeps my example nice and short
MATLAB_VERSION=( MATLAB9.4..0,8.6,5,3,0,7.5,.app )

# RC 0 = found
# RC 1 = not found
matlab_check()
for MATLAB in "$MATLAB_VERSION[@]"; do
if [ -d "/Applications/$MATLAB" ]; then
echo "Found in $MATLAB"
return 0
fi
done

return 1


matlab_check
echo rc is $?


And if you didn't want to be outputting details of where found, the if..fi portion could be reduced down to just this. No need to use return 0 because at this point we know that $? must be 0, hence just return with no arguments.



[ -d "/Applications/$MATLAB" ] && return





share|improve this answer


















  • 1




    Ah, that's more like it. I'll test a bit later and follow-up thereafter.
    – marshki
    Aug 13 at 20:38










  • I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
    – marshki
    Aug 14 at 13:03












up vote
2
down vote



accepted







up vote
2
down vote



accepted






You can return from the whole function immediately when you find a match. That way, you can have a "tail part" after the loop, that only runs if no matches were found. Something like this:



#!/usr/bin/env bash 
# You might not care for this declaration of the array contents,
# but it does the same thing, and keeps my example nice and short
MATLAB_VERSION=( MATLAB9.4..0,8.6,5,3,0,7.5,.app )

# RC 0 = found
# RC 1 = not found
matlab_check()
for MATLAB in "$MATLAB_VERSION[@]"; do
if [ -d "/Applications/$MATLAB" ]; then
echo "Found in $MATLAB"
return 0
fi
done

return 1


matlab_check
echo rc is $?


And if you didn't want to be outputting details of where found, the if..fi portion could be reduced down to just this. No need to use return 0 because at this point we know that $? must be 0, hence just return with no arguments.



[ -d "/Applications/$MATLAB" ] && return





share|improve this answer














You can return from the whole function immediately when you find a match. That way, you can have a "tail part" after the loop, that only runs if no matches were found. Something like this:



#!/usr/bin/env bash 
# You might not care for this declaration of the array contents,
# but it does the same thing, and keeps my example nice and short
MATLAB_VERSION=( MATLAB9.4..0,8.6,5,3,0,7.5,.app )

# RC 0 = found
# RC 1 = not found
matlab_check()
for MATLAB in "$MATLAB_VERSION[@]"; do
if [ -d "/Applications/$MATLAB" ]; then
echo "Found in $MATLAB"
return 0
fi
done

return 1


matlab_check
echo rc is $?


And if you didn't want to be outputting details of where found, the if..fi portion could be reduced down to just this. No need to use return 0 because at this point we know that $? must be 0, hence just return with no arguments.



[ -d "/Applications/$MATLAB" ] && return






share|improve this answer














share|improve this answer



share|improve this answer








edited Aug 13 at 22:08

























answered Aug 13 at 19:16









steve

12.9k22149




12.9k22149







  • 1




    Ah, that's more like it. I'll test a bit later and follow-up thereafter.
    – marshki
    Aug 13 at 20:38










  • I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
    – marshki
    Aug 14 at 13:03












  • 1




    Ah, that's more like it. I'll test a bit later and follow-up thereafter.
    – marshki
    Aug 13 at 20:38










  • I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
    – marshki
    Aug 14 at 13:03







1




1




Ah, that's more like it. I'll test a bit later and follow-up thereafter.
– marshki
Aug 13 at 20:38




Ah, that's more like it. I'll test a bit later and follow-up thereafter.
– marshki
Aug 13 at 20:38












I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
– marshki
Aug 14 at 13:03




I'm marking the revised answer as correct. Placing the "tail" portion outside of the loop is correct. Thanks.
– marshki
Aug 14 at 13:03

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462368%2fiterate-through-array-in-bash-exit-if-no-matches-are-found%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?