Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 10th, 2005, 3:25 AM   #1
gawi
Newbie
 
Join Date: May 2005
Posts: 3
Rep Power: 0 gawi is on a distinguished road
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
gawi is offline   Reply With Quote
Old May 10th, 2005, 1:11 PM   #2
Seamus
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Seamus is on a distinguished road
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

Last edited by Seamus; May 10th, 2005 at 1:28 PM. Reason: Forgot code blocks
Seamus is offline   Reply With Quote
Old May 11th, 2005, 10:39 AM   #3
gawi
Newbie
 
Join Date: May 2005
Posts: 3
Rep Power: 0 gawi is on a distinguished road
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

gawi is offline   Reply With Quote
Old May 11th, 2005, 12:59 PM   #4
Seamus
Newbie
 
Join Date: May 2005
Posts: 5
Rep Power: 0 Seamus is on a distinguished road
yea I know what you mean.
You'll have to use shift somewhere then.
__________________
Check out Atlantic Apartments, my blog, or my latest site
Seamus is offline   Reply With Quote
Old May 11th, 2005, 1:42 PM   #5
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
for the first one, you could probally use a loop of some kind to keep looping, till you get to the end.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old May 11th, 2005, 3:12 PM   #6
gawi
Newbie
 
Join Date: May 2005
Posts: 3
Rep Power: 0 gawi is on a distinguished road
yeah i know but i tried with for and while but no idea
gawi is offline   Reply With Quote
Old Jul 18th, 2005, 12:12 PM   #7
levk
Newbie
 
Join Date: Jul 2005
Posts: 7
Rep Power: 0 levk is on a distinguished road
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
levk is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:02 PM.

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