Help with this code please.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with this code please.
# 1  
Old 04-18-2011
Question Help with this code please.

Hello,
I haven't used this nano editor very often so i'm not very familiar with it.
So I've done this code for FCFS or FIFO algorithm in another language and tried to "Translate it into the nano editor.
Thing is I don't really know what's wrong with my "translation right now" so help would be much appreciated . cheers.

Here's the code:
Code:
#!/bin/bash

a[i]=0; b[i]=0; g[i]=0; w[i]=0;
echo "Enter Number of Processes:"
read n

echo "Enter Burst Time:"
for (( i=0; i<n; i++ ))
do
read b[i]
done

echo "Enter Arrival Time:"
for (( i=0; i<n; i++ ))
do
read a[i]
done

g[0]=0
for (( i=0; i<n; i++ ))
do
w[i]=g-a[i]
t[i]=g+1-a[i]
awt=awt+w[i]
at=at+t[i]
done

awt=awt/n
at=at/n

echo " Waiting time:"
for (( i=0; i<n; i++ ))
do
echo $w[i]
done

echo "Turnaround time:"
for (( i=0; i<n; i++ ))
do
echo $t[i]
done

echo " The average waiting time is ",$awt
echo " The average turnaround time is ",$at

# 2  
Old 04-18-2011
Quote:
Originally Posted by imo
Hello,
I haven't used this nano editor very often so i'm not very familiar with it.
So I've done this code for FCFS or FIFO algorithm in another language and tried to "Translate it into the nano editor.
You're not "programming in nano language", you're programming BASH. You can edit C, shell, c-shell, and many other kinds of code in nano.

Code:
#!/bin/bash

# what is i supposed to be here?  zero?
i=0
a[$i]=0; b[$i]=0; g[$i]=0; w[$i]=0;
echo "Enter Number of Processes:"
read n

echo "Enter Burst Time:"
for (( i=0; i<n; i++ ))
do
        read b[$i]
done

echo "Enter Arrival Time:"
for (( i=0; i<n; i++ ))
do
        read a[$i]
done

Mostly, so far so good...
Code:
g[0]=0
for (( i=0; i<n; i++ ))
do
        # Expressions don't work that way in BASH.
        #w[i]=g-a[i]
        #t[i]=g+1-a[i]
        #awt=awt+w[i]
        #at=at+t[i]
        # Is this a[1] supposed to be a[i] ?
        ((w[i]=g+1-a[1]))
        ((t[i]=g+1-a[i]))
        ((awt=awt+w[i]))
        ((at=at+t[i]))
done

# Beware that BASH only supports integers!
# You won't get decimal points here.
((awt=awt/n))
((at=at/n))

# You don't need to loop through an array to print it.
# echo " Waiting time:"
#for (( i=0; i<n; i++ ))
#do
#        echo $w[i]
#done
echo "Waiting time:  ${w[*]}"

#echo "Turnaround time:"
#for (( i=0; i<n; i++ ))
#do
#echo $t[i]
#done

echo "Turnaround time: ${t[*]}"

# You don't need , to separate arguments in shell.
# you can put arguments right inside strings, too.
#echo " The average waiting time is ",$awt
#echo " The average turnaround time is ",$at
echo "The average waiting time is $awt"
echo "The average turnaround time is $at"


Last edited by Corona688; 04-18-2011 at 07:48 PM..
# 3  
Old 04-18-2011
PHP

I've done all what you said and still no luck .. get syntax errors that aren't very specific like unexpected end of line ...

Here's my current code again
Code:
#!/bin/bash
i=0
a[$i]=0; b[$i]=0; g[$i]=0; w[$i]=0;

echo "Enter Number of Processes:"
read n

echo "Enter Burst Time:"
for (( i=0; i<n; i++ ))
do
read b[$i]
done

echo "Enter Arrival Time:"
for (( i=0; i<n; i++ ))
do
read a[$i]
done

g[0]=0
for (( i=0; i<n; i++ ))
do
((w[i]=g[i]-a[i]))
((t[i]=g[i+1]-a[i]))
((awt=awt+w[i]))
((at=at+t[i]))
done

((awt=awt/n))
((at=at/n))
echo " The waiting time is ${w[*]}"
echo " The turnaround time is ${t[*]}"
echo " The Average waiting time is $awt"
echo " The Average Turnaround time is $at"

---------- Post updated at 12:30 AM ---------- Previous update was at 12:29 AM ----------

Thank you for the help anyway, much appreciated .

---------- Post updated at 03:31 AM ---------- Previous update was at 12:30 AM ----------

Still can't figure out what's wrong and I think it's to do with the "g[i+1] -a[i]"
I just get silly results when it shows it...

Last edited by imo; 04-18-2011 at 11:30 PM..
# 4  
Old 04-19-2011
I didn't get any syntax error with the code you have pasted. May be you can paste the error you are getting.
Also, check your formula for awt and at. When it enters the calcaulation loop for the first time, g[i], g[i+1] is zero/null.

regards,
Ahamed
# 5  
Old 04-19-2011
Yeah I get no syntax error now ... but when it displays the result they are just not right...
The formula is the same as the one i used in the other language... but this one just doesn't want to work properly :/
not sure what i missed out.
# 6  
Old 04-20-2011
Quote:
Originally Posted by imo
Yeah I get no syntax error now ... but when it displays the result they are just not right...
I warned you that BASH doesn't support floating point numbers... You never get decimal points. Does your math use, or result in, numbers with decimal points?
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Linux

Go to a line of code , skip few lines of code

Hi , I have a code where i am using a infinite while loop . some thing like below while do if then #go to line 20 fi command 1; command 2; #line 20: sleep 34; (5 Replies)
Discussion started by: Paarth
5 Replies

2. Shell Programming and Scripting

Block of code replacement in Java source code through Unix script

Hi, I want to remove the following code from Source files (or replace the code with empty.) from all the source files in given directory. finally { if (null != hibernateSession && hibernateSession.isOpen()) { //hibernateSession.close(); } } It would be great if the script has... (2 Replies)
Discussion started by: hareeshram
2 Replies

3. UNIX for Dummies Questions & Answers

If ‘922’ Code does not exist on ‘03’ Record, ‘901’ Code will be there instead, move ‘03’ R

01,011600033,011600033,110516,0834,2,90,,2/ 02,011600033,011103093,1,110317,0834,,2/ 03,105581,,015,+00000416418,,,901,+00000000148,,,922,+000000 00354,,/ 03,113806,,015,+00000559618,,,901,+00000000096,,,922,+000000 00621,,/ 88,902,+0000000025218,,/... (1 Reply)
Discussion started by: sgoud
1 Replies

4. Shell Programming and Scripting

translate ksh code to csh code

hi all, Can any 1 help me translate this korn shell code to C shell code : email=$(grep "^$1" $folder/config_2.txt | awk '{print $2'}) In config_2.txt the content is : which mean in korn shell , $1=groupname and $2=email address. Now i need to write in C shell script,when i set the... (2 Replies)
Discussion started by: proghack
2 Replies

5. Programming

how i prepare a c++ code(c code) for implementing my own protocol format

helo my protocol format is given below { destno,mode,no.of packet,pktsize,,pktno,textsize,CRC} description:- { is starting flag destno - 4bytes mode - 1 byte no.of pkt - 4byes pktsize - 6 bytes ... (1 Reply)
Discussion started by: amitpansuria
1 Replies

6. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question