Operating System - HP-UX
1833770 Members
2119 Online
110063 Solutions
New Discussion

Why `expr $VAR : '.*'` return the length of VAR?

 
SOLVED
Go to solution
Eric Antunes
Honored Contributor

Why `expr $VAR : '.*'` return the length of VAR?

Hi gurus,

I did successfuly a change in .profile:

...
if [ `tty` = "/dev/console" ]
then
export TERM=hp
else

tipo_terminal=`tty|cut -f 4 -d "/"`

if [ `expr $tipo_terminal : '.*'` -eq 1 ]
then
export DISPLAY="IP_ADDRESS:0.0"
xhost + IP_ADDRESS
else
export TERM=vt220
fi
fi
...


But, I didn't understand why `expr $tipo_terminal : '.*'` returns the tipo_terminal variable length and would like to understand (I saw this as an example in expr manual)

Thanks,

Eric Antunes
Each and every day is a good day to learn.
5 REPLIES 5
Peter Godron
Honored Contributor
Solution

Re: Why `expr $VAR : '.*'` return the length of VAR?

Eric,
the : is the key to it.
the expr compares $tipo_terminal against .*
and return the matched characters (or 0 if none matched)
See man expr

Did I mis-understand the question.
RAC_1
Honored Contributor

Re: Why `expr $VAR : '.*'` return the length of VAR?

man sh-posix
Read section Parameter Substitution
There is no substitute to HARDWORK
Eric Antunes
Honored Contributor

Re: Why `expr $VAR : '.*'` return the length of VAR?

Of course, what a stupid question...

EVERYTHING is equal to '.*'...

Thanks Peter,

Eric Antunes

Each and every day is a good day to learn.
Arunvijai_4
Honored Contributor

Re: Why `expr $VAR : '.*'` return the length of VAR?

Hi Eric,

expr : expr

The matching operator : compares the first argument with the second argument which must be a regular expression. expr supports the Basic Regular Expression syntax (see regexp(5)), except that all patterns are ``anchored'' (i.e., begin with ^) and, therefore, ^ is not a special character, in that context. Normally, the matching operator returns the number of characters matched (0 on failure). Alternatively, the \(...\) pattern symbols can be used to return a portion of the first argument.

http://docs.hp.com/en/B2355-60127/expr.1.html

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Eric Antunes
Honored Contributor

Re: Why `expr $VAR : '.*'` return the length of VAR?

Thanks to all!
Each and every day is a good day to learn.