validate input

the user inputs names that have to be inside square brackets
[John,Dan]
I want to check if the user puts the brackets and if not ask him to re-enter the names

why not just add the brackets yourself? it's far easier...

anyhoo....

#!/bin/ksh

input='[alskhdf]'

case $input in

  \[*\]) echo good ;;
  *) echo bad ;;

esac
> invalue="[John,Dan,]"
> if [ `echo "${invalue}" | tr "[:alpha:]" "~" | tr -s "~"` != "[~,~]" ] ; then echo "Bad input format; should be [name1,name2]"; fi
Bad input format; should be [name1,name2]

> invalue="[John,Dan]"
> if [ `echo "${invalue}" | tr "[:alpha:]" "~" | tr -s "~"` != "[~,~]" ] ; then echo "Bad input format; should be [name1,name2]"; fi
> 

Hope this also should work :slight_smile:

#!/bin/ksh

eval input=$@

echo $input |awk '/^\[/ {print}'
if [ $? -eq 0 ]
then
echo $input |awk '/\]$/ {print}'
if [ $? -eq 0 ]
then
echo "good"
else
echo "Please enter the input ending with ]"
fi
else
echo "Please enter the input starting with ["
fi

The solution are working guys but i want it to be like a loop.The user cannot exit unless he enters the correct input
Thanks anw

That's another story and you should search the forum and if you don't find the answer start a new thread.

Try this:

eval input=$@
VALID="false"
while [ "$VALID" != "true" ]
do
    echo $input |awk '/^\[/ {print}'
    if [ $? -eq 0 ]
    then
        echo $input |awk '/\]$/ {print}'
        if [ $? -eq 0 ]
        then
            echo "good"
            VALID="true"
        else
            echo "Please enter the input ending with ]"
            VALID="false"
        fi
    else
        echo "Please enter the input starting with ["
        VALID="false"
    fi
done

I have this code.The case works but i get the following error
for while: [:missing `]'

VALID="false"
while ["$VALID" != "true"]
do
case $input in
\[*\]) echo Input is ok
VALID="true";;
*) echo Invalid input
echo Enter input again in [,]
read input
VALID="false";;
esac
done

Insert spaces between the brackets ( [ and ] ) and the variables:

while [ "$VALID" != "true" ]

it works fine! thank u very much!!:stuck_out_tongue: