length of a variable!

Reply

Join Date: Nov 2008
Posts: 4
Reputation: cristi2008 is an unknown quantity at this point 
Solved Threads: 0
cristi2008 cristi2008 is offline Offline
Newbie Poster

length of a variable!

 
0
  #1
Nov 30th, 2008
how can I limit the length of a variable??thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: length of a variable!

 
0
  #2
Dec 1st, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC