Also, one can use wdread to get the number of workspaces that currently exist:

wdread WMState Workspaces | grep -c '^    Name'

I'm not terribly happy about the four-spaces-then-Name part, but I
believe the format to be relatively consistent.

$(( 1 + 1)) is supported in dash, so don't need to worry about using
bashisms to get simple arithmetic. If you are happy to use bashisms,
instead of using a counter one can use for i in {1..10}. zsh lets you
use {1..$n}. for i in $(seq 1 $(wdread WMState Workspaces | grep -c '^
   Name')) also works, and avoids all the bashisms.

I did not know about sort -R. However, we still have the problem with
embedded newlines, unless you use -z:

find -L /path/to/backgrounds -type f -print0 | sort -z -R

Now you have a new problem of head not accepting null terminated
lines. Additionally, while -z is in BSD's sort, -R is a GNUism.

Use bash:

typeset -a backgrounds
backgrounds=(/path/to/backgrounds/*); # this sets the array
backgrounds to each file in /path/to/backgrounds
number_of_pictures="${#backgrounds[@]}"
random_picture_index=$(( RANDOM % number_of_pictures )); # Random
number, 0 to total number of files minus one
random_picture="${backgrounds[$random_picture_index]}"; # quotes, to
protect whitespace including newlines
wmsetbg $WHATEVER_OPTIONS_YOU_WANT "$random_picture"; # again, quotes
to protect whitespace

zsh makes this even easier, since arrays are first class citizens:

backgrounds=(/path/to/backgrounds/*); # zsh knows () creates arrays
number_of_pictures=$#backgrounds; # no need for weird array
specification. zsh knows arrays.
random_picture_index=$(( RANDOM % number_of_picutres + 1 )); # zsh
arrays are 1 indexed; bash are 0 indexed.
random_picture=$backgrounds[$random_picture_index]; # zsh knows
scalars too. no need to quote.
wmsetbg $OPTS $random_picture; # $OPTS is an array, right? ${=OPTS}
does $IFS splitting if not

Or more succinctly:

backgrounds=(/path/to/backgrounds/*)
random_picture=$backgrounds[$(( RANDOM % $#backgrounds + 1 ))]
# the succinct bash version is random_picture="${backgrounds[$((
RANDOM % ${#backgrounds[@]} ))]}"

This is why I love zsh.

-john


-- 
To unsubscribe, send mail to wmaker-user-unsubscr...@lists.windowmaker.org.

Reply via email to