Apologies: the first code example in my previous email won't check for a
given directory, and will print out bogus paths:

~$ echo "/Users/none/bogus_dir/" | raku -e 'for lines.IO -> $a is copy
{$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir};'
/Users/none/bogus_dir/
/Users/none
/Users
/

The second code example will first check for a given directory:

~$ echo "/Users/admin/Documents/" | raku -e 'for lines.IO -> $a is copy {if
$a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir}};'
/Users/admin/Documents/
/Users/admin
/Users
/
~$ echo "/Users/none/bogus_dir/" | raku -e 'for lines.IO -> $a is copy {if
$a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
$*SPEC.rootdir}};'
~$

https://docs.raku.org/type/IO::Path#File_test_operators

HTH, Bill.

On Wed, Sep 7, 2022 at 7:39 AM William Michels <w...@caa.columbia.edu>
wrote:

> Hi Marc,
>
> Does this do what you want? I've omitted the call to `run` and used mostly
> IO::Path calls instead:
>
> ~$ echo "/Users/admin/logs" | raku -e 'for lines.IO -> $a is copy
> {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
> $*SPEC.rootdir;};'
>
> #returns:
>
> /Users/admin/logs
> /Users/admin
> /Users
> /
>
> Note, above won't actually check if the given directory is valid--it only
> checks the parents. To check a given directory first, try the following:
>
> ~$ echo "/Users/admin/bogus_dir/" | raku -e 'for lines.IO -> $a is copy
> {if $a.d {$a.Str.say; repeat {$a.=parent andthen $a.Str.say;} until $a eq
> $*SPEC.rootdir}};'
>
> #returns nothing for `bogus_dir`
>
> HTH, Bill.
>
>
> On Mon, Sep 5, 2022 at 1:56 PM Marc Chantreux <m...@unistra.fr> wrote:
>
>> I love this one. I used uniq and run so the whole script can be run from
>> raku (except the xargs ls avoid the ARG_MAX error)
>>
>> <<. raku -e 'run < ls -lUd >, unique map {(.IO, *.parent …^
>> "/")>>.Str.Slip}, lines'
>> /var/log/messages
>> /var/log/auth.log
>>
>> regards
>>
>> --
>> Marc Chantreux
>> Pôle de Calcul et Services Avancés à la Recherche (CESAR)
>> http://annuaire.unistra.fr/p/20200
>>
>

Reply via email to