Re: [GTALUG] Bash does-directory-exist question

2020-07-13 Thread Giles Orr via talk
On Sun, 12 Jul 2020 at 23:26, o1bigtenor  wrote:
> On Sun, Jul 12, 2020 at 8:48 PM Giles Orr via talk  wrote:
>> On Fri, 10 Jul 2020 at 17:51, John Sellens  wrote:
>> > On Fri, 2020/07/10 05:39:59PM -0400, Giles Orr  wrote:
>> > | I love this list!  I thought that '[ -w . ]' and '[ -w $PWD ]' were
>> > | practically equivalent.  "Practically" means, in this case, "almost."
>> > | But not quite - and the difference is the solution to the problem.
>> >
>> > It's a very important, though sometimes subtle, concept in unix-land
>> > that there are multiple names for just about anything.
>> >
>> > Here, obviously, $PWD is a variable substitution equivalent to /some/path,
>> > which likely existed at some point, but may or may not exist now.  The
>> > directory "." always (I think) exists, because a process always has a
>> > current directory open. (Hmmm, but opendir(".") might not work?)
>> >
>> > The other canonical example is "how do I remove a file that starts with 
>> > -?".
>> > The key to that of course is the multiple names thing "-file" (which looks
>> > like an option string) is the same as "./-file" (which doesn't).
>> >
>> > Once you understand that, the world opens up :-)
>> >
>> > Of course, most times "rm -- -file" works but I'm old enough (uh, I mean
>> > I've read about the history of unix) to know that -- didn't always exist.
>>
>> Here's a simple implementation of a Bash prompt using what we were 
>> discussing:
>>
>> PS1="\$(if ! [ -w "\${PWD}" ]; then echo -en '\[\033[41m\]' ; fi ;
>> echo '\w\[\033[0m\]\$ ')"
>>
> H - - - - I do believe that you suggested  " . . . simple . . .  ." - - - 
> yes?

For better or worse, that's about as simple as that kind of
implementation gets.  The if-then-fi should be fairly clear.  The '\w'
is a Bash prompt short form for "the working directory."  The super
ugly bits are colour codes.  Which you could do without, but without
them the directory name doesn't look different when the directory
becomes unwritable.  The '\033[41m' changes the background to red.
The '\033[0m' turns off all colour codes so everything after it is in
plain colours.  Both are surrounded in '\[' and '\]' to indicate to
Bash that they're non-printing characters, or more correctly that they
take up no physical space ... that's a long story I won't get into
here, suffice to say they're necessary in a prompt.

Without colour codes:

PS1="\w\$(if ! [ -w "\${PWD}" ]; then echo -en '(RO)' ; fi )\$ "

In this one - as in the last one - there's a lot of backslash-escaping
things so they don't get interpreted right away, instead being
interpreted each time the $PS1 is shown.  The '\w' and '\$' are both
special commands for the prompt, not backslash escapes.

That's the best I can do - I hope it helps.

-- 
Giles
https://www.gilesorr.com/
giles...@gmail.com
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-12 Thread o1bigtenor via talk
On Sun, Jul 12, 2020 at 8:48 PM Giles Orr via talk  wrote:

> On Fri, 10 Jul 2020 at 17:51, John Sellens  wrote:
> >
> > On Fri, 2020/07/10 05:39:59PM -0400, Giles Orr 
> wrote:
> > | I love this list!  I thought that '[ -w . ]' and '[ -w $PWD ]' were
> > | practically equivalent.  "Practically" means, in this case, "almost."
> > | But not quite - and the difference is the solution to the problem.
> >
> > It's a very important, though sometimes subtle, concept in unix-land
> > that there are multiple names for just about anything.
> >
> > Here, obviously, $PWD is a variable substitution equivalent to
> /some/path,
> > which likely existed at some point, but may or may not exist now.  The
> > directory "." always (I think) exists, because a process always has a
> > current directory open. (Hmmm, but opendir(".") might not work?)
> >
> > The other canonical example is "how do I remove a file that starts with
> -?".
> > The key to that of course is the multiple names thing "-file" (which
> looks
> > like an option string) is the same as "./-file" (which doesn't).
> >
> > Once you understand that, the world opens up :-)
> >
> > Of course, most times "rm -- -file" works but I'm old enough (uh, I mean
> > I've read about the history of unix) to know that -- didn't always exist.
>
> Here's a simple implementation of a Bash prompt using what we were
> discussing:
>
> PS1="\$(if ! [ -w "\${PWD}" ]; then echo -en '\[\033[41m\]' ; fi ;
> echo '\w\[\033[0m\]\$ ')"
>
> H - - - - I do believe that you suggested  " . . . simple . . .  ." -
- - yes?

Regards
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-12 Thread Giles Orr via talk
On Fri, 10 Jul 2020 at 17:51, John Sellens  wrote:
>
> On Fri, 2020/07/10 05:39:59PM -0400, Giles Orr  wrote:
> | I love this list!  I thought that '[ -w . ]' and '[ -w $PWD ]' were
> | practically equivalent.  "Practically" means, in this case, "almost."
> | But not quite - and the difference is the solution to the problem.
>
> It's a very important, though sometimes subtle, concept in unix-land
> that there are multiple names for just about anything.
>
> Here, obviously, $PWD is a variable substitution equivalent to /some/path,
> which likely existed at some point, but may or may not exist now.  The
> directory "." always (I think) exists, because a process always has a
> current directory open. (Hmmm, but opendir(".") might not work?)
>
> The other canonical example is "how do I remove a file that starts with -?".
> The key to that of course is the multiple names thing "-file" (which looks
> like an option string) is the same as "./-file" (which doesn't).
>
> Once you understand that, the world opens up :-)
>
> Of course, most times "rm -- -file" works but I'm old enough (uh, I mean
> I've read about the history of unix) to know that -- didn't always exist.

Here's a simple implementation of a Bash prompt using what we were discussing:

PS1="\$(if ! [ -w "\${PWD}" ]; then echo -en '\[\033[41m\]' ; fi ;
echo '\w\[\033[0m\]\$ ')"

-- 
Giles
https://www.gilesorr.com/
giles...@gmail.com
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-10 Thread Giles Orr via talk
On Fri, 10 Jul 2020 at 11:11, David Mason  wrote:
>
> (base) : ~/foo ; [ -w .. ] && echo true
> true
> (base) : ~/foo ; /bin/pwd
> pwd: .: No such file or directory
> (base) : ~/foo ; pwd
> /Users/dmason/foo
> (base) : ~/foo ; [ -w $PWD ] && echo true
> (base) : ~/foo ;
>
> So, /bin/pwd fails and [ -w $PWD ] also fails, as John hypothesized
>
> ../Dave
> On Jul 10, 2020, 11:01 AM -0400, John Sellens via talk , 
> wrote:
>
> On Fri, 2020/07/10 09:38:48AM -0400, Giles Orr via talk  
> wrote:
> | This gives immediate visual feedback on the write-status of the
> | current directory. But test's '-w' and '-d' both claim that you're
> | still in a valid directory under the above circumstances. Does anyone
> | know of a simple way to find out if the directory you're currently in
> | actually exists?
>
> The directory "." will still exist while you have it open (your current
> directory), but will be unreachable, as you observed with stat(1) and
> the number of links.
>
> Would checking for "test -d $PWD" work? I think $PWD is the full path
> and so if it's no longer reachable, the test should fail?
>
> Hope that helps

I love this list!  I thought that '[ -w . ]' and '[ -w $PWD ]' were
practically equivalent.  "Practically" means, in this case, "almost."
But not quite - and the difference is the solution to the problem.
Thanks everyone, particularly John and Dave.

-- 
Giles
https://www.gilesorr.com/
giles...@gmail.com
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-10 Thread Christopher Browne via talk
On Fri, 10 Jul 2020 at 09:39, Giles Orr via talk  wrote:

> The worst case I've seen is 'git', and this is what's brought me back
> to this puzzle. If you're in a directory and you 'git rm' the last
> file in the directory, 'git' will helpfully delete the directory as
> well.  Never mind that you're still in the directory, and are now in
> the very confusing position of being in a non-existent directory.
>

Huh.  I just experienced this situation for (as far as I can tell) the
very first time about 1/2h ago.  I was in a directory, did a merge
that got rid of the only file in $PWD, and found myself in that
somewhat odd place.

I wasn't overly confused at it; I realized that a broadly reasonable
thing was happening.   But yeah, it was strange.

I was in zsh rather than bash; that made little difference in the
matter.  I had a strange looking prompt, but all resolved fine.
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-10 Thread David Mason via talk
(base) : ~/foo ; [ -w .. ] && echo true
true
(base) : ~/foo ; /bin/pwd
pwd: .: No such file or directory
(base) : ~/foo ; pwd
/Users/dmason/foo
(base) : ~/foo ; [ -w $PWD ] && echo true
(base) : ~/foo ;

So, /bin/pwd fails and [ -w $PWD ] also fails, as John hypothesized

../Dave
On Jul 10, 2020, 11:01 AM -0400, John Sellens via talk , wrote:
> On Fri, 2020/07/10 09:38:48AM -0400, Giles Orr via talk  
> wrote:
> | This gives immediate visual feedback on the write-status of the
> | current directory. But test's '-w' and '-d' both claim that you're
> | still in a valid directory under the above circumstances. Does anyone
> | know of a simple way to find out if the directory you're currently in
> | actually exists?
>
> The directory "." will still exist while you have it open (your current
> directory), but will be unreachable, as you observed with stat(1) and
> the number of links.
>
> Would checking for "test -d $PWD" work? I think $PWD is the full path
> and so if it's no longer reachable, the test should fail?
>
> Hope that helps
>
> John
> ---
> Post to this mailing list talk@gtalug.org
> Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-10 Thread John Sellens via talk
On Fri, 2020/07/10 09:38:48AM -0400, Giles Orr via talk  wrote:
| This gives immediate visual feedback on the write-status of the
| current directory.  But test's '-w' and '-d' both claim that you're
| still in a valid directory under the above circumstances.  Does anyone
| know of a simple way to find out if the directory you're currently in
| actually exists?

The directory "." will still exist while you have it open (your current
directory), but will be unreachable, as you observed with stat(1) and
the number of links.

Would checking for "test -d $PWD" work?  I think $PWD is the full path
and so if it's no longer reachable, the test should fail?

Hope that helps

John
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


Re: [GTALUG] Bash does-directory-exist question

2020-07-10 Thread Howard Gibson via talk
On Fri, 10 Jul 2020 09:38:48 -0400
Giles Orr via talk  wrote:

> I have a strange Bash question for you.  It's an edge case, but I've
> run into it just often enough that I'd like to know how to deal with
> it.
> 
> How do you determine if the directory you're in has been deleted?

Giles,

   Is this command line or scripting?

-- 
Howard Gibson 
hgib...@eol.ca
jhowardgib...@gmail.com
http://home.eol.ca/~hgibson
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk


[GTALUG] Bash does-directory-exist question

2020-07-10 Thread Giles Orr via talk
I have a strange Bash question for you.  It's an edge case, but I've
run into it just often enough that I'd like to know how to deal with
it.

How do you determine if the directory you're in has been deleted?

I've done this to myself: 'cd' to a directory in one terminal, then
'rm -r' that same directory in another terminal.  It becomes very
confusing when you try to do something in the first terminal: 'touch
test.txt' responds with "touch: cannot touch 'test.txt': No such file
or directory".  Which is exceptionally hard to decipher because of
course it doesn't exist, I was trying to create it!

The worst case I've seen is 'git', and this is what's brought me back
to this puzzle. If you're in a directory and you 'git rm' the last
file in the directory, 'git' will helpfully delete the directory as
well.  Never mind that you're still in the directory, and are now in
the very confusing position of being in a non-existent directory.

One of my hobbies is tinkering with Bash Prompts.  It's fairly easy to
use 'test/[' to determine if a directory is writable with 'if [ -w .]
...' and then change the colour of the prompt based on the response.
This gives immediate visual feedback on the write-status of the
current directory.  But test's '-w' and '-d' both claim that you're
still in a valid directory under the above circumstances.  Does anyone
know of a simple way to find out if the directory you're currently in
actually exists?

I may have found my own answer: 'stat .' returns (in part) "Links: 0"
which would seem to indicate a non-existent node.  Is that a
definitive answer?  (I don't use 'stat' much.)  I'm also interested to
see if anyone else has a simpler or more "Bash" answer.

Thanks.

-- 
Giles
https://www.gilesorr.com/
giles...@gmail.com
---
Post to this mailing list talk@gtalug.org
Unsubscribe from this mailing list https://gtalug.org/mailman/listinfo/talk