Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

What about use so solution?

Functions run in loop (cycle?). In that loop - I have another function wuch also uses loop. When second function get NO answer from user - it send break 2 to stop loop and proceed main script actions.

Function uses variables wich set in file.

So, is it good idea use variables as parameters for functions?

share|improve this question

closed as unclear what you're asking by slm, terdon, Anthon, jasonwryan, Bernhard Oct 29 '13 at 18:21

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What is your actual question? –  terdon Oct 29 '13 at 14:42
    
Is it bad idea - use variables as parametrs for functions? –  setevoy Oct 29 '13 at 15:00
    
Not at all, in fact that is the most common use of functions. You almost always pass variables to a function and not hardcoded values, otherwise functions would not be very useful at all. –  terdon Oct 29 '13 at 15:03

1 Answer 1

up vote 2 down vote accepted

One alternative that might be cleaner is to have answer return 0 or return 1, depending on whether the user said yes or no. Then test the value of answer in the place where you call it, and only do the action if answer returned 0.

Based on your earlier script, it would look something like this:

while tomcat_running && user_wants_to_stop_tomcat; do
    echo "$tomcat_status_stopping"
    kill $RUN
    sleep 2
done

function tomcat_running() {
    check_tomcat_status
    [ -n "$RUN" ]
}

function user_wants_to_stop_tomcat() {
    answer "WARNING: Tomcat still running. Kill it? "
}

function answer() {
    while true; do
        printf "$1"
        read response
        case $response in
        [yY][eE][sS]|[yY])
            return 0
            ;;
        [nN][oO]|[nN])
            return 1
            ;;
        *)
            printf "Please, enter Y(yes) or N(no)!\n"
            ;;
        esac
    done
}
share|improve this answer
    
answer function used in many places... but - good idea, thanks! –  setevoy Oct 29 '13 at 15:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.