943,754 Members | Top Members by Rank

Ad:
Nov 30th, 2008
0

length of a variable!

Expand Post »
how can I limit the length of a variable??thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cristi2008 is offline Offline
4 posts
since Nov 2008
Dec 1st, 2008
0

Re: length of a variable!

Hey There,

In bash (and similar shells) you can use read with the -n flag, like:

read -n4

if you want to limit the variable input to 4 characters.

If you want to just trim your variables, you can check the length of a variable by gettings the length value and running a simple if compare. so, if:

var=12345
echo $var = 12345
echo ${#var} = 5

if [ ${#var} -gt 4 ]
then
bail
fi

Hope that helps

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Input file!
Next Thread in Shell Scripting Forum Timeline: DOS 6.22





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC