DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   length of a variable! (http://www.daniweb.com/forums/thread160121.html)

cristi2008 Nov 30th, 2008 2:18 pm
length of a variable!
 
how can I limit the length of a variable??thanks

eggi Dec 1st, 2008 3:29 am
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


All times are GMT -4. The time now is 12:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC