Re: [SLUG] quoting

From: Eben King (eben01@verizon.net)
Date: Fri Jun 02 2006 - 18:17:42 EDT


On Fri, 2 Jun 2006, John Kinary wrote:

> Eben King wrote:
>> OK, I can't figure this out. I'm sure it's logical, but I don't have a
>> handle on the logic.
>>
>> What I've got is a shell script, written in ksh (think of it as a dialect
>> of bash):
>>
>> ...
>> 40 function choose_a_background
...
> A friend of mine gave this solution:
>
> Re:
>
>> > So, I need to add "-name tall\*" or "! -name tall\*" to find's
>> > options. I've tried between 1 and 6 backslashes in line 47, single
>> > quotes around tall_\\* with varying numbers of backslashes, reducing
>> > "$BACKGROUNDDIR $PATTERNDIR" to "/export", reducing tall_\\* to \\*,
>> > and what I've found out is that:
>> > with this many backslashes it'll find
>> > 0 something having to do with filenames in $PWD
>> > 1 files named "*"
>> > 2 files named "\*"?
>> > 3+ ??
>>
>
> I don't think so. I don't think he tried 0 backslashes.

Well, maybe pdksh acts differently than bash, but TTBOMK I did. Can't try
it now, as I'm away from the machine and can't see any effect.

I expanded the variable out, so it works now (but isn't pretty).

> By the way, I was impressed by the way a random number was generated:
> RANDOM=$(dd if=/dev/random bs=4 count=1 2>/dev/null |\
> sum - | cut -f 1 -d " ")
>
> which should give a value in the range 0 through 65535. But *not*
> in bash where the $RANDOM variable generates an new random number
> in the range 0 through 32767 each time it is referenced.

Yup, ksh too -- 10000 "echo $RANDOM"s and the highest was 32745. OTOH, I
don't have anywhere _near_ 32K images. Still, I might want to use
/dev/random there and dispense with $RANDOM altogether, since I only need
randomness once.

> You didn't show how the image file was selected. My guess is (using
> bash's RANDOM):
>
> numfiles=$(find $FIND_FLAGS ! -name 'random*' | wc -l)
> imagenum=$(expr $numfiles \* $RANDOM / 32767 + 1)
> imagefile=$(find $FIND_FLAGS ! -name 'random*' |\
> sed -n ${imagenum}p)

Like this:

numfiles=$(find $BACKGROUNDDIR $PATTERNDIR -type f $bang -name tall_\* ! -name random\* | wc -l)

pick_num=$(( 1 + RANDOM * numfiles / 32767 ))

find $BACKGROUNDDIR $PATTERNDIR -type f $bang -name tall_\* ! -name random\* \
| sed -n ${pick_num}'{p;q;}'

-- 
-eben   QebWenE01R@vTerYizUonI.nOetP   royalty.no-ip.org:81

He who will not reason is a bigot; he who cannot is a fool; and he who dares not is a slave. -Sir William Drummond ----------------------------------------------------------------------- This list is provided as an unmoderated internet service by Networked Knowledge Systems (NKS). Views and opinions expressed in messages posted are those of the author and do not necessarily reflect the official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:00:31 EDT