From: Mikael Magnusson <mikachu@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: random once but not twice
Date: Sat, 11 Nov 2017 22:52:48 +0100 [thread overview]
Message-ID: <CAHYJk3SE_JV0BHY55vjgqSeDeM5vTOB=d1UH_Ok-yn0WBZv4Zg@mail.gmail.com> (raw)
In-Reply-To: <86k1yw63u0.fsf@zoho.com>
On Sat, Nov 11, 2017 at 7:57 PM, Emanuel Berg <moasen@zoho.com> wrote:
> Just wrote this. Strange thing is, random-video
> gives me randomized files, but not
> play-random-video, which is just an interface -
> or that the intention, at least :)
>
> Can anyone figure out what is going on?
>
> All other suggestions how to improve the code,
> both style and function, is appreciated, as
> always...
>
> random-video () {
> local vid_dir=${1:-.}
> local ext=${2:-mkv}
>
> local vid
> local -a vids
> local num_vids
> local vid_num
>
> vids=("${(@f)$(ls ${vid_dir}/*.${ext})}")
> num_vids=${#vids}
>
> vid_num=$(( RANDOM % $num_vids + 1 ))
> vid=$vids[$vid_num]
>
> echo $vid
> }
> alias rv=random-video
>
> play-random-video () {
> local vid_dir=${1:-/mnt-disk/mm/survivor/au/04}
> local vid
> vid=$(random-video $vid_dir)
> echo "playing $vid"
> # pl $vid # local function to omx player
> }
> alias prv=play-random-video
The subshell in the second function ensures that the RANDOM state in
the parent shell stays the same.
--
Mikael Magnusson
next prev parent reply other threads:[~2017-11-11 21:52 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-11 18:57 Emanuel Berg
2017-11-11 21:52 ` Mikael Magnusson [this message]
2017-11-11 22:26 ` Emanuel Berg
2017-11-11 22:32 ` Emanuel Berg
2017-11-11 22:46 ` Emanuel Berg
2017-11-12 19:08 ` Peter Stephenson
2017-11-12 20:02 ` Emanuel Berg
2017-11-12 20:15 ` Bart Schaefer
2017-11-12 20:18 ` Emanuel Berg
2017-11-13 0:15 ` Emanuel Berg
2017-11-13 4:56 ` Bart Schaefer
2017-11-13 5:07 ` Emanuel Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAHYJk3SE_JV0BHY55vjgqSeDeM5vTOB=d1UH_Ok-yn0WBZv4Zg@mail.gmail.com' \
--to=mikachu@gmail.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).