Answer the question
In order to leave comments, you need to log in
Using a function within a function. Is it possible in bash?
Made a function to check the status. And separate functions for some script checks. I use functions in order to be able to specify an (intermediate) exit code through return and at the same time continue the script (and generally convenient). And the function for checking the status just looks at the intermediate exit code. And if the code is not what is expected, it increments the error counter. It would be great to do the check directly in the function, and not at the output, so that some additional lines would not spoil my "prepared return". Is it possible to do this somehow? Because I see that inserting a function into a function is not an option =( In general terms, it looks like this:
check=0
function check-exit-stat()
{ if [ $? -ne $1 ]
then check=`expr $check + 1`
fi
}
function f1()
{ k=1
if [ $k -eq 0 ]
then
return 0
else
return 1
fi
#было бы хорошо тут поставить check-exit-stat 0
}
т.к. если сделать так
f1
check-exit-stat 0
exit $check
Answer the question
In order to leave comments, you need to log in
but how do you want to execute something inside the function after return, because return is just intended to exit the function, everything below will simply not be executed.
#!/usr/bin/bash
check=0
check-exit-stat() {
if
then ((check++))
fi
}
f1() {
k=1
if
then ls # return 0
else blabla # return 127
fi
N=${?}
check-exit-stat 0 ${N}
return ${N}
}
f1
echo $?
# ...
f1
echo $?
# ...
echo check=$check
exit $check
#!/usr/bin/bash
check=0
check-exit-stat() {
if
then ((check++))
fi
return ${check}
}
f1() {
k=1
if
then ls # return 0
else blabla # return 127
fi
check-exit-stat 0 ${?}
}
f1
echo $?
# ...
f1
echo $?
# ...
echo check=$check
exit $check
#!/usr/bin/bash
check=0
check-exit-stat() {
if
then ((check++))
fi
return ${check}
}
f1() {
k=1
if
then check-exit-stat 0 0 # return 0
else check-exit-stat 0 1 # return 1
fi
}
f1
echo $?
# ...
f1
echo $?
# ...
echo check=$check
exit $check
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question