LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-20-2003, 05:39 AM   #1
pazvant
Member
 
Registered: Jul 2003
Location: Istanbul
Distribution: slack
Posts: 43

Rep: Reputation: 15
Question About bash scripting


Here is the code for given at least 10 scores and ascending them, then making a sum of inputed values..In expr part i got 'syntax error' İ did not solve the problemm...Can anyone help me


#!/bin/bash
declare -a num
echo -n " input 10 scores : "
read -a num
for ((i=0; i<=10; i++))
do
echo " ${num[i]} " >> temp1
done
echo -n " Ordered Scores : "
sort -n -r temp1 | xargs -l1
sum=0
read -a num
for ((i=0; i<=10; i++))
do
sum=`expr $sum + ${num[i]}`
done
echo $sum
 
Old 10-20-2003, 07:50 AM   #2
nhs
Member
 
Registered: Aug 2003
Location: Edinburgh, Scotland
Distribution: Gentoo
Posts: 246

Rep: Reputation: 30
Firstly, your script asks for 12 scores (0 to 10 inclusive is 11 and you ask for a 12th after the sum=0 line). You then read the first 11 entries of which only 10 will be numeric so the eleventh will generate an error. (I think, I cannot experiment at the moment).
 
Old 10-20-2003, 09:32 AM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
yeah nhs is right . I even checked your program by removing
Code:
read -a num
Then
Quote:
[SaTaN@mirage linux_org]$ sh bash.sh
input 10 scores : 1 2 3 4 5 6 7 8 9 10 11
Ordered Scores : 11 10 9 8 7 6 5 4 3 2 1
66
So I suppose you should remove those equal to signs in
Code:
for ((i=0; i<=10; i++))
This is if you want to accept only 10 numbers

And your code doesn't work for the next time because you are using >> which means append and so the 11 numbers which are
entered now will get appended to the previous 11 numbers and so on.
Don't use temp1 , if you really want to use it add a statement
rm -f temp1
at the end of the your code.

And your order is not ascending it is descending

Welcome to LQ

Last edited by SaTaN; 10-20-2003 at 09:34 AM.
 
Old 10-20-2003, 11:12 AM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Instead of mailing me, you could have posted it right here.

One way of getting rid of temp is to write your sort.
i.e, here all that you need to do is sort an array of "n" elements
which is quite easy.
Use two for loops and it is done...
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting help (su ...) shwong Linux - General 1 11-02-2005 12:26 PM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 04:07 AM
need help with bash scripting rich2oo1 Programming 2 12-17-2003 12:50 PM
HELP with BASH scripting atwah Linux - Newbie 6 09-09-2003 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration