Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

few queries on syntax in scripting

SOLVED
Go to solution
Vishu
Trusted Contributor

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

3 REPLIES
Steven Schweda
Honored Contributor

Re: few queries on syntax in scripting

 
Dennis Handly
Acclaimed Contributor
Solution

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

Vishu
Trusted Contributor

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...