1833760 Members
2121 Online
110063 Solutions
New Discussion

ksh, script help

 
SOLVED
Go to solution
Fred Martin_1
Valued Contributor

ksh, script help

I'm trying to decifer what this does, exactly:

PF=/rd/opsys/${1:-editor}.pf

I have a file called /rd/opsys/editor.pf, and I know in the script I'm looking at, it should resolve to PF=/rd/opsys/editor.pf.

So what is 1:-

It would seem 1 refers to the first parameter that is passed to the script, for if I pass one, it shows up in the value of PF.

I'm guessing this means: if parameter 1 is set, take it's value, else use "editor"

Am I off the mark?

Fred
fmartin@applicatorssales.com
5 REPLIES 5
spex
Honored Contributor
Solution

Re: ksh, script help

Fred,

You are correct.

PCS
Fred Martin_1
Valued Contributor

Re: ksh, script help

Thanks. Not as complicated as I thought it might be :)
fmartin@applicatorssales.com
Bill Hassell
Honored Contributor

Re: ksh, script help

To generalize about the ${something:-somethingElse}, this is a way to test if a value has been set and if not, assign a value. For instance, if a global variable called COLUMNS is not set, you want to set it to 80:

COLUMNS=${COLUMNS:-80}

You can even use this construct in a calculation:

let ROWS=${ROWS:-24}-2

So ROWS is now set to 2 less than actual or default value. In your example, 1 is shorthand for the first parameter passed to the current shell.


Bill Hassell, sysadmin
spex
Honored Contributor

Re: ksh, script help

Hello (again) Fred,

It's worth mentioning that this form of parameter substitution:

${parameter:-word}

will return 'word' if 'parameter' is unset and/or null, but WILL NOT set 'parameter' to the value of 'word'. While this form:

${parameter:=word}

WILL set 'parameter' to the value of 'word' if 'parameter' is unset and/or null.

In your particular example, you must use the first form, as the shell does not allow positional parameters (in your case: '$1') to be set after the fact.

PCS
Fred Martin_1
Valued Contributor

Re: ksh, script help

Thanks folks, this is great information.
fmartin@applicatorssales.com