0

In my program I need to validate user input getting by "read" in shell script. It check user input has spaces and if validation fails we should give user a another chance to input again.

stringValidator() {  
  if [[ ${1} =~ " " ]]; then
    echo Should not contain spaces
    echo Enter Again!    
    read input
    echo $2=$input 
    return $2   
  else 
    return $1  
  fi
}


echo "Enter below details"
echo -e "client-id :"
read clientId
stringValidator "$clientId" ${!clientId@}

Here I tried to do was send the variable name also into stringValidator() and reassign value to that name (here it is clientId).

But the problem is I can't return that variable from stringValidator(). I use many read inputs though I showed only one here. Therefore I need a separate function for validation.

If you have better way to do this please mention.

2 Answers 2

1

The statement "return" is not used like that, it's to send back an error code (numerical).

That code should do what you want though :

#!/bin/bash

echo "Enter below details"

while true; do
    echo -n "client-id : "
    read clientID
    if [[ $clientID =~ " " ]]; then
        echo "ClientID Should not contain spaces"
        continue
    else
        break
    fi
done

echo "do something else"

EDIT

To answer your comment, this code in ksh would do what you ask :

#!/bin/ksh

function stringValidator
{

typeset locVarName
typeset locVar

locVarName=$1
nameref locVar=$1

while true; do
    if [[ $locVar =~ " " ]]; then
        echo "$locVarName should not contain spaces"
        echo -n "$locVarName : "
        read locVar
        continue
    else
        break
    fi
done
}

echo "Enter below details"
echo -n "Client-ID : "
read clientId
stringValidator "clientId"
echo "Final value is $clientId"

echo "do something else"

Running would result to something like that :

Enter below details
Client-ID : test t
clientId should not contain spaces
clientId : tesgg
Final value is tesgg
do something else

Hope it helps !

Sign up to request clarification or add additional context in comments.

8 Comments

But the problem is I use many read inputs to validate. I showed here only one as it is easy to understand. I need a separate function for validation.
Well, that would be easy with indirect referencing, but Bash doesn't support it (well 4.3 supposed to but can't test it). Ksh do support it though if you can use that shell on your system.
Can't use that. This script is use for multiple users. This need to work on ubuntu shell.
Just to be sure, with the shebang the script may run within ksh even if your users are on bash. But like I said, having a function to do what you want will be very difficult without the indirect referencing. The other way is to write the while loop for each entry that requires a validation.
Can you give me a example code using indirect referencing for this problem?
|
0

Separate Validation function from read input. Next, introduce it into a while loop to ask for enter input again up to a correct value. Use ECHO instead of RETURN to get the value from a function.

2 Comments

Can you include some code? Code speaks louder than words.
I use many read inputs though I showed only one here. Therefore I need a separate function for validation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.