zsh-users
 help / color / mirror / code / Atom feed
* default values
@ 2016-03-30 18:12 Emanuel Berg
  2016-03-30 18:52 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Emanuel Berg @ 2016-03-30 18:12 UTC (permalink / raw)
  To: zsh-users

Is there a C-like "... ? ... : ... ;" syntax or
otherwise standard or conventional way to assign
default values to sometimes-not-submitted parameters?

I have found many ways to do it and I can't tell if
one is better than the other.

For example, like this:

    biggest-files () {
        local num_files=10
        local where=./

        if [ $1 ]; then num_files=$1; fi
        if [ $2 ]; then     where=$2; fi

        du -ahS $where | sort -hr | head -n $num_files
    }

How is this supposed to look?

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 20 Blogomatic articles -                   


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: default values
  2016-03-30 18:12 default values Emanuel Berg
@ 2016-03-30 18:52 ` Bart Schaefer
  2016-03-30 19:35   ` Emanuel Berg
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2016-03-30 18:52 UTC (permalink / raw)
  To: zsh-users

On Mar 30,  8:12pm, Emanuel Berg wrote:
}
} Is there a C-like "... ? ... : ... ;" syntax or
} otherwise standard or conventional way to assign
} default values to sometimes-not-submitted parameters?

In math context you can actually use ternary syntax:

    $(( x ? y : z ))

But I think you're actually looking for this:

${NAME-WORD}
${NAME:-WORD}
     If NAME is set, or in the second form is non-null, then substitute
     its value; otherwise substitute WORD.  In the second form NAME may
     be omitted, in which case WORD is always substituted.

So e.g.

    local num_files=${1:-10}
    local where=${2:-./}


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: default values
  2016-03-30 18:52 ` Bart Schaefer
@ 2016-03-30 19:35   ` Emanuel Berg
  0 siblings, 0 replies; 3+ messages in thread
From: Emanuel Berg @ 2016-03-30 19:35 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> ${NAME-WORD} ${NAME:-WORD} If NAME is set, or in the
> second form is non-null, then substitute its value;
> otherwise substitute WORD. In the second form NAME may
> be omitted, in which case WORD is always substituted.
>
> So e.g.
>
>     local num_files=${1:-10} local where=${2:-./}

Excellent, thanks!

-- 
underground experts united .... http://user.it.uu.se/~embe8573
Emacs Gnus Blogomatic ......... http://user.it.uu.se/~embe8573/blogomatic
                   - so far: 20 Blogomatic articles -                   


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-03-30 19:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-30 18:12 default values Emanuel Berg
2016-03-30 18:52 ` Bart Schaefer
2016-03-30 19:35   ` Emanuel Berg

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).