Facing problem in incrementing the variable

When I did,
echo $SHELL in cmd prompt of putty,
its displaying /bin/sh

And in my shell script.,

I hav started with.,
#!/bin/sh

and

i=1;

while [ $i -lt $VAR ] ;
do
.
.
.

i=$[$i + 1];
#i=`expr $i +1`;
#i=`bc $i + 1`
#i=`($i+1 | bc)`

done

I am trying to increment the var, its running to infinite loop with o/p:

inside loop
./run1.sh: +: not found
inside loop
./run1.sh: +: not found
inside loop
./run1.sh: +: not found
inside loop
./run1.sh: +: not found
inside loop
./run1.sh: +: not found
inside loop
./run1.sh: +: not found
.
.
.
.

Also I tried with other ways of incrementing the var, as indicated with bold text.

I request you to guide to debug this error.

Thanks.,

Try

i=$(($i+1))

I tried with the above syntax,

still it is throwing the error:

./run1.sh: syntax error at line 44: `i=$' unexpected

Regards.,

Can you please try this code alone and let me know what is its ouptut:

#!/bin/sh

i=0
VAR=10

while [ $i -lt $VAR ]
do
i=$(($i+1))
echo $i
done

If it works then there's probably another error in you script (please post your complete script in this case), else you work with a non-posix sh or something.

I tried with the sample script provided. The same error I am getting as an output. ..

$ ./rr.sh
./rr.sh: syntax error at line 8: `i=$' unexpected

And also in your reply, you said about " non-posix sh " , whether it means coding inside bash script?
Actually I tried with writing the things starting with #!/bin/bash also, but din't succeeded doing that either.

Is there any other way of incrementing the var in sh script?.

Thanks.,

change (ksh/bash specific)

i=$(($i+1))

to (Bourne)

i=`expr $i + 1`

av_vinay,
please try my sample code, but this time change the i=$(($i+1)) line to i="$(($i+1))"
If it won't work, change the shebang line from #!/bin/sh to #!/bin/bash

Let me know how it goes.

Those are all wrong.

$[$i + 1]; I don't even know what this is supposed to be :wink:

`expr $i +1` should have a space after the plus sign.

`bc $i + 1` does not work because bc reads its commands/data from files and standard input, not directly from the command line.

`($i+1 | bc)` tries to run a pipeline in a subshell and the first command's name is "$i+1", which most likely does not exist. What you want there is to echo into bc: `echo "$i+1" | bc`

If you continue to have problems, even with simple scripts, take a close look at the sh script file to see if there are any unwanted, unseen characters. You can use 'od -bc scriptfile' to take an unambiguous look. putty implies that windows is involved. If you are editing and/or copy-pasting in windows, there could be carriage returns in the file. If that's the case, use dos2unix or 'tr -d \\r' to remove them and try to run the resulting file.

If problems persist, please provide more info. /bin/sh alone doesn't say much. At minimum, specify which operating system and shell you are using.

Actually, that arithmetic expansion is posix-compliant.

Regards,
Alister

I hav tried with seperate script with #!/bin/bash, it worked fine.

But I am facing a problem in replacing the file contents by iterating through the list.

My present code:

#!/bin/bash

#TFILE="/tmp/vinay/testb_1.txt"

while read line
do
  aline="$line"
                echo $aline
   code=`echo $aline|cut -d ',' -f1`
   country=`echo $aline|cut -d ',' -f 2`

sed "s/$code/$country/g" testb_1.txt>testb_1.txt
#sed 's/$/| testb_1.txt/g' "$TFILE" > "output1.txt"
echo $code
echo $country
done<country_code.txt

and my

country code file contains some county code on line basis, seperated with commas.

Here after executing the script, only last country code is replacing with its country name. And if I change > (overwriting) to >> (appending) then same contents with changes are appending to output file. I request you to suggest a solution for this.

And my second concern is that I have to append the name of the file at the end of each line, which I hav done using the commented line. Request you to suggest to perform both actions at a stretch.

Thanks.,

The above code can be significantly simplified

i=0
VAR=10

while (( i++ < VAR ))
do
   echo $i
done

Works with bash and ksh93.