5

I've never done shell script before and now I'm running into a simple problem... I have a for loop which executes every time the run.sh script. To see how far the script has already run I want to print e.g. every 5000 the actual index.

$counter = 0
for ((  i = 0 ;  i <= 5000;  i++  ))do
    if ($i =  $counter); then
            echo "$counter"
            counter=$(counter+1000)
    fi
./run.sh
done

running this piece of code gives me the following error

./for_loop.sh: line 1: =: command not found
./for_loop.sh: line 3: 0: command not found

I have also tried to init the variable counter with

declare -i counter = 0

which gives me the following error

./for_loop.sh: line 1: declare: `=': not a valid identifier
3
  • 1
    In Bash, there are two assignment operators, = and let =, so you may assign the first counter with counter=0 or let counter=0. let is meant to be used only with numbers while = works with any string. Commented Aug 6, 2010 at 10:47
  • You need to read a tutorial on shell syntax, you have many basic syntax errors. Commented Feb 4, 2019 at 23:01
  • For instance, you don't put $ before the variable name when assigning, only when reading it. Commented Feb 4, 2019 at 23:01

3 Answers 3

7

You don't really need two counters. A single counter will suffice:

for (( counter = 0; counter <= 5000; counter++ ))
do
    if (( counter % 1000 == 0 ))
    then
            echo "$(( counter / 1000 ))"
    fi
    ./run.sh
done

This executes run.sh 5000 times and prints the counter value every 1000 iterations. Note that % is the modulus operator which computes remainder after division and / is the integer division operator.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your nice solution! the command echo "%(( counter / 1000 ))" prints actually %(( counter / 1000 )) and not the result of counter/1000. Do you know where the problem is?
Oops! %(( counter / 1000 )) should read $(( counter / 1000 )). Replace the % with a $. I corrected my answer.
oh of course... could have found it out by myself :) thx again
4

Line 1 should be: (No $, no extra spaces around '=')

counter=0

Line 3 should be: (Square brackets, '-eq' because '=' is for string equality)

if [ $i -eq $counter ]

Line 5 should be: (Double parentheses)

counter=$((counter+1000))

8 Comments

In the for-loop, there should also be a semicolon between )) and do, or do should be on the next line.
You are right, but for some reason the for (( .. ))do runs fine on my machine (bash 3.2.17).
What about for (( .. )) do, that is, with a space between the last closing parenthesis and do? Perhaps Bash accepts the closing parenthesis as a delimiter replacement for the ;.
That also works. I guess the for (()) do is easy enough to parse. But I agree it is bad style to omit the semicolon or newline here.
Line 3 could also be if ((i == counter)); then (just double the parentheses and the equal sign and you can omit or retain the dollar signs). Line 5 could be (( counter += 1000 )). Also, in line 1 there shouldn't be a space after the equal sign either.
|
1

In line 3 I believe you have mistaken assignment = for equality ==

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.