1753524 Members
5537 Online
108795 Solutions
New Discussion юеВ

Re: char

 
SOLVED
Go to solution
Oliver_39
Occasional Contributor

char

I need to an input in a script not to accept more or less than eight characters.
I want it to accept only eight alphanumeric characters.

10 points for best answer!
8 REPLIES 8
john korterman
Honored Contributor
Solution

Re: char

Hi,

there are probably many ways of achieving that; you could try something like this:


#!/usr/bin/sh

REST=""
RES=""

message()
{
echo "input 8 alnum chars: \c"
}

message

while [ "$RES" != "OK" ]
do
read INPUT
REST=$(echo "$INPUT" |tr -d "[[:alnum:]]")
if [ "$REST" != "" ]
then
message
else
# continue with count check...
if [ "${#INPUT}" != 8 ]
then
message
else
RES=OK
fi
fi
done
echo "$INPUT" ok, continue with script

regards,
John K.
it would be nice if you always got a second chance
Leif Halvarsson_2
Honored Contributor

Re: char

Hi,
echo the string into awk as follow:

echo $AAA | awk '{ A=length($0) } END { if (A == 8) { exit(1) }}'

awk returns 1 if 8 char else return 0



A. Clay Stephenson
Acclaimed Contributor

Re: char

I would leverage grep -E -q to return a zero if all is well otherwise non-zero and match on an anchored string:

#!/usr/bin/sh

typeset -i STAT=0
typeset X=""
while [[ ${STAT} -ne 0 ]]
do
echo "Enter value: \c"
read X
echo "${X}" | grep -E -q -e "^[[:alnum:]]{8}$"
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
echo "\aInvalid; re-enter.\n"
fi
done
echo "X = \"${X}\""

This must return exactly 8 alphanumeric characters.
If it ain't broke, I can fix that.
Oliver_39
Occasional Contributor

Re: char

hi,

both answers accept alpha?
I want it to accept only aphanumeric characters?


Hein van den Heuvel
Honored Contributor

Re: char


Please explain what you mean with alpha.
Best I know it is a sub-set of alpha-numeric.
So 8 alpha = 8 alpha-numeric.
examples:
abcdefgh
12345678
A1B2C3D3
counter examples:
123 5678
ab%def?h

If need be, give examples of acceptable and not acceptable input.

Here is an other starting point for a solution:

#!/usr/bin/csh
#
set test
while (`echo $test | tr '[:alnum:]' '[x*0]'` != 'xxxxxxxx')
set test=$<
echo "--$test--"
end

any and all alphanumerics in $test are replaced by an 'x' (x*0 = unlimited x's :-)
If the result is 8 x's, the input must have been valid.

fwiw,
hein.
Muthukumar_5
Honored Contributor

Re: char

Try this script.

#!/bin/ksh

echo "Enter input word";
read word;

LEN=$(echo $word | awk '{ print length($1) }')

if [[ ${LEN} -ge 8 ]]
then
ALPLEN=$(echo $word | tr -d '[[:alnum:]]'| awk '{ print length($1) }')
if [[ ${LEN} -eq 8 ]]
then

echo "Entered word $WORD is having 8 alphanumeric characters"

else
echo "Entered word $WORD is not having 8 alphanumeric characters"

fi
else
echo "Entered word $WORD is having less than 8 characters"
fi

hth.
Easy to suggest when don't know about the problem!
Amit Agarwal_1
Trusted Contributor

Re: char

Well you can use 'typeset -L' to specify the length of variable contents that it can take.

$ cat script
typeset -L8 myVar
echo "Enter value for myVar: \c"
read myVar
echo "myVar is " $myVar

$ ./script
Enter value for myVar: jlfjldjf98df9dskljfsdu
myVar is jlfjldjf

You can add the check for alphanumeric content over top of it.
Oliver_39
Occasional Contributor

Re: char

.