restrict the user not to key in more than 50 chars

When my script is run, it will ask the user to enter some text and the user should be restricted not to enter more than 50 chars. Is there any command which I can use with "read" command? Help me please

Something like this - just to show a way how to do it with more than 5 characters:

root@isau02:/var/log> echo "Input:"; read INPUT; echo $INPUT| awk '{if ( length($0) > 5 ) {print "too many"}}'
Input:
awidawdi
too many

You can also use "wc -c" and check how many characters are contained so you don't have to use awk. There are many ways.

thanks for the reply. Actually am looking like something...
eg: Input:testing

and the cursor should stop after a particular length and not allow user to enter any more chars in shell scripting not awk

If using bash...

read -n 50

am working on korn shell, could you help me on this, this is the code

echo "Enter the text"
read text

which should not allow the user to enter more than 50 chars

Hi,

am working on bash shell, could you help me on this, this is the code

echo "Enter the text"
read text

which should not allow the user to enter more than 50 chars

help me, thanks in advance

reborg already showed you how to do that....

echo -n "Enter text:"
read -n 40 text

Thank you but I couldnt succeed. First line of my script is #!/bin/sh

The error am getting is "A specified flag is not valid for this command"

Need some help, thanks, some please help me

some please help me on this

It's not allowed to bump up questions or duplicate postings.
Read the rules.

echo -n "enter the text:"
read
if [[ ${#REPLY} -gt 50 ]]
then
echo "Error"
fi

to gnsxhj,

this script will check the length of the text once its entered but i think injeti want to restrict user to enter max of 50 characters.

yes am looking for something where the user will not be able to enter more than 50 chars at the prompt

like...

echo "Enter string"
name string

here I need some code where it will beep or stop when the user tries to key-in 51st char

thanks

dear injeti i am afraid that there is no such option in unix..
because in unix the validation is done once user hits enter while entering text simultaniously validation can't be done..
but i suggest you can go for some java script

someone gave me one option like

echo -n "ENTER"
read -n 50 name

but it didnt work

even i was thinking that there is nothing like that in unix

may be new version of unix may have that but AIX and current unix don't have any such option..

Hi.

I dislike using the sledgehammer of dd, but this does seem to work:

#!/bin/sh -

# @(#) s3       Demonstrate read one character in shell.
# See man stty and
# http://cs.senecac.on.ca/~lczegel/UNX510/lectures/Lecture6.html
# for some details.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) stty dd
set -o nounset

echo
string=""
oldsettings=$(stty -g)
stty -icanon min 1 time 0 -icrnl -echo

i=0
printf " Enter your string:\n"
while :
do
  ch=$(dd bs=1 count=1 2> /dev/null)
  printf "$ch"
  i=`expr $i + 1`
  if [ $i -gt 3 ]
  then
    printf " \nToo many characters, kept only 3: \"$string\".\n"
        break
  fi
  string="${string}$ch"
done

stty $oldsettings
echo " Final value of string is \"$string\""

exit 0

producing:

$ ./s3

(Versions displayed with local utility "version")
AIX 1
sh - no version provided for /usr/bin/sh.
stty - no version provided for /usr/bin/stty.
dd - no version provided for /usr/bin/dd.

 Enter your string:
abcd
Too many characters, kept only 3: "abc".
 Final value of string is "abc"

Best wishes ... cheers, drl

great job!!!!!!!!

Hi.

This is a slightly improved version. The older version did not allow less than the required number in a friendly way, and required n+1 characters to be entered. This version stops at n characters, and recognizes a CR as a termination.

#!/bin/sh -

# @(#) s4       Demonstrate read one character in shell.
# See man stty and
# http://cs.senecac.on.ca/~lczegel/UNX510/lectures/Lecture6.html
# for some details.

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) stty dd
set -o nounset

echo
string=""
oldsettings=$(stty -g)
stty -icanon min 1 time 0 -icrnl -echo

i=0
last=false
printf " Enter your string:\n"
while :
do
  ch=$(dd bs=1 count=1 2> /dev/null)
  printf "$ch"
  # To get a real "^M" with vi/vim, use control-v control-Enter.
 ]]f [[ "$ch" =
  then
    printf "\n"
    last=true
  else
    string="${string}$ch"
    i=$( expr $i + 1 )
    if [[ $i -ge 3 ]]
    then
          printf "\n"
      last=true
    fi
  fi
  if [ $last = true ]
  then
    break
  fi
done

stty $oldsettings
echo " Final value of string is \"$string\""

exit 0

Producing:

aix drl 405 $ ./s4

(Versions displayed with local utility "version")
aix 5.1
sh - no version provided for /usr/bin/sh.
stty - no version provided for /usr/bin/stty.
dd - no version provided for /usr/bin/dd.

 Enter your string:
abc
 Final value of string is "abc"
aix drl 405 $
aix drl 405 $ ./s4

(Versions displayed with local utility "version")
aix 5.1
sh - no version provided for /usr/bin/sh.
stty - no version provided for /usr/bin/stty.
dd - no version provided for /usr/bin/dd.

 Enter your string:
xy
 Final value of string is "xy"

cheers, drl