Iterate Through Array in Bash; Exit if No Matches are Found
Clash 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
bash
add a comment |Â
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
bash
you could set a variable likefound=0
before the loop. if you do find a matlab version installed, then setfound=1
, then after your loop test it:(( $found == 0)) && exit 1
â Tim Kennedy
Aug 13 at 19:10
add a comment |Â
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
bash
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
bash
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 likefound=0
before the loop. if you do find a matlab version installed, then setfound=1
, then after your loop test it:(( $found == 0)) && exit 1
â Tim Kennedy
Aug 13 at 19:10
add a comment |Â
you could set a variable likefound=0
before the loop. if you do find a matlab version installed, then setfound=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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
you could set a variable like
found=0
before the loop. if you do find a matlab version installed, then setfound=1
, then after your loop test it:(( $found == 0)) && exit 1
â Tim Kennedy
Aug 13 at 19:10