* curiosity with here document. @ 2015-11-26 18:37 Ray Andrews 2015-11-26 19:01 ` ZyX 2015-11-26 19:03 ` ZyX 0 siblings, 2 replies; 6+ messages in thread From: Ray Andrews @ 2015-11-26 18:37 UTC (permalink / raw) To: Zsh Users I often comment out blocks of code with here-document, and I just found this strange thing: /test1 ()/ /{/ / if [[ "$1" = 'howdy' ]];/ /: <<'COMMENT'/ /blah blah COMMENT/ / then/ / echo stranger/ / fi/ /}/ /$ test1 no/ /stranger/ There's no issue if I put the 'then' above the here-document. How should I understand that? It is as if the here-document is hijacking the 'if' test and returning true. I'd expect the 'if' to go hunting for it's 'then' and if it doesn't find it, then throw and error, but not let the here-document interlope. But if this is proper then it's a caution as to using these things for commenting. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: curiosity with here document. 2015-11-26 18:37 curiosity with here document Ray Andrews @ 2015-11-26 19:01 ` ZyX 2015-11-26 19:03 ` ZyX 1 sibling, 0 replies; 6+ messages in thread From: ZyX @ 2015-11-26 19:01 UTC (permalink / raw) To: Ray Andrews, Zsh Users 26.11.2015, 21:38, "Ray Andrews" <rayandrews@eastlink.ca>: > I often comment out blocks of code with here-document, and I just found > this strange thing: > > /test1 ()/ > /{/ > / if [[ "$1" = 'howdy' ]];/ > > /: <<'COMMENT'/ > /blah blah > COMMENT/ > > / then/ > / echo stranger/ > / fi/ > /}/ > > /$ test1 no/ > /stranger/ > > There's no issue if I put the 'then' above the here-document. How should > I understand that? It is as if the here-document is hijacking the 'if' > test and returning true. I'd expect the 'if' to go hunting for it's > 'then' and if it doesn't find it, then throw and error, but not let the > here-document interlope. But if this is proper then it's a caution as > to using these things for commenting. This has nothing to do with here document. You can have any *sequence* of commands as the `if` condition, including `:`. What this sequence returns is used for the if: if true; false then echo True else echo False fi will echo False. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: curiosity with here document. 2015-11-26 18:37 curiosity with here document Ray Andrews 2015-11-26 19:01 ` ZyX @ 2015-11-26 19:03 ` ZyX 2015-11-26 23:19 ` Ray Andrews 1 sibling, 1 reply; 6+ messages in thread From: ZyX @ 2015-11-26 19:03 UTC (permalink / raw) To: Ray Andrews, Zsh Users 26.11.2015, 21:38, "Ray Andrews" <rayandrews@eastlink.ca>: > I often comment out blocks of code with here-document, and I just found > this strange thing: > > /test1 ()/ > /{/ > / if [[ "$1" = 'howdy' ]];/ > > /: <<'COMMENT'/ > /blah blah > COMMENT/ > > / then/ > / echo stranger/ > / fi/ > /}/ > > /$ test1 no/ > /stranger/ > > There's no issue if I put the 'then' above the here-document. How should > I understand that? It is as if the here-document is hijacking the 'if' > test and returning true. I'd expect the 'if' to go hunting for it's > 'then' and if it doesn't find it, then throw and error, but not let the > here-document interlope. But if this is proper then it's a caution as > to using these things for commenting. And when writing such questions do read documentation. The specification for `if` looks like if list then list [ elif list then list ] ... [ else list ] fi and using “list” for *both* `if` condition and `then` block should have given you an answer. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: curiosity with here document. 2015-11-26 19:03 ` ZyX @ 2015-11-26 23:19 ` Ray Andrews 2015-11-27 1:56 ` Bart Schaefer 0 siblings, 1 reply; 6+ messages in thread From: Ray Andrews @ 2015-11-26 23:19 UTC (permalink / raw) To: zsh-users On 11/26/2015 11:03 AM, ZyX wrote: > 26.11.2015, 21:38, "Ray Andrews" <rayandrews@eastlink.ca>: >> And when writing such questions do read documentation. The specification for `if` looks like >> >> if list then list Until you know what 'list' entails, " if list then list " doesn't answer every question. I've never seen anything like this previously so I wanted some knowledgeable comment, begging your pardon and thanks, I now know that a here-doc is true as far as any following 'then' is concerned, and any preceding 'if' test is ignored, which seems strange to me, but there it is. So using that for commenting is not always safe; lesson learned. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: curiosity with here document. 2015-11-26 23:19 ` Ray Andrews @ 2015-11-27 1:56 ` Bart Schaefer 2015-11-27 2:29 ` Ray Andrews 0 siblings, 1 reply; 6+ messages in thread From: Bart Schaefer @ 2015-11-27 1:56 UTC (permalink / raw) To: zsh-users On Nov 26, 3:19pm, Ray Andrews wrote: } } lesson learned. Except it's the wrong lesson! What you *ought* to learn is, first, that for purposes of determining the "truth value" (zero or nonzero return status), if command1; command2; command3; then ... is the same as function three_commands { command1; command2; command3 } if three_commands; then ... And second, that [[ ... ]] is just a command like any other command, it's not magically connected to "if". Further, ":" is also a command like any other command. And third, that a here-document is just the standard input of the command it follows, so : <<END some stuff END is (again for purposes of determining truth value) the same as : and the colon command is "true" for purposes of if/while/until/etc., it has a return status of zero. So what you've written is if [[ ... ]]; : ; then ... and colon is true and is the last command before the "then", so the true branch is taken. The here-document itself has no true or false interpretation, and the presence of the here-document is not what is causing "if" to behave the way it does. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: curiosity with here document. 2015-11-27 1:56 ` Bart Schaefer @ 2015-11-27 2:29 ` Ray Andrews 0 siblings, 0 replies; 6+ messages in thread From: Ray Andrews @ 2015-11-27 2:29 UTC (permalink / raw) To: zsh-users On 11/26/2015 05:56 PM, Bart Schaefer wrote: > On Nov 26, 3:19pm, Ray Andrews wrote: > } > } lesson learned. > > Except it's the wrong lesson! Well no, it is a lesson to be careful trying to use here-docs to comment blocks. As to why it goes off the rails, I was close to understanding it anyway--I'd expect legal code like that to have some sort of truth value, inconvenient as it may be. I'm not really surprised by the correct understanding. > function three_commands { > command1; command2; command3 > } > if three_commands; then ... Sure, but in practice I don't think we see that sort of 'list' after an 'if' very often. It seems that only the truth value of the last statement is acted upon as far as any 'if-then' structure, so as I said, the original 'if' seems to be orphaned. So what? It's me put the parser into that strange position, I'm not bitchin', it did what it had to do. Don't want orphan tests? Don't make them. > And second, that [[ ... ]] is just a command like any other command, > it's not magically connected to "if". That's the sort of fact that one might think one understands without really understanding it. I probably can't hardly help C-ing it the C way. I appreciate these deep corrections. > And third, that a here-document is just the standard input of the > command it follows, so > > : <<END > some stuff > END > > is (again for purposes of determining truth value) the same as > > : Yeah, I'm really not as thick as I seem about this. Wanting the 'comment' to be available at any point wasn't realistic. Not one of my better questions. BTW, I wish there was a way of going back to the archives and deleting lousy questions, I'd sure cut out a pile of mine. Library of Babel. ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2015-11-27 2:59 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2015-11-26 18:37 curiosity with here document Ray Andrews 2015-11-26 19:01 ` ZyX 2015-11-26 19:03 ` ZyX 2015-11-26 23:19 ` Ray Andrews 2015-11-27 1:56 ` Bart Schaefer 2015-11-27 2:29 ` Ray Andrews
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/zsh/ This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).