On 11/26/2022 7:52 AM, Milan Zimmermann wrote:
> # Result:
> # "It is 3"
> # "It is NOT 3"
> #
>
> if { = 3 3 } {
> echo "It is 3"
> }
> {
> echo "It is NOT 3"
> }
According to Eshell's logic, I think this is correct (though
inconvenient). Because Eshell treats a newline as the end of a command
whenever possible, it just sees these as two separate commands.
Yes, I agree. From the way of thinking "whitespace should not matter" it is a surprising behavior though.
> # BUT we get the same incorrect result if we place the whole if
> _expression_ into {}
> {
> if { = 4 4 } {
> echo "It is 4"
> }
> {
> echo "It is NOT 4"
> }
> }
This is really the same as the above: {...} allows multiple commands, so
it sees this as two separate commands nested inside the {}.
Yeah, I realized the same during my hike today. The wrapping {..} should not make a difference, asking for that would make things worse.
Ultimately, I think this is closer to a feature request: adding an
"else" token would disambiguate this:
if { = 2 2 } {
echo "good"
}
else {
echo "bad"
}
Actually making this work in Eshell's internals might be painful though...
Yes. Well, personally I feel the current behavior means that "lisp-iness" seeps through too much into the scripting behavior. So a feature request for adding and "else" would work for me. I will abstain from doing that at the moment, as I feel asking something like that requires a deeper review of how close eshell should behave like lisp syntax-wise. But if someone files such a request, I will not complain :)
BTW, a slightly related question if I may: A further diversion of lisp-iness, I do not suppose there is a way to do a "return"? In bash, the ability to "return" from sourced bash scripts or functions allows us to deal with errors at the beginning, then process the main logic. In Eshell , I am doing things like:
=============
if ${ not { = {length $*} 3 } } {
echo "Invalid arguments: $*"
echo "Usage: $0 file-to-generate.dart snippet.dart template.dart"
} {
if ${ not { file-exists-p $2 || file-exists-p $3 } } {
echo "File $2 or $3 does not exist."
} {
##### The main logic here is indented 2 levels deep
export file-to-generate=$1
export snippet="${cat $2}"
export template="${cat $3}"
echo $file-to-generate
echo $snippet
echo $template
# todo : check if template contains string %s
# format string template, substitute snippet, place to generated-code
# echo generated-code to file-to-generate
}
}
=============
I do also see a potential bug. I'd expect this to work, but it doesn't:
if { = 2 2 } \
{ echo "good" } \
{ echo "bad" }
Yeah, you are right. I just tried that.
Thanks,
Milan