Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Need help in 2 scripts in bash (http://www.programmingforums.org/showthread.php?t=3832)

gawi May 10th, 2005 3:25 AM

Need help in 2 scripts in bash
 
Hi! I need your help.

1)
How to make a script which writes its argument in the other way round. I mean if arguments are "2 3 4 5" the script writes "5 4 3 2"

2)
How to make a script which reads a file which consinst of numbers (one number in one line) and writes the sum of those numbers.

E.G. the content of file:
------
3
7
6
4
------

in this case the script should write: 20

Please help me

Seamus May 10th, 2005 1:11 PM

for the first will
:

echo "$9 $8 $7 $6 $5 $4 $3 $2 $1"
do?

Second:
:

#! /bin/bash
a=`cat file | perl -nle "print for m/^[0-9]*/g"`
b=`echo $a | tr ' ' '+'`
((c=$b))
echo $c


gawi May 11th, 2005 10:39 AM

For the first script:

:

echo "$9 $8 $7 $6 $5 $4 $3 $2 $1"
This is not universal and too simple way. I need a script which writes its arguments in the other way round but i don't know how many will be those arguments. That way you wrote me works only with 1 to 9 arguments.

For the second: This is what i looked for :) thanx a lot. I didn't know that commend:

:

((=$b))

I even wrote simpler way (also working)

:


a=`cat file`
b=`echo -n $a | tr ' ' '+'`
((c=$b))
echo $c


THANX a lot and if you could help me with the first script

:)

Seamus May 11th, 2005 12:59 PM

yea I know what you mean.
You'll have to use shift somewhere then.

Pizentios May 11th, 2005 1:42 PM

for the first one, you could probally use a loop of some kind to keep looping, till you get to the end.

gawi May 11th, 2005 3:12 PM

yeah i know but i tried with for and while but no idea :(

levk Jul 18th, 2005 12:12 PM

re: reverse
 
for the first one...

I din't know, but is there a way to take the first argument and the rest of arguments? that way you can do it recursively:

:

if [ -n $1 ]
else
 ./reverse # after $1
 echo $1
fi



All times are GMT -5. The time now is 4:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC