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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing a variable

Status
Not open for further replies.

Daedelus

Technical User
Aug 14, 2002
70
US
I have a script in which I need to test a variable to see if it consists of a 7 digit number (possibly starting with 0) followed by a dash and a number of 1,2 or 3 digits. For example:

6912054-3
0156924-103

I have used
[[ $num = [0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]* ]]

and so far gotten away with it, since in my application it is very rare to see anything other than 1 or 2 more digits after the first in the "dash number". But I would like a solution which is exact, and preferably elegant![afro]

Also, can anyone tell me where I can find a reference on how to use [[ ]]? While I can find information on the ordinary "test" or [ ] command, nothing I have seen has ever said more than "you can also use double brackets". [mad] What little I know about them I gleaned from other people's scripts!
 
IS_VALID=`echo ${NUM} | grep "^[0-9]\{7\}-[0-9]\{1,3\}$"`
 
oops, use this:

IS_VALID=`echo ${NUM} | grep "^[0-9]\{7\}-[0-9]\{1,3\}$" | wc -l`

Should return 0 or 1 indicate no-match or match, respectively


 
yet another variation on the 'theme'

if [[ $(expr ${num} : '[0-9]\{7,7\}-[0-9]\{1\}') -ne 0 ]] ; then
echo "$num matched"
else
echo "$num didNOT match"
fi;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
It's unix. There's always at least two ways of doing it, and probably closer to five.
 
granted - should we wait for [closer to] 3 more then? P-) vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks for the suggestions.

I tried
[ `echo $num | grep &quot;^[0-9]\{7\}-[0-9]\{1,3\}$&quot;` ]
and it also works. Unless someone has a better suggestion, I will go with it or Vlad's.

What is the point of enclosing &quot;num&quot; in braces in the evaluation? I have seen many people do this, but I have never understood why.
 
It tells the shell explicitly that the name enclosed in braces is a variable name.

Bad example, but here's where it can be important:

echo $size_of_fileK
vs
echo ${size_of_file}K

Guess which one works. You could use a space for the first, but if you're trying to make a concatenation without a space, you'll have to use the braces.

For me, it simply became habit to make sure I'm explicit about what I want.
 

here's the snippet from 'man ksh'
I [personally] trying enclose all my parameters in '{}' as a a matter of coding convention to ease readibility and avoid naming ambiguities.


# Parameter Expansion
The format for parameter expansion is as follows:

${expression}

where expression consists of all characters until the match-
ing }. Any } escaped by a backslash or within a quoted
string, and characters in embedded arithmetic expansions,
command substitutions and variable expansions, are not exam-
ined in determining the matching }.

The simplest form for parameter expansion is:

${parameter}

The value, if any, of parameter will be substituted.

The parameter name or symbol can be enclosed in braces,
which are optional except for positional parameters with
more than one digit or when parameter is followed by a char-
acter that could be interpreted as part of the name. The
matching closing brace will be determined by counting brace
levels, skipping over enclosed quoted strings and command
substitutions.

If the parameter name or symbol is not enclosed in braces,
the expansion will use the longest valid name whether or not
the symbol represented by that name exists. When the shell
is scanning its input to determine the boundaries of a name,
it is not bound by its knowledge of what names are already
defined. For example, if F is a defined shell variable, the
command:

echo $Fred

does not echo the value of $F followed by red; it selects
the longest possible valid name, Fred, which in this case
might be unset.
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks! A real problem I have is that some twit sysadmin [evil] has DISABLED the man instruction here! I have not been able to discern the reason for this. I guess the feeling is that if we have easy access to information about the operating system, we might actually use it, and the the sysadmin would have more work to do!
 
If you have a web access (and you should since you can post on this forum) try looking for 'man ksh' in your favorite search site (yahoo, lycos, ...). There is many sites providing unix man pages in (hyperlinked) html format. It is not always usefull for command or flags specific to your platform but really good enough for general information about standard commands or utilities (and ksh is sort of standard).

The danger is that your sysadmin could disable web access to prevent this :)
 

the FASTEST and my be the elegantest way is: the good old 'case' statement:

case x$var in x) echo failed; exit 1
;; x[0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]*[0-9]) echo OK
;; *) echo failed; exit 1
;; esac

try it in a 1000000 loop agaist 'test'...
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top