Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-20-2003, 05:39 AM   #1
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

declare -a num
echo -n " input 10 scores : "
read -a num
for ((i=0; i<=10; i++))
echo " ${num[i]} " >> temp1
echo -n " Ordered Scores : "
sort -n -r temp1 | xargs -l1
read -a num
for ((i=0; i<=10; i++))
sum=`expr $sum + ${num[i]}`
echo $sum
Old 10-20-2003, 07:50 AM   #2
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
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
yeah nhs is right . I even checked your program by removing
read -a num
[SaTaN@mirage linux_org]$ 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
So I suppose you should remove those equal to signs in
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
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...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:39 PM.

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