0

I want to use only one validation function many times. I tried to make a validation function like beneath. How can I fix my code and use the validation function like this one many times?

#!/bin/bash
digit='^[[:digit:]]+$';
function numberValidate {
    if [[ $1 =~ $digit ]];
       then
          echo "$1 is number. Correct";
       else
          echo “Enter only numbers”;
          numberValidate
    fi
}
printf “Enter the number 1 ”; read n1;
numberValidate $n1
printf “Enter the  number 2”; read n2;
numberValidate $n2

Thanks.

Walter A
  • 19,067
  • 2
  • 23
  • 43
Roger
  • 110
  • 2
  • 12
  • The recursive call to `numberValidate` when there's a problem is probably wrong. The function doesn't get passed any value but it expects one, and it doesn't prompt for a value. You need to think a bit harder about how to get this to work. You'll need to be able to prompt (again) for a number from within the function, and you'll need to be able to get the value out of the function somehow... – Jonathan Leffler Dec 20 '15 at 02:58
  • The use of smart quotes here is one thing that's obviously wrong. Was your code copied and pasted from a Microsoft platform? – Charles Duffy Dec 20 '15 at 15:58
  • That said, there's no actual **question** in this question -- it's nothing but a dump of code that shows that you already *do* know how to call a function more than once (which is what the title would seem to indicate you to be asking). If you're asking why the code fails in a given way, describe the failure mode and what you intend it to do; see http://stackoverflow.com/help/how-to-ask. – Charles Duffy Dec 20 '15 at 16:00

2 Answers2

1

Don't use recursion when a loop will do:

inputNumber () {
    local digit='^[[:digit:]]+$'
    read $n
    until [[ ${!n} =~ $digit ]]; do
        echo "Enter only numbers"
        read $n
    done
}
printf "Enter the number 1 "; inputNumber n1
printf "Enter the number 2 "; inputNumber n2

The preceding moves all the input into the validation function, and takes advantage of the fact that variables set in a function are global by default. The argument to inputNumber is the name of the variable to set.

chepner
  • 497,756
  • 71
  • 530
  • 681
1
inputNumber () {
    local digit='^[[:digit:]]+$'
    outputvar=$1
    shift
    if [[ $# = 0 ]]; then
       read -p "Please enter a number for ${outputvar} "
    else
       read -p "$* " n
    fi
    until [[ "${n}" =~ $digit ]]; do
        read -p "Enter only numbers " n
    done
    printf -v "${outputvar}" '%s' "$n"
}

inputNumber n1 "Enter the number 1"
inputNumber n2 "Enter the number 2"
inputNumber n3
echo "n1=${n1} n2=${n2} n3=${n3}"

Question in a comment: How to reuse these? When you like functions like this one, you can collect them in files and put them in a dedicated directory. Perhaps choose ${HOME}/shlib as a folder and create files like iofunctions, datefunctions, dbfunctions and webfunctions (or io.sh, date.sh, db.sh and web.sh). Next you can source the files with a dot:

$ head -2 ${HOME}/shlib/iofunctions
# This file should be included with ". path/iofunctions"
function inputnumber {

$ cat ${HOME}/bin/roger.sh
#!/bin/bash
# Some settings
shlibpath=/home/roger/shlib
# Include utilities
. "${shlibpath}"/iofunctions
# Let's rock
inputNumber rockcount How many rocks
# call an imaginary function that only accepts y and n
inputyn letsdoit Are you sure

You can put the functions (or source the function files) in your .profile/.bashrc, but that will give problems when you want to start the script in crontab (and you will get used to the functions and forget that inputNumber is not a standard bash function).
Another question is how you can refer to another directory that is on the same level as your script file (not a fixed path). Including ../shlib/iofunctions will fail when you start your script using a path (like bin/roger.sh). A good question with many answers, I can't tell which is the best:
How to set current working directory to the directory of the script?
Getting the source directory of a Bash script from within
Reliable way for a bash script to get the full path to itself?
How to set current working directory to the directory of the script?
how to get script directory in POSIX sh?

Community
  • 1
  • 1
Walter A
  • 19,067
  • 2
  • 23
  • 43
  • To improve this code. this solution works in one file(bash). If y one to work in another file, I have to copy this function. "this function for each file(bash)". – Roger Dec 20 '15 at 14:11
  • THE QUESTION IS How I have in one file many functions and from another file only call this file to work. – Roger Dec 20 '15 at 14:18