Solved $((i++)) does not work

It's written everywhere that in sh scripts instead of i=$((i + 1)) you can write $((i++)),
but in my scripts I does not work, I only get error messages.

Any explanations?

Thanks.
 
It's written everywhere [...]
"everywhere" ?

It would have helped I you at least quoted one source (preferably reputable) that states this. What comes to mind:
  1. sh(1)
  2. Shell Command Language - POSIX
  3. Sh - the POSIX Shell
  4. A relevant book that covers sh scripting relating to POSIX or the FreeBSD's sh or equivalent.
  5. Any relevant FreeBSD base sh script source code
I quickly searched #1 - #3 and some books. I haven't seen any mentioning the use of i++ in this context; though I may have missed something.

However, as mentioned in the previous message, bash(1):
Code:
ARITHMETIC EVALUATION
       The  shell allows arithmetic expressions	to be evaluated, under certain
       circumstances (see the let and declare builtin commands,	 the  ((  com-
       pound command, and Arithmetic Expansion).  Evaluation is	done in	fixed-
       width  integers	with  no  check	 for overflow, though division by 0 is
       trapped and flagged as an error.	 The operators and  their  precedence,
       associativity,  and values are the same as in the C language.  The fol-
       lowing list of operators	is grouped into	levels of equal-precedence op-
       erators.	 The levels are	listed in order	of decreasing precedence.

       id++ id--
	      variable post-increment and post-decrement
 
"everywhere" ?
Well, O'Reilly's "Classic Shell Scripting", Addison-Wesley's "Shell Programming in Unix, Linux and OS X", and several online tutorials I found.

I know ((...)) means it's an arithmetic expression, which i++ or i+1 in my eyes is, ain't it?
It would have helped I you at least quoted one source (preferably reputable) that states this.
Sorry. You're right. I just thought more about "Dude, that's so obsolete..." so I thought it wasn't necessary.
(I just combined all I tried in this source to post only one)
sh:
#!/bin/sh

fileliste=$(ls *sh)
i=0
#n=0
for datei in $fileliste
    do
        $((n++))
        j=$((n++))
        i=$((i + 1))
       # echo $datei
    done

echo
echo $n
echo $i
echo $j

exit 0

#!/bin/sh => ./temp.sh: arithmetic expression: expecting EOF: "n++"
#!/bin/bash => ./temp.sh: Command not found.
But if do a bash ./temp.sh the bash moans about line 8 $((n++)), only.
Commenting that line in bash it works.
Doing the same in sh doesn't.
I thought it might be caused by default I'm in tcsh (I know the shebang redirects to the according shell, but there are "few exotic minor" issues between running a sh script from tcsh or sh. But even when I'm doing this in sh or bash, the same.
So I thought:"Ask the guys!"

So, eternal_noob was right.
It's a bash thing.
However, I don't know why this is in books primarily about sh, not mentioning it's bash :-/

Since I don't see much saving in writing this way, I can live with "good old"
i=$((i + 1)), but I wanted to understand why it's not working.


Thanks.

Edit:
Doing bash ./temp.sh with line 8 as ((n++)) with #!/bin/bash, works. :-/
Seems to me I either stumbled over some (minor) error in my books, or about this somethings was changed,...but 2017 does not seem that old to me...

However, I stay with the common way.
Thanks guys!
 
don't do this
sh:
fileliste=$(ls *sh)
do
sh:
for datei in *sh
you don't execute an unnecessary external command and it works better if there are spaces in the file names
consider this
sh:
host$ ls -1
1 2.txt
3 4.txt
5.txt
#-----------------------------------
host$ A=$(ls *txt)
host$ for i in $A;do echo I=$i;done
I=1
I=2.txt
I=3
I=4.txt
I=5.txt
#------------------------------------
host$ for i in *txt;do echo I=$i;done
I=1 2.txt
I=3 4.txt
I=5.txt
 
don't do this
I wasn't trying to do anything on files, really. I was actually experimenting with lists, and misused this ls to get quickly a simple list - lazy 😁

But thanks a lot for this tip anyway! 👍
I will keep that in mind (always interested in improving my [still very bad] programming.)
Thanks.
 
I know ((...)) means it's an arithmetic expression, which i++ or i+1 in my eyes is, ain't it?
For the version of sh that ships with FreeBSD, the definition of "arithmetic" can be found in the man page for sh, in the section "arithmetic expansion". It has a list of all operators, and ++ and -- are not among the ones listed. It explicitly says "The allowed expressions are a subset of C expressions, summarized below". Easy to find that information.

One of the problems with Linux and bash having >99% market- and mind-share is that nearly all material you find on the web thinks that bash is the only shell.
 
thinks that bash is the only shell.
Yeah. I know.
But I'm trying to do sh only, since I find the bash kind of bloated, sometimes even really (too) complicated.
I did some little stuff in perl, and python, and think:"It doesn't make much sense [to me] to increase shell's programming capabilities, when you can get to better solution way less complicated when using python or perl."
But I'm by far not in neither of those languages daring to say this aloud. And for sure there are many who would prove me otherwise.

However,
For the version of sh that ships with FreeBSD, [...] Easy to find that information.
Yes, a peek into its man page may have revealed it.
Since the man page ain't the "Shell Programming for Dummies" I seldom look into that one - even otherwise I am a man page's man - humans are no logic computers 😅

That's what my intention was to start this thread here, to answer the question:
Is there a difference between FreeBSD's sh and Linux'?

Thanks!
 
linux distros use either bash or dash as sh (as hard or sym link)
i know shit about dash but bash as sh seems pretty posix compliant. i heard it's not 100% posix but i don't care that much about it anyway
 
Back
Top