few queries on syntax in scripting (289 Views)
Reply
Trusted Contributor
Vishu
Posts: 407
Registered: ‎03-27-2007
Message 1 of 4 (289 Views)
Accepted Solution

few queries on syntax in scripting

Hi,

 

I have a script, but i need your help in understanding a small portion of that script.

 

1) One is, what is this loop doing.. i mean n is assigned value 0, but what does (:) colon doing in while command, if we are specifying range, then it can be specified without colons (:) i think.

 

typeset -i n=0
 while (( n < ${1:-80} )); do
  (( n+=1 ))
  echo "-\c"
 done

 

4) Secondly, how we are calculating the value of "u" in below command.

 

typeset u=$(id)
 u=${u%%\)*}; u=${u#*\(}

 

Thanks

Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 2 of 4 (276 Views)

Re: few queries on syntax in scripting

Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 3 of 4 (270 Views)

Re: few queries on syntax in scripting (Parameter Substitution)

>1) what is this loop doing

 

Are you asking what the whole fragment is doing or just what the Parameter Substitution is doing?

    ${1:-80}

 

From the ksh man page:

 ${parameter:-word}  If parameter is set and is non-null, substitute its value; otherwise substitute  word.

If the colon (:) is omitted from the above expressions, the shell only checks to determine whether or not parameter is set.

 

So the important character is the "-", not the colon.  ;-)

 

>4) how we are calculating the value of "u"?

 

typeset u=$(id)
 u=${u%%\)*}; u=${u#*\(}

$ id
uid=###(abc) gid=###(def) groups=...

 

This removes everything from first ")" to end.  Then removes from the start to the "(".

 

Note this is all silly because:
$ id -un
###

So just do: typeset u=$(id -un)

 

>Or, again: man sh

 

Actually you'll be sorry.  ;-)

You need: man sh-posix

Trusted Contributor
Vishu
Posts: 407
Registered: ‎03-27-2007
Message 4 of 4 (262 Views)

Re: few queries on syntax in scripting (Parameter Substitution)

Thanks Dennis for this clarification...i did read man page, but was bit confused, so turned to you....anyway, thanks again for your help...Your posts always help me out...
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.