A $(( expression )) bug?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users A $(( expression )) bug?
# 8  
Old 10-01-2016
Quote:
Originally Posted by RudiC
The [..] arithmetic expansion [..]
(c.f. man bash) is available in recent shells only, not in sh..
Arithmetic expansion is part of the POSIX specification. So it depends on what you mean by sh. If it means the classic (non-POSIX) Bourne shell, then yes. If it means a POSIX shell (which is its meaning on almost any POSIX compliant system, /usr/xpg4/bin/sh on Solaris), then no.
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 10-01-2016
You are right, Scrutinizer, it is even available in my dash...
# 10  
Old 10-01-2016
Hi.

Here is my take on this. Given a modified script:
Code:
#!/bin/sh
txt="1234567890"
echo "$(( $txt ))"
echo "$(( txt ))"
echo "$(( ${#txt} ))"
echo "$(( ${#txt} - 1 ))"
echo "$(( #txt - 1 ))"

On my system:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie)

The shell sh is /bin/sh: symbolic link to dash
When we run shellcheck on the script, we get:
Code:
$ shellcheck s1

In s1 line 3:
echo "$(( $txt ))"
          ^-- SC2004: $ on variables in (( )) is unnecessary.

So far so good, the warning is correct.

If we then run the script, we get:
Code:
$ ./s1
1234567890
1234567890
10
9
./s1: 7: ./s1: arithmetic expression: expecting primary: " #txt - 1 "

So we have fed a syntactically incorrect line into the shell, which has diagnosed it.

If we complain that shellcheck is not diagnosing it, we are on a slippery slope. We would have to re-create the shell mechanism into shellcheck in order to diagnose syntax errors.

The solution, it seems to me, is to get rid of syntax errors, then ask shellcheck to look over the code.

I think we'll all be interested in what the developers say.

Best wishes ... cheers, drl
# 11  
Old 10-02-2016
Well I got 2 emails back from [koalaman/shellcheck]:-
"""
--
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
$(( expression )) error? * Issue #739 * koalaman/shellcheck * GitHub
Code:
"""

First:-
"Closed #739."

So bug report #739 was looked at.

Now second:-
"""
You're right, but shellcheck also does not produce a warning for `echo "$(( ${#txt} - 1 ))"`.

It only produces a warning where the `$` is unnecessary, on line 3.

I've updated the wiki to mention this explicitly.
"""

/Me shrugs.

What is the point of just updating the wiki when it actually does do RED sentences when it is not able to parse a line; after all it is a syntax error...
So in essence it was a waste of time, a lesson learnt for us all...
# 12  
Old 10-02-2016
Just had a first look at shellcheck.
It looks like it makes suggestions in addition to the real faults that are found by sh -nx
# 13  
Old 10-02-2016
Hi all...
Been messing around and guess what...
ShellCheck:-
Code:
#!/bin/sh
txt="12345"
echo "$(( # == txt ))"
echo "$(( $# == txt ))"
echo "$(( $# == $txt ))"
echo "$(( # == $txt ))"
echo "$(( # = txt ))"
echo "$(( $# = txt ))"
echo "$(( $# = $txt ))"
echo "$(( # = $txt ))"

ShellCheck reaults:-
Code:
$ shellcheck myscript
 
Line 5:
echo "$(( $# == $txt ))"
                ^-- SC2004: $/${} is unnecessary on arithmetic variables.
 
Line 6:
echo "$(( # == $txt ))"
               ^-- SC2004: $/${} is unnecessary on arithmetic variables.
 
Line 9:
echo "$(( $# = $txt ))"
               ^-- SC2004: $/${} is unnecessary on arithmetic variables.
 
Line 10:
echo "$(( # = $txt ))"
              ^-- SC2004: $/${} is unnecessary on arithmetic variables.

$

Manually using DASH.
Code:
Last login: Sun Oct  2 10:30:26 on ttys000
AMIGA:barrywalker~> /usr/local/bin/dash
AMIGA:\u\w> txt="123456"
AMIGA:\u\w> echo "$(( # == txt ))"
/usr/local/bin/dash: 2: arithmetic expression: expecting primary: " # == txt "
AMIGA:\u\w> echo "$(( $# == txt ))"
0
AMIGA:\u\w> echo "$(( $# == $txt ))"
0
AMIGA:\u\w> echo "$(( # == $txt ))"
/usr/local/bin/dash: 5: arithmetic expression: expecting primary: " # == 123456 "
AMIGA:\u\w> echo "$(( # = txt ))"
/usr/local/bin/dash: 6: arithmetic expression: expecting primary: " # = txt "
AMIGA:\u\w> echo "$(( $# = txt ))"
/usr/local/bin/dash: 7: arithmetic expression: expecting EOF: " 0 = txt "
AMIGA:\u\w> echo "$(( $# = $txt ))"
/usr/local/bin/dash: 8: arithmetic expression: expecting EOF: " 0 = 123456 "
AMIGA:\u\w> echo "$(( # = $txt ))"
/usr/local/bin/dash: 9: arithmetic expression: expecting primary: " # = 123456 "
AMIGA:\u\w> exit
AMIGA:barrywalker~> _

I have posted a report and it is public here so I am off air with this now...
I am well aware of the errors now and this site has a thread on it...
# 14  
Old 10-02-2016
Hi.
Quote:
Originally Posted by wisecracker
... And the WRONG answer with zsh!
Code:
AMIGA:barrywalker~> zsh
AMIGA:\u\w> zsh --version
zsh 4.3.11 (i386-apple-darwin11.0)
AMIGA:\u\w> txt="1234567890"
AMIGA:\u\w> echo "$(( ${#txt} - 1 ))"
9
AMIGA:\u\w> echo "$(( #txt - 1 ))" 
48
AMIGA:\u\w> _

EDIT:-
This should have been a new post but was added to this one, no idea why!
It turns out that zsh has a specific meaning for #txt:
Code:
       expression of the form `#foo' gives the value of the first character of
       the  contents  of the parameter foo.

excerpt from man zshall, section ARITHMETIC EVALUATION

For example:
Code:
#!/bin/zsh

txt="1234567890"
echo "$(( #txt - 1 ))"

# txt="1234567890"
txt="1"
echo "$(( #txt - 1 ))"
txt="2"
echo "$(( #txt - 1 ))"

producing:
Code:
$ ./z4
48
48
49

On a system:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
zsh 5.0.7

The zsh and man zsh both are incredibly complicated.

Best wishes ... cheers, drl

Last edited by drl; 10-02-2016 at 08:13 AM.. Reason: Add version of OS, zsh
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why Relational Expression is Writing to a Expression?

Hello All, Not sure why this is happening... When the following If Statement is evaluated for some reason it is creating a file in the CWD called '0'. I've seen this happen before, just not in an If Statement... CODE: if then DIR_NAME="$1" DIR_SIZE=0 STATUS="" else... (3 Replies)
Discussion started by: mrm5102
3 Replies

2. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

5. UNIX for Dummies Questions & Answers

where's the bug?

#!/bin/bash if then #echo "infinite loop" exit 0 fi when I run this file I get the following error: ./test_infinite_loop: line 5: syntax error near unexpected token `fi' ./test_infinite_loop: line 5: `fi' :confused: (4 Replies)
Discussion started by: jon80
4 Replies

6. AIX

bug in 43 ???

xxxxserver# lsattr -El inet0 | grep 255.240.0.0,32.224.0.0,32.78.120.254 | grep '.40' route net,-hopcount,1,-netmask,255.240.0.0,32.224.0.0,32.78.120.254 How this is possible? (1 Reply)
Discussion started by: itik
1 Replies

7. Programming

error: initializer expression list treated as compound expression

I had seen this error for the first time ..... error: initializer expression list treated as compound expression please help.... (12 Replies)
Discussion started by: arunchaudhary19
12 Replies

8. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies
Login or Register to Ask a Question