How to set character limit on READ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to set character limit on READ?
# 1  
Old 12-10-2013
How to set character limit on READ?

Hello,
I created the following (snippet from larger code):

Code:
 echo -n "A1: "
 read A1

 VERIFY=$(echo -n $A1|wc -c)
 if [ $VERIFY -gt 8 ]; then
  echo -e "TOO MANY CHARACTERS"
 fi

 echo -n "A2: "
 read A2
 echo -n "A3: "
 read A3
 echo -e "Concat: $B1/$B2/$B3"

Basically what it does is it asks the user for values for A1, A2, and A3. The only catch is that character length cannot exceed 8. I think I have the validations, but even if they exceed 8, the value is stored. How can I make it so that conditions for A1 must be met before you proceed. The same character limit applies to A2 and A3.

Last edited by jl487; 12-10-2013 at 06:21 PM..
# 2  
Old 12-10-2013
Use an infinite while loop. Break from the loop when the condition is met.

Here is a sample code:
Code:
#!/bin/ksh

while :
do
        print "A1: "
        read A1
        [ ${#A1} -le 8 ] && break
done

Use shell builtin to get variable length instead of wc
# 3  
Old 12-10-2013
Quote:
Originally Posted by Yoda
Use an infinite while loop. Break from the loop when the condition is met.

Here is a sample code:
Code:
#!/bin/ksh

while :
do
        print "A1: "
        read A1
        [ ${#A1} -le 8 ] && break
done

Use shell builtin to get variable length instead of wc
Thanks for the suggestion. Unfortunately, I have this in a bash script, so I'm trying to use bash.
# 4  
Old 12-10-2013
That was just an example. Use bash instead, replace print with printf:
Code:
#!/bin/bash

while :
do
        printf "%s" "A1: "
        read A1
        [ ${#A1} -le 8 ] && break
done

This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-11-2013
Could truncating it be a valid choice? If so, using typeset might be usful:-
Code:
typeset -L8 A1
read A1?"Please enter a string: "

You might end up with trailing blanks of the input is less that 8 characters though. Is that acceptable?
Code:
$ typeset -L8 A1 
$ read A1?"Please enter a string: "
Please enter a string: greatbiglongstring             
$ echo "!$A1!"                     
!greatbig!
$ read A1?"Please enter a string: "
Please enter a string: Hello
$ echo "!$A1!"                     
!Hello   !



I hope that this helps,
Robin
Liverpool/Blackburn
UK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Set hard block limit for user using quota

hi all, i have installed quota on my centos 7 machine and its what im after (setting size limit on users, so they cant fill the hard drive) i want to now make this part of my create user script for my sftp server so i want to do a echo and a read command so i capture the limit they enter... (0 Replies)
Discussion started by: robertkwild
0 Replies

2. Shell Programming and Scripting

Read character by character in line in which space is also included

Hi friend, I have one file , and i want to read that file character by character. I need this script in ksh. while using read option with -n1 am getting error. while read -n1 c read has bad option And if i am using below script, then if in a line has space like this ( Pallvi mahajan)... (10 Replies)
Discussion started by: pallvi_mahajan
10 Replies

3. Shell Programming and Scripting

How to set end limit while copying files of a range??

I have files being generated in format A20140326.00........ to A20140326.24............. I need to copy these hourly basis from one location to another. Eg. If i copy from 14 to 19 the hour, I use wildcard as A201403226.1*. Requirement is : I need to copy from 06 hour and wil run the script... (1 Reply)
Discussion started by: Saidul
1 Replies

4. UNIX for Dummies Questions & Answers

character limit via awk

I'm using a command that outputs the total size of the files that I've specified. I'd like to introduce a character limit that appends an ellipsis to the lines that go beyond the specified amount. du -chs {query} | sed 's!.*/!!' | awk '{print substr($0,0,25)""}' That's what I have so far.... (4 Replies)
Discussion started by: Light_
4 Replies

5. Shell Programming and Scripting

csh shell script 'set' argument limit?

Hi , I have a script that is causing a problem that led me to think if there is a limit to the number of arguments for 'set' command in csh shell script. Here is my script: #!/bin/csh -f set top = design_top #1 set v_mbist = ( sim_mbist/*.v ) #2 set v_simlist = ( -v... (2 Replies)
Discussion started by: return_user
2 Replies

6. IP Networking

Telnet Character Limit

Hi All, We are using the SunOS 5.9 and the application is listening on user defined ports and waiting for the request from the user.When i am trying to connect and give the requests using telnet ,I am not able to provide the request beyond 256 characters.The telnet connection is... (1 Reply)
Discussion started by: sureshbabuc
1 Replies

7. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. Shell Programming and Scripting

I need to set a time limit for a script

Hello Folks, I have been asked to write a test script which can be run by students. the script should have a time limit. I have almost completed it except the bit of timing! I've seen something like this: on_timeout() { echo "$USER $score " >> theresult.txt echo "Time out!... (2 Replies)
Discussion started by: SultanKSA
2 Replies

10. Solaris

How to set Root password age limit in Solaris 9/10

Hi Friends, Can anyone tell me how can I set the password age limit for root user to 14 days....??? Also would like to add following for root password; min-alpha --- 4 min-other --- 1 min-length -- 6 min-diff ----- 3 How can I do these on command line....??? Regards, jumadhiya (7 Replies)
Discussion started by: jumadhiya
7 Replies
Login or Register to Ask a Question