Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 108:27:47 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,683 |
For this command
typeset -i indents=$(( level * indent_factor )) ###
with two integer variables, 'level' and 'indent_factor', declared
I'm getting this nice warning message
warning: line 28: indents=$(( level * indent_factor ))
is slower than ((indents= level * indent_factor ))
I thought (to avoid the warning) I'd have to split the lines like
typeset -i indents
(( indents = level * indent_factor ))
but I noticed that we can also write
(( typeset -i indents = level * indent_factor ))
(I wasn't aware about the 'typeset' command possible in arithmetic expressions.)
Though I wonder why Ksh doesn't silently optimize the '###' marked
code since Ksh optimizes so many less obvious and much more complex
things.
Janis
On 25.10.2024 06:46, Janis Papanagnou wrote:[...]
(( typeset -i indents = level * indent_factor ))
(I wasn't aware about the 'typeset' command possible in arithmetic
expressions.)
Oops! - That's wrong. - I had just tested that only with 'ksh -n'.
ksh -n had produced the above performance warning message, but it
did not produce an error message for the (( typeset ... )) syntax,
which I saw just now when invoking the shell regularly on the code.
This is really odd (shell-)behavior! (Reporting a warning but not
an error.) - Looks like a bug to me.[*]
[*] The man page says for option '-n':
"Read commands and check them for syntax errors, [...]"
So the warning message is just an undocumented feature for free?
Op 28-10-2024 om 00:21 schreef Janis Papanagnou:
On 25.10.2024 06:46, Janis Papanagnou wrote:
[...]
C-style shell arithmetic is treated as mostly separate from the shell language proper. As far as the shell language is concerned, an
arithmetic expansion or command is just a glorified quoted string. So,
they are parsed separately from the shell language, and both parsing and executing happens at runtime. This means shell arithmetic syntax errors
can only be detected while executing the script, i.e., not when using
noexec. All the POSIX-based shells are the same in that, because they
all copied ksh's design for shell arithmetic.
[*] The man page says for option '-n':
"Read commands and check them for syntax errors, [...]"
So the warning message is just an undocumented feature for free?
Yes. The -n (noexec) option activates a (very limited) linter mode that produces certain warnings. The arithmetic-related linter warnings don't
look at the arithmetic expression itself at all, but only at the shell grammar context in which an arithmetic expression is used.