3

I am trying to write a code that will calculate the radius and area of a circle using pi and the circumference. When I try to run my code, I receive an error message that says

main.sh: line 5: bc: command not found
main.sh: line 7: bc: command not found

Could you please advise me on where I went wrong, and help me to fix my code?

The code:

#!/bin/bash
pi=3.14;
circumference=5;
echo "$circumference"
radius=$(echo "$circumference" \* 2 \* "$pi" |bc -l);
echo "$radius"
area=$(echo "$pi" \* "$radius" \* "$radius" |bc -l);
echo "$area"
echo "The radius of a circle is $radius";
echo "The area of a circle is $area";
exit
10
  • When I remove the bc -l, it just prints 5 *3.14 instead of doing the calculations – user445702 Dec 9 '20 at 18:30
  • 2
    So bc doesn't seem to be available for some peculiar reason. What Unix are you running? Note that this does not mean anything is wrong with your script, only that you system is lacking the bc command. – Kusalananda Dec 9 '20 at 18:32
  • 1
    Please edit your question and tell us i) what operating system (if Linux, which one) you are using and ii) show us the output of type -a bc. – terdon Dec 9 '20 at 18:40
  • I am running putty for my class and I am on the atlas server. – user445702 Dec 9 '20 at 18:42
  • It does not recognize bc as a command at all terdon – user445702 Dec 9 '20 at 18:42
2

The OP came up with a sensible script, and can't even get to test it because his course stuck him with a broken copy of Linux

it's probably not a broken copy of linux.

Be the class hero by doing the following

  1. download the program source bc-1.03.tar.gz from https://ftp.gnu.org/gnu/bc/
  2. upload that .tar.gz file to that server you are working on.
  3. tar -xf bc-1.03.tar.gz
  4. cd bc-1.03
  5. ./configure
  6. make
  7. a working executable of bc will then be available in this current directory.
  8. simply reference this bc location in your bash script, example below.
  9. open permissions on that file and folder and let your classmates also run it.

#!/bin/bash
pi=3.14;
circumference=5;
echo "$circumference"
radius=$(echo "$circumference" \* 2 \* "$pi" |/home/ron/bc-1.03/bc -l);
echo "$radius"
area=$(echo "$pi" \* "$radius" \* "$radius" |/home/ron/bc-1.03/bc -l);
echo "$area"
echo "The radius of a circle is $radius";
echo "The area of a circle is $area";
exit
1
  • 1
    it does not require doing make install. In fact, unless you are an administrator having root privilege then make install is probably not going to work for you trying to put it under /usr/local/bin.. What I described is all that needs to be done, and that 1.03 version works perfectly fine. I don't know the current version of bc out now... I typed this all up while eating my lunch I figured ftp.gnu.org was an acceptable provider of the bc program. – ron Dec 9 '20 at 19:49
1

I am running putty for my class and I am on the atlas server

at the prompt in putty, type which bc

it should respond with /usr/bin/bc

if it says not found, then it is likely the bc - An arbitrary precision calculator language program is not installed on the system you are working on.

if you do an rpm -qa | grep bc you should see something like bc-1.06.95-13.el7.x86_64.

There will likely be a few things reported, as it will pattern match many other things like libc and glibc and bcrypt and anything else simply having the letters b and c together.

If bc is installed, and should be found as /usr/bin/bc then consider putting the full path of it which is /usr/bin/bc in your bash script. There are security aspects of linux where the PATH environment variable is restricted when in a bash script, however /usr/bin definitely would not be blocked or left out so more than likely bc is not installed on that atlas server you are on.

There is nothing wrong with your posted code. Exactly as posted it runs just fine for me on RHEL 7, the output being

5
31.40
3095.914400
The radius of a circle is 31.40
The area of a circle is 3095.914400
3
  • fyi a helpful sanity check is doing echo $PATH in your script and observe what that environment variable is when used in the script. Then do echo $PATH at the terminal prompt, and see if there is a difference. If the one printed out from within the script is shorter then there's some security aspect being imposed. – ron Dec 9 '20 at 19:02
  • bc could be installed in a non standard location, which will be available at the prompt but within your bash script that non standard location isn't coming through for the PATH environment variable. So if bc is installed simply put the correct full path to it in your script to make it happen. – ron Dec 9 '20 at 19:05
  • rpm -q bc or rpm -qf /usr/bin/bc. Piping -qa thru grep is inefficient. – Warren Young Dec 12 '20 at 0:35
0

with your code, the radius turns out to be bigger than the circumference.

Try this:

#!/bin/bash
pi=3.14;
circumference=5;
echo "$circumference"
radius=$(echo "$circumference" / 2 / "$pi" |bc -l);
echo "$radius"
area=$(echo "$pi" \* "$radius" \* "$radius" |bc -l);
echo "$area"
echo "The radius of a circle is $radius";
echo "The area of a circle is $area";
exit
4
  • 2
    Good spot on the math, but it is not going to fix "bc: command not found" so not actually an answer to the question. – Paul_Pedant Dec 9 '20 at 18:52
  • The code of conduct says to be nice. I was under the impression two people with an immense reputation were going to let him be a laughing stock. – Gerard H. Pille Dec 9 '20 at 18:56
  • 1
    No mirth involved. The OP came up with a sensible script, and can't even get to test it because his course stuck him with a broken copy of Linux. I share his frustration. – Paul_Pedant Dec 9 '20 at 19:11
  • Could just be a messed up $PATH. – Gerard H. Pille Dec 9 '20 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.