how can i campare te length of the string
$var = unix
if ( $var -eq 4 )
then..
is it right
how can i campare te length of the string
$var = unix
if ( $var -eq 4 )
then..
is it right
var="UNIX"
if [ ${#var} -eq 4 ]; then
# do something
fi
For basic usage like this you really should look at the appropriate man pages or tutorials.
if [ `printf $var|wc -c` -eq 4 ];then echo "4"; fi
ghostdog74: If $var contains whitespace or special characters, it needs to be double-quoted. The solution posted by Franklin52 seems more succinct and workable anyway; the built-in ${#var} length operator doesn't require any external processes, and doesn't have issues with quoting etc.
true, then put quotes. anyway, its good practice too
if [ `printf "$var"|wc -c` -eq 4 ];then echo "4"; fi
this method should work on most shells, including bourne, csh
Actually I believe the syntax for csh is different.
my bad. what i meant is the way to get length, not the if/else.