Loop Counting Question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Loop Counting Question
# 1  
Old 12-20-2012
Loop Counting Question

I figured this out, but I wanted to pose the question.

I was writing a while loop that required counting the amount of times the job had run, for example.

An example below is what I had:

Code:
variable=0

while :
do
    variable = `expr $variable + 1`
done

That didn't do what I needed it to (allow multiple attempts of input). However the following did:

Code:
variable=0

while :
do
    (( variable = variable + 1 ))
done

I was just curious why the double parenthesis handled that while the expr statement didn't. I'm still learning Smilie
# 2  
Old 12-20-2012
Remove the blank space in between and re-try:-
Code:
variable=`expr $variable + 1`

Or
Code:
variable=$( expr $variable + 1 )

# 3  
Old 12-20-2012
Ahh ok cool - thank you. That did it. The guy I'm learning from at work does it in double parenthesis. Is there any reason to do it one way or the other?
# 4  
Old 12-20-2012
Double parentheses are used for arithmetic operations and they enable you to omit the dollar signs on integer and array variables and include spaces around operators for readability.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-20-2012
I appreciate it. Makes sense to me Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk counting question

Probably a simple to this, but unsure how to do it. I would prefer an AWK solution. Below is the data set. 1 2 3 2 5 7 4 6 9 1 5 4 8 5 7 1 1 10 15 3 12 3 7 9 9 8 10 4 5 2 9 1 10 4 7 9 7 12 6 9 13 8 For the second... (11 Replies)
Discussion started by: mollydog11
11 Replies

2. Shell Programming and Scripting

Quick Question: counting columns?

Hi everyone I have a quick question... I have an ascii file that looks like the one below, and I wanted to find a way to make a listing of how many columns there is in each row, similar to the example below. Anyone has any ideas of how I can do this? Thanks!! 6 Sep 2008 -158.535 33.6617... (2 Replies)
Discussion started by: lucshi09
2 Replies

3. Shell Programming and Scripting

For loop question

I have two files. In file one, there are many columns, but only two of interest to me. Column 1 contains a list of individuals, defined by an ID number. Column 10 contains the diagnosis that each individual has (I am a physician). All together, there are 3000 lines in this file, one line per... (2 Replies)
Discussion started by: awc228
2 Replies

4. Shell Programming and Scripting

For loop question

Hi, I'm trying to put together a small script that will read a txt file that contains a list of two columns. Each column is the name of a folder.. e.g. AIX Server1 AIX Server2 AIX Server3 $ for i in `cat /opt/apacheprod/scripts/input/copy_list.txt` do PLATFORMVAR=`awk ' { print $1 } '... (7 Replies)
Discussion started by: Jazmania
7 Replies

5. Shell Programming and Scripting

For Loop Question

I am struggling with the for loop. I have a file name heros.txt and I would like to go through a list in file where.txt and see if I can find the name from where inside heros. One of the problems that I am having is I dont understand how to setup the for loop to find the list to search.:wall: ... (6 Replies)
Discussion started by: captaindoogles
6 Replies

6. Programming

trouble with loop counting

HI there, I am trying to count manually what this code does but I am stuck and I don't learly see the result. The code works and it compiles and runs but I just don't follow the value of var. #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<unistd.h> #include<wait.h>... (2 Replies)
Discussion started by: bluetxxth
2 Replies

7. Shell Programming and Scripting

For Loop Question

I'm improving the way an existing script handles arrays, but the results aren't what I had in mind: e="Too many consecutive errors... System is probably unstable!" e="Cancelable Timer Wait Failed!" for errcd in ${e} do echo ${errcd} done The for loop interprets the spaces... (2 Replies)
Discussion started by: ironhalo
2 Replies

8. Shell Programming and Scripting

BASH loop inside a loop question

Hi all Sorry for the basic question, but i am writing a shell script to get around a slightly flaky binary that ships with one of our servers. This particular utility randomly generates the correct information and could work first time or may work on the 12th or 100th attempt etc !.... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

counting the lines of diff files in loop

i have two file. i want to count the lines of each file one by one in loop and compare it. can any one pls help me on this? (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

10. Shell Programming and Scripting

while loop question

while do print What is the next device number to be added to $dgroup? print Press \<Enter\> if there are no more devices to be added. read dev_num export dev_num symld -g $dgroup -sid $sname add dev $dev_num done In this while... (2 Replies)
Discussion started by: stepnkev
2 Replies
Login or Register to Ask a Question