Facing problem in incrementing the variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Facing problem in incrementing the variable
# 8  
Old 05-20-2010
Quote:
Originally Posted by av_vinay
i=$[$i + 1];
#i=`expr $i +1`;
#i=`bc $i + 1`
#i=`($i+1 | bc)`
Those are all wrong.

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

`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.


Quote:
Originally Posted by vgersh99
(ksh/bash specific)
Code:
i=$(($i+1))

Actually, that arithmetic expansion is posix-compliant.

Regards,
Alister

Last edited by alister; 05-20-2010 at 12:01 PM..
# 9  
Old 05-21-2010
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:

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.,

Last edited by pludi; 05-21-2010 at 03:13 AM.. Reason: code tags, please...
# 10  
Old 05-21-2010
Quote:
Code:
#!/bin/sh

i=0
VAR=10

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

The above code can be significantly simplified
Code:
i=0
VAR=10

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

Works with bash and ksh93.
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