Facing problem in incrementing the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Facing problem in incrementing the variable
# 1  
Old 05-20-2010
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.,
# 2  
Old 05-20-2010
Try
Code:
i=$(($i+1))

# 3  
Old 05-20-2010
I tried with the above syntax,

still it is throwing the error:

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


Regards.,
# 4  
Old 05-20-2010
Can you please try this code alone and let me know what is its ouptut:
Code:
#!/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.
# 5  
Old 05-20-2010
I tried with the sample script provided. The same error I am getting as an output. ..

Code:
$ ./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.,


Last edited by pludi; 05-21-2010 at 03:18 AM..
# 6  
Old 05-20-2010
Quote:
Originally Posted by pseudocoder
Can you please try this code alone and let me know what is its ouptut:
Code:
#!/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.
change (ksh/bash specific)
Code:
i=$(($i+1))

to (Bourne)
Code:
i=`expr $i + 1`

# 7  
Old 05-20-2010
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing specific and incrementing lines of text from file via variable

This is part of a larger script where I need to pass only 1 line of a file to the script, based on a variable and not a direct reference. As part of a for loop : # for((line=0;line<50;line++)); do # awk ‘NR==$line' PhraseList.txt; done ... (5 Replies)
Discussion started by: Seth
5 Replies

2. Shell Programming and Scripting

incrementing the variable name along with the data?

Hello folks. I am trying to increment my variable names to match a counter that is to be used later on... Basically, i have a for loop that lists directories (for example TEST_OS DVP_OS PROD_OS ) but this loop is not static, it may contain 3 directory once and the next run 5 directories. I... (6 Replies)
Discussion started by: Stephan
6 Replies

3. AIX

facing problem using su

Hi, I am able to login using su - or su directly , # prompt is coming, it doesnt ask for password. any normal user on aix system is login using su - or su . Please suggest where to change the configuration direct root login is disabled in /etc/ssh/sshd_config file. (0 Replies)
Discussion started by: manoj.solaris
0 Replies

4. Shell Programming and Scripting

AWK SCRIPT HELP : INCREMENTING PROBLEM

Hi Guys , I am having one command file like this FILE1 ################################ awk '/output/ {a=$2} {for(i=1;i<=NF;i++) { gsub("i1", i) ; gsub("i2",++i) ; gsub("P1", p) }}1' output >> out9 awk '/output/ {a=$2} {for(i=1;i<=NF;i++) { gsub("i1", i) ;... (2 Replies)
Discussion started by: jaita
2 Replies

5. UNIX for Dummies Questions & Answers

Incrementing variable in for

Hi, want to increment a variable in a for loop like this: for (( c=$total-1; c>=0; c-- )) do if ; then maximo=$valores fi done But it gives the error: No such file or directory How can i do this only incrementing the c variable? Thanks (8 Replies)
Discussion started by: limadario
8 Replies

6. UNIX for Dummies Questions & Answers

Incrementing Variable Names

Hi, I am using BASH. I have encountered a situation where the following is necessary (but I am not sure how to do it): #Define multiple arrays, whose names only differ by a number: ARRAY_1=(1 2 3) ARRAY_2=(4 5 6) ARRAY_3=(7 8 9) #Define ARRAY_AMOUNT, the number of arrays. In this case... (1 Reply)
Discussion started by: msb65
1 Replies

7. Homework & Coursework Questions

Incrementing Variable resets outside of while loop

1. The problem statement, all variables and given/known data: Variable is resetting to 0 after incrementing in while loop My bit of scripting displays the current users logged in the machine. Then it reads in a specific username and displays the processes for that user. The portion that I... (3 Replies)
Discussion started by: ratzlaff
3 Replies

8. Shell Programming and Scripting

Incrementing a variable is not happening

Hi All, Iam trying to increment a variable Following is the code #!/usr/bin/ksh i=1; i='expr $i+1'; echo $i; Output: expr $i+1 not able to understand why its happening in that way i was expecting result as 2... if the above method is worng .. can you help how i can get... (3 Replies)
Discussion started by: kiranlalka
3 Replies

9. Programming

Interesting Problem About Incrementing ++

Here is my code: int startingPort = 200; string tempPort; stringstream out; out<<startingPort; tempPort = out.str(); //tempPort carries startingPort in string format //convert tempPort to *char - currentPort going to be passed into getaddrinfo() char currentPort;... (10 Replies)
Discussion started by: f.ben.isaac
10 Replies

10. Linux

Incrementing the date stored in the variable

Hi all, I have a variable with date as 20080831 . Now I want to increment it as 20080901 and so on.Is there any command for this. Please help me. thanks rameez (1 Reply)
Discussion started by: rameezrajas
1 Replies
Login or Register to Ask a Question