Pass parameters to a bash function

  Kiến thức lập trình
#! /bin/bash

testFunction () {
#clear 
count=0
    echo $rb; echo $1; echo $count
    while [[ count < 3 ]]
    do
        case $rb in
            "YES") echo "bongo";;
            [Nn][Oo]|[Nn]) echo "Nothing done"; exit 1;;
            *) echo "That isn't a valid answer"
    ((count++))
        if [[ $count == 3 ]]
            then echo "Done with you, bye"; exit 1
        fi;;
        esac
    done
}

read -p "Test fundtion YES or (Nn)o ? " rb
    testFunction $rb
    echo "It worked"

So what I’m trying to do is pass the response from the user input to a function called testFunction.

The echo $rb, echo $1; and echo $count are for debugging purposes, and it seems the function is receiving the variable.
However, the case statement doesn’t execute as expected.

I was trying to get the script to exit inside the testFunction, or carry on if “YES” is typed.

echo “bongo” is for debugging too.
I’ve tried $rb and $1 inside the testFunction and both behave the same.

I’ve used similar construct with success, but I’m trying to create a function to reduce redundant code…………

3

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT