Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  9] [ 0]  / answers: 1 / hits: 86381  / 3 Years ago, sat, august 7, 2021, 7:24:42

I have a Bash shell function that takes an argument and performs something on it if needed.


do_something() {
if [need to do something on $1]
then
do it
return 0
else
return 1
fi
}

I want to call this method with several arguments and check if at least one of them succeeded.


I tried something like:


if [ do_something "arg1" ||
do_something "arg2" ||
do_something "arg3" ]
then
echo "OK"
else
echo "NOT OK"
fi

Also, I want to make sure that even if the first condition is true all other conditions will still be evaluated.


What is the correct syntax for that?


More From » bash

 Answers
3

Run the commands first, then check if at least one of them succeeded.



#!/bin/bash

success=0
do_something arg1 && success=1
do_something arg2 && success=1
do_something arg3 && success=1

if ((success)); then
printf 'Success! At least one of the three commands succeeded
'
fi

[#31254] Sunday, August 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
teromato

Total Points: 139
Total Questions: 102
Total Answers: 100

Location: Liechtenstein
Member since Mon, May 15, 2023
1 Year ago
;