Unknown (probably) simple problem with my code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unknown (probably) simple problem with my code
# 1  
Old 10-11-2011
Java Unknown (probably) simple problem with my code

I'm relatively new to both UNIX and Linux and slightly less new at programming. But I can't figure out why this won't run properly.

It's meant to calculate the GCD of two numbers (simple enough, you'd think). And I designed it myself and it looks good to me and my instructor won't respond. If anyone could help me out, it would be appreciated.


#! /bin/bash

#Takes arguments to determin the GCD

errorNum="Too many or too few arguments passed."
errorUse="Usage: $0 [decimal] [decimal]"
errorNeg="Cannot use a negative number."

checkIfZero ()
{
if [ $a=0 ]
then
drop=1
elif [ $b=0 ]
then
drop=1
else
drop=0
fi
exit 0
}

if [ $1 < 1 -o $2 < 1 ]
then
echo $errorNeg
echo $errorUse
exit 0
fi

if [ $# -le 1 -o $# -gt 2 ]
then
echo $errorNum
echo $errorUse
exit 0
elif [ $1 -ge 0 ] && [ $2 -ge 0 ]
then
a=$1
b=$2
drop=0
while [ $a -gt 0 ] || [ $b -gt 0 ]
do
if [ $a -gt $b ]
then
if [ drop -gt 0 ]
then
checkIfZero
c=`expr $a % $b`
a=$c
fi
elif [ $b -gt $a ]
then
if [ drop -gt 0 ]
checkIfZero
c=`expr $b % $a`
b=$c
fi
fi
done

if [ $a -gt $b ]
then
echo "The GCD is $a."
else
echo "The GCD is $b."
fi
fi
# 2  
Old 10-11-2011
We have a special homework forum. Please post over there. I would move the thread but it the homework forum requires a special kind of post.

Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

2. Shell Programming and Scripting

simple wc problem

Hi there, Im sure there is a simple explanation for this but I have a file like this with no balnk lines peter paul john I run the command # var=`grep paul file.txt` # echo $var paul # echo $var | wc -l 1 but when I grep for a value that isnt in the file, i still... (4 Replies)
Discussion started by: rethink
4 Replies

3. Shell Programming and Scripting

Simple echo problem

Hey all! I'm in an intro to UNIX class at university, and we've just began writing scripts. Naturally I can't get it to do what I want. Basic script as follows: COMPARE1=`ls|wc -l` tar czf archive.tgz ~/path/to/file COMPARE2=`tar tvzf archive.tgz|wc -l` if then ... (7 Replies)
Discussion started by: nickzourdos
7 Replies

4. Homework & Coursework Questions

Problem with simple octal to decimal code

Hello all I started computer science and C++ programming only 6 weeks ago with no prior knowledge. It's a great language, the only other one apart from English that I hope to speak fluently quite soon. I look forward to participating further on these forums, seeking answers and looking at previous... (3 Replies)
Discussion started by: qf_woodfox
3 Replies

5. Shell Programming and Scripting

Please help me with a simple problem

Hi, I have a very simple script like below: for n in 10 20 30 do for a in 30 40 50 60 70 80 do for r in 2 3 4 5 6 7 do m=$((r*a)) count=1 while do echo "a = " $a ", m = " $m ", n = " $n ... (2 Replies)
Discussion started by: Dark2Bright
2 Replies

6. Shell Programming and Scripting

simple awk problem

pcn linus> ntpq -p remote refid st t when poll reach delay offset disp ============================================================================== +smpnn01 ntpsrv1 2 u 829 1024 377 1.46 0.793 0.85 *smpnn02 ntpsrv1 2 u ... (2 Replies)
Discussion started by: arch12
2 Replies

7. Shell Programming and Scripting

Unknown Problem. I really want your help to solve this!

Take a look on this code: #!/bin/sh currentpath=`pwd` if ; then #Normal user if ; then "$currentpath"/.cleaner else ./runit fi else #Root user if ; then rm -r /some fi mkdir /some cd /home/ echo "`ls --group-directories-first -1`" > /some/allusers cat /some/allusers | sed 's/... (17 Replies)
Discussion started by: hakermania
17 Replies

8. Shell Programming and Scripting

Unknown problem-beginner

Hi, I have a problem running my script.Here it is: num1="1" num2="2" num3="3" num4="4" num5="5" num6="6" num7="7" echo echo "Type what you wanna do..." echo "1= Start interface 2= Stop interface 3= See your mac" echo "4= Change your mac 5= Hack WEP...... 6= Hack WPA...." echo... (3 Replies)
Discussion started by: hakermania
3 Replies

9. Solaris

PING - Unknown host 127.0.0.1, Unknown host localhost - Solaris 10

Hello, I have a problem - I created a chrooted jail for one user. When I'm logged in as root, everything work fine, but when I'm logged in as a chrooted user - I have many problems: 1. When I execute the command ping, I get weird results: bash-3.00$ usr/sbin/ping localhost ... (4 Replies)
Discussion started by: Przemek
4 Replies

10. Shell Programming and Scripting

simple date problem

i have a script that grep for today date a=`date +"%F"`--------greps current/today date wat if suppose i want to grep a date for yesterday... how to do that using the above format: i,e 2008-01-20 (4 Replies)
Discussion started by: ali560045
4 Replies
Login or Register to Ask a Question