You are not logged in.

#1 2009-02-12 23:37:43

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

[Solved] Quick Bash Question

Ok, I have a loop and I want to count the number of times the loop occurs in my script. Every time the loop occurs there is an output. I want during the loop to display 1 - infinite number until the loop stops.
Do you know what I am getting at?

Last edited by haxit (2009-02-13 18:04:37)


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#2 2009-02-12 23:58:50

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: [Solved] Quick Bash Question

Is this what you mean?

i=0; while true; do let i=$i+1; echo $i; done

archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

#3 2009-02-12 23:59:44

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Quick Bash Question

Like this?

i=0
while [ $i -lt 10 ]; do
echo $i
i=$(($i + 1))
done

Offline

#4 2009-02-13 00:11:01

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: [Solved] Quick Bash Question

Thanks


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#5 2009-02-13 00:14:17

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: [Solved] Quick Bash Question

Ok, heres another one. I get a list of number in the output, three to be exact. How can I add them all together and divide them by three?


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#6 2009-02-13 00:41:36

kludge
Member
Registered: 2008-08-03
Posts: 294

Re: [Solved] Quick Bash Question

haxit wrote:

Ok, heres another one. I get a list of number in the output, three to be exact. How can I add them all together and divide them by three?

can you provide a more specific example?


[23:00:16]    dr_kludge | i want to invent an olfactory human-computer interface, integrate it into the web standards, then produce my own forked browser.
[23:00:32]    dr_kludge | can you guess what i'd call it?
[23:01:16]    dr_kludge | nosilla.
[23:01:32]    dr_kludge | i really should be going to bed.  i'm giggling madly about that.

Offline

#7 2009-02-13 00:49:02

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Quick Bash Question

Like this?

i=0
sum=0
amt=0
while [ $i -lt 10 ]; do
if [ $(( $i % 2 )) == 0 ]; then
echo $i
sum=$sum+$i
amt=$(($amt+1))
fi
i=$(($i+1))
done
echo avg = \($sum\)/$amt = $(( ($sum)/$amt ))

Offline

#8 2009-02-13 00:51:04

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: [Solved] Quick Bash Question

list="1 2 3"
count=0
for number in list; do
  count=$(($count + 1))
  sum=$(($sum + $number))
fi
# Don't know if bash can do division all that well
awk "BEGIN { print $sum / $count }"

Offline

#9 2009-02-13 17:28:14

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: [Solved] Quick Bash Question

Ok, so I have this script:

#!/bin/bash
if [ -z "$1" ]; then
    echo "Please run like so: 'sh "$0" <highest number on dice>'"
else
    WOW=`echo "$1*2" | bc`
    for X in 1 2 3; do
    ONE=`seq 1 $1 | sort -R | head -1`
    TWO=`seq 1 $1 | sort -R | head -1`
    HAH=`echo "$ONE+$TWO" | bc`
    until [ $HAH -eq $WOW ]; do
        ONE=`seq 1 $1 | sort -R | head -1`
        TWO=`seq 1 $1 | sort -R | head -1`
        HAH=`echo "$ONE+$TWO" | bc`
        NUM=`echo "1"`
        echo "$HAH" >> probability.log
    done
    LOL=`cat probability.log | wc -l`
    echo "$LOL"
    rm probability.log
    done
fi

It gives me three outputs, I want to add those outputs and divide them by 3.


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#10 2009-02-13 17:50:38

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [Solved] Quick Bash Question

sum=$(( $1 + $2 + $3 ))
answer=$(echo "${sum}/3" | bc)
echo $answer

EDIT: duh...

echo "($1+$2+$3)/3" | bc

dunno why i broke it up originally.

Last edited by brisbin33 (2009-02-13 17:53:09)

Offline

#11 2009-02-13 17:51:10

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: [Solved] Quick Bash Question

brisbin33 wrote:
sum=$(( $1 + $2 + $3 ))
answer=$(echo "${sum}/3" | bc)
echo $answer

Wow, why didn't I think of that sad
Thanks!


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

#12 2009-02-13 18:05:12

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [Solved] Quick Bash Question

Pipe it to

awk 'BEGIN {s=0;c=0} {s=s+$1; c=c+1} END {print s/c}'

Offline

#13 2009-02-14 07:15:20

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: [Solved] Quick Bash Question

I don't get what this does. :\


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#14 2009-02-14 07:39:47

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: [Solved] Quick Bash Question

This sounds like someones homework...

Offline

#15 2009-02-14 17:03:44

haxit
Member
From: /home/haxit
Registered: 2008-03-04
Posts: 1,247
Website

Re: [Solved] Quick Bash Question

fumbles wrote:

This sounds like someones homework...

Nah! Dude I am only in grade 11 and not taking any computer courses. This is something i was discussing with a friend and I said Ill do it on the weekend. Check it out here: http://web.ncf.ca/ey723/scripts/probability.sh


Archi686 User | Old Screenshots | Old .Configs
Vi veri universum vivus vici.

Offline

Board footer

Powered by FluxBB