1. The problem statement, all variables and given/known data:
Making a small GCD calculator. It's fully written, but i'm getting an error that i can't trace.
2. Relevant commands, code, scripts, algorithms:
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.
Code:
#! /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
3. The attempts at a solution (include all code and scripts):
tried set -x and various echo commands to find the problem
[b]4.
AACC, Arnold Maryland, USA, Brandon Brisbon, CSI-135-401:I tried to post the url to the class, but it said i need to post 5 times before i could (sorry)
Adding to the above corrections, use "-lt" instead of "<".
Also the script you wrote will be in an infinite loop. There will be no changes to $a and $b since $drop is always equal to zero. Restructuring yours to work would be impossible, hence i wrote a new one::
Code:
if [ $# -lt 2 ]
then
echo "Wrong usage"
exit -1
elif [ $1 -le 0 -o $2 -le 0 ]
then
echo "Invalid args"
exit -1
fi
#get the smallest of 2 args and store it in $div
a=$1
b=$2
if [ $a -lt $b ]
then
div=$a
elif [ $b -lt $a ]
then
div=$b
else
div=$a
fi
#loop from $div to 1 till $div divides both $a and $b completly
while [ $div -gt 0 ]
do
remA=`expr $a % $div`
remB=`expr $b % $div`
if [ $remA -eq 0 -a $remB -eq 0 ]
then
echo "GCD of $a and $b is $div"
exit 0
fi
div=`expr $div - 1`
done
echo "GCD of $a and $b is 1"
exit 0
Hi 2 all,
i have had AIX 7.2
:/# /usr/IBMAHS/bin/apachectl -v
Server version: Apache/2.4.12 (Unix)
Server built: May 25 2015 04:58:27
:/#:/# /usr/IBMAHS/bin/apachectl -M
Loaded Modules:
core_module (static)
so_module (static)
http_module (static)
mpm_worker_module (static)
... (3 Replies)
Hello.
System : opensuse leap 42.3
I have a bash script that build a text file.
I would like the last command doing :
print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt
where :
print_cmd ::= some printing... (1 Reply)
How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address
and column 3 contains “cc” e-mail address to include with same email.
Sample input file, email.txt
Below is an sample code where... (2 Replies)
logs:
"/home/abc/public_html/index.php"
"/home/abc/public_html/index.php"
"/home/xyz/public_html/index.php"
"/home/xyz/public_html/index.php"
"/home/xyz/public_html/index.php"
how to use "cut" or "awk" or "sed" to get the following result:
abc
abc
xyz
xyz
xyz (8 Replies)
I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this:
$ look "string" "/home/patrick/filename.txt"
However, this gives me the following message:
"look: /home/patrick/filename.txt: File too large"
So, I have two... (14 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
Hi Friends,
Can any of you explain me about the below line of code?
mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`
Im not able to understand, what exactly it is doing :confused:
Any help would be useful for me.
Lokesha (4 Replies)