Bash: find out PI
Uncategorized • March 25, 2011 • By Juje007 @ 11:27 pm • • 0 Comments
A long time ago I wrote a script to find out the numbers behind the number PI but that was written in VBS. And today I wanted to recreate that script but than in Bash. I succeeded completely and here is the code:
#!/bin/bash
x=4;
endNr=$x;
lastUsed="+";
counter=3;
while [[ $counter -lt 1000000 ]];
do
if [[ $lastUsed == "+" ]];
then
newNr=$(echo "scale=30; $x/$counter" | bc);
endNr=$(echo "scale=30; $endNr-$newNr" | bc);
lastUsed="-";
else
newNr=$(echo "scale=30; $x/$counter" | bc);
endNr=$(echo "scale=30; $endNr+$newNr" | bc);
lastUsed="+";
fi;
counter=$[$counter+2];
echo $endNr;
done;
Usage (Linux):
Step 1: Put the code inside a file and save it as a .sh file.
Step 2: In command line give the file a chmod of +x (eg. chmod +x fileName.sh)
Step 3: Than run the file (eg. ./fileName.sh)
And here you go.
Have fun!!

