4 Replies - 803 Views - Last Post: 22 March 2015 - 12:07 AM Rate Topic: -----

#1 jaav89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 15

Lost in coding.. For loop in Bash

Posted 21 March 2015 - 09:14 PM

Hi all! New to the forums! I decided to make an account after countless horus of researching online in the attemp to fix the errors in my code! I've foudn a lot of help here in the past and I'm excited to be a part of the forums!

I'm trying to get a shell script in Linux and use a loop from 1 to 100, then search the results from (1 to 100 mod 5) + 2.
And lastly when the loop ends I want to display the average for all previous results and attach them to a new txt file.

This is what I've been working with and I receive 5 errors

#!/bin/sh
for (( k = 1; k <= 100; k++ ))
do
remainder =$ ((k%5)+2)
echo k + "%5=" + remainder>>file.txt 
sh
int sum = 0
for(( k = 1; k <= 100; k+))
do
sum = sum + $ ((k%5)+2)
average = $ ((sum/100))
echo average
echo average >>file.txt
done 


I've tried running bash file.sh and sh file.sh nothing works.

Errors: shell.sh: 2: shell.sh: Syntax error: Bad for loop variable

Is This A Good Question/Topic? 0
  • +

Replies To: Lost in coding.. For loop in Bash

#2 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Lost in coding.. For loop in Bash

Posted 21 March 2015 - 09:36 PM

Welcome to DIC!

I have to admit, I'm not much of a bash expert, but when I tried running your script I got an error on line 4, not on line 2:

./file.sh: line 4: syntax error near unexpected token `('


I suppose we can puzzle that one out with a little work, but first let's make sure we're looking at the same code. Is the above exactly the same as what you've got in file.sh?
Was This Post Helpful? 0
  • +
  • -

#3 jaav89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 15

Re: Lost in coding.. For loop in Bash

Posted 21 March 2015 - 09:40 PM

Yes, that is one of the few errors I've gotten :/
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6559
  • View blog
  • Posts: 30,699
  • Joined: 10-May 07

Re: Lost in coding.. For loop in Bash

Posted 21 March 2015 - 09:55 PM

You have 2 'do' statements & only one 'done'?
Did you post everything?

for value in $values, not using parens.

Your assignments need to be tight. Meaning you can't have spaces.

variable=value

Lastly, I can't say that I've ever seen someone indicate variable type. That's not to say I'm sure it will throw an error, but I never use it.

Where did you get this code? Are you sure it's bash & not perl?
Was This Post Helpful? 0
  • +
  • -

#5 ndc85430  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 611
  • View blog
  • Posts: 2,583
  • Joined: 13-June 14

Re: Lost in coding.. For loop in Bash

Posted 22 March 2015 - 12:07 AM

That code seems to be a mixture of Bash and other things. Usually, for loops in bash look like, e.g.

for variable in sequence
do
   # Statements here
done



So for generating the numbers from 1 to 10, you could use

for i in $(seq 1 10)


This runs the command seq and assigns each value to i in the loop.

Also when getting the contents of a variable, you prefix it with a $.

There are other things wrong with the code. I'd suggest following a tutorial on Bash, like this one.

This post has been edited by ndc85430: 22 March 2015 - 12:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1