Reputation: 2538
I found a script which has the following snippet:-
userid=`expr "\`id\`" : ".*uid=[0-9]*(\(.[0-9a-z]*\)) .*"`
It returns the userid.
When i tried to learn how it is doing:-
#id
#uid=11008(adilm) gid=1200(cvs),1400(build)
So I realized that (.[0-9a-z]*) is matching the userid. But if I placed like below:
#userid=`expr "uid=11008(ADILM) gid=1200(cvs),1400(build)" : ".*uid=[0-9]*(\(.[0-9a-z]*\)) .*"`
#echo $userid
ADILM
It works. As per my understanding '.' is matching to ADILM. But when i removed '.' like below:-
#userid=`expr "uid=11008(ADILM) gid=1200(cvs),1400(build)" : ".*uid=[0-9]*(\([0-9a-z]*\)) .*"`
#echo $userid
ADILM
It still works how? We have only provided lowercase letters but its still working.
Upvotes: 1
Views: 280
Reputation: 342373
this will not explain the regex, but alternative ways to get your id.
$ id -u -n
$ id|sed 's/uid=[0-9]*(//;s/).*//'
Upvotes: 0
Reputation: 9440
(\(.[0-9a-z]*\))
matches a group containing the brackets plus the user id.[a-z0-9]
it still matches.(\([0-9a-z]+\))
then.Upvotes: 3