![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
for the first will
echo "$9 $8 $7 $6 $5 $4 $3 $2 $1" 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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 3
Rep Power: 0
![]() |
For the first script:
echo "$9 $8 $7 $6 $5 $4 $3 $2 $1" 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 ![]() |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2005
Posts: 5
Rep Power: 0
![]() |
yea I know what you mean.
You'll have to use shift somewhere then. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() |
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! |
|
|
|
|
|
#6 |
|
Newbie
Join Date: May 2005
Posts: 3
Rep Power: 0
![]() |
yeah i know but i tried with for and while but no idea
![]() |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jul 2005
Posts: 7
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|