🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #8792

Re: suma en bash

#8792
From: J.A. Gutierrez
Date: Tue, 08 Oct 2002 12:40
91 lines
3001 bytes
Esteban Navarro <scandisk@able.es> wrote:


	hmmm
	a similar pregunta respondia yo mismo el 5 de mayo de 2000
	a un tal Esteban <scandisk@ctv.es>

	tambien mande un mensaje sobre el tema, curiosamente tambien
	respondiendo a <scandisk@ctv.es>, el dia 2 de junio de 2000


: Pacolinx expuso:

:> Esteban Navarro wrote:
:> 
:>> Estoy intentando crear un script en bash que me sume los numeros de un
:>> fichero: ej.
:>> 
:>> 12,67
:>> 14,78
:> 
:> Si,
:> 
:> fich=/elfichero.txt
:> suma=0
:> for linea in $(cat $fich); do

	se esta utilizando innecesariamente un subshell y un cat
	Mucho mejor algo tipo

		while read linea
		do
			lo que sea $linea
		done < $fich

:>         suma=$suma+$linea
:> done

	para que eso funcione tienen que darse varias condiciones:

	- shell posix/ksh/bash (no sh)
	- la variable "sum" debe ser numerica (typeset -i) y se debe usar "let"

: Ok. Solo me ha faltado al final un: 

: echo $suma | bc -l 

	no deja de ser un metodo bastante poco elegante...


---------------------------------------------------------------------------
* awk

$ (echo 2; echo 3) | awk '{i+=$0}END{print i}'
5

* dc

$ (echo 2; echo 3) |\
(echo 0 ; while read line; do echo $line +; done; echo p )| dc
5

* posix sh

$ (echo 2; echo 3) |\ 
(let a=0; while read line; do let a=$a+$line; done; echo $a)
5

* sh + expr

$ (echo 2; echo 3) |\ 
(a=0; while read line; do a=`expr $a + $line`; done; echo $a)
5

* :-)

$ (echo 2; echo 3) |\
(
while read line
do
        dd if=/dev/zero bs=$line count=1 2>/dev/null
done
) | wc -c
5

---------------------------------------------------------------------------
-- 
PGP and other useless info at      \
http://www.cps.unizar.es/~spd/      \
finger://daphne.cps.unizar.es/spd    \       Timeo Danaos et dona ferentes
ftp://ivo.cps.unizar.es/pub/          \                         (Virgilio)   
Technical Details
Message-ID: <anujns$8i0$1@news.unizar.es>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!nntp-relay.ihug.net!ihug.co.nz!news-out.newsfeeds.com!propagator2-maxim!news-in.spamkiller.net!news.telia-iberia.com!news.rediris.es!news.unizar.es!not-for-mail
References: <mg9tna.81a.ln@127.0.0.1> <t42a7-h2n.ln1@pacohost.aldariasnet.es> <b69una.if.ln@127.0.0.1>

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII-7-bit
X-Google-Thread: f86c8,56cc30fda95ab037
X-Google-Attributes: gidf86c8,public
X-Google-ArrivalTime: 2002-10-08 05:09:10 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeed.berkeley.edu!news-hog.berkeley.edu!ucberkeley!nntp-relay.ihug.net!ihug.co.nz!news-out.newsfeeds.com!propagator2-maxim!news-in.spamkiller.net!news.telia-iberia.com!news.rediris.es!news.unizar.es!not-for-mail
From: "J.A. Gutierrez" <spd@daphne.cps.unizar.es>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: suma en bash
Date: 8 Oct 2002 12:40:28 GMT
Organization: Rank
Lines: 91
Message-ID: <anujns$8i0$1@news.unizar.es>
References: <mg9tna.81a.ln@127.0.0.1> <t42a7-h2n.ln1@pacohost.aldariasnet.es> <b69una.if.ln@127.0.0.1>
NNTP-Posting-Host: daphne.cps.unizar.es
X-Trace: news.unizar.es 1034080828 8768 155.210.152.20 (8 Oct 2002 12:40:28 GMT)
X-Complaints-To: usenet@news.unizar.es
NNTP-Posting-Date: 8 Oct 2002 12:40:28 GMT
User-Agent: tin/1.4.5-20010409 ("One More Nightmare") (UNIX) (IRIX/6.2 (IP22))