Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Statement question 2

Status
Not open for further replies.

UCF87

Technical User
Feb 13, 2003
44
0
0
US
Is it possible in an if statement, to determine if a variable contains a two or three digit number? Then I will need to add 2 or 1 preceeding zero(s) to that number respectively.
 
#!/bin/ksh

a='12'

if [[ "${a}" = @([0-9][0-9]|[0-9][0-9][0-9]) ]] ; then
a=$(printf "%04d" "${a}")
echo "[$a] YES"
else
echo "[$a] NO"
fi;


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
If in ksh you can play with the typeset -Z4 and typeset -i commands, something like this(for testing variable a):
Code:
typeset -Z4 result=0
[ ${#a} -ge 2 -a ${#a} -le 4 ] &&
  typeset -i b=&quot;$a&quot; 2>/dev/null &&
    result=$b || echo BAD

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top