
On Sun, 12 Jul 2020 at 23:26, o1bigtenor <o1bigtenor@gmail.com> wrote:
On Sun, Jul 12, 2020 at 8:48 PM Giles Orr via talk <talk@gtalug.org> wrote:
On Fri, 10 Jul 2020 at 17:51, John Sellens <jsellens@syonex.com> wrote:
On Fri, 2020/07/10 05:39:59PM -0400, Giles Orr <gilesorr@gmail.com> 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\]\$ ')"
Hmmmm - - - - 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/ gilesorr@gmail.com