The Wayback Machine - https://web.archive.org/web/20231216160022/https://www.geeksforgeeks.org/scientific-calculator-using-awk-command-in-linux/
Open In App
Related Articles

Scientific Calculator Using awk Command in Linux

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The awk command in Linux allows the user to use the numeric functions, variables, logical operators and string functions. Basically, it is a utility that allows the user to write small and effective programs in the form the statements. It is mostly used in pattern scanning and processing. Here, we are using the awk command to perform the following operations:

  • Floating-point calculations
  • Trigonometric operations
  • Logarithms
  • Exponent

Syntax for awk Command

awk options 'pattern{action }' input-file > output-file

Program: Here, we are using Ubuntu. We are saving the below program in file named scientificcalculator.sh. To execute this program, you can use the below command:

sh  scientificcalculator.sh




pi=`echo "scale=10;4*a(1)" | bc -l`
  
while true
do
  
cat << MENU
  
      Menu:
    a) Floating Point Calculations
    b) Trigonometric Operations
    c) Logarithmic Operations
    d) Exponential Operations
      
MENU
echo '
Enter your Choice: \c'
read choice
  
case $choice in
      
    a)  echo "\nEnter expression: "
        read exp    
        cal()
            awk "BEGIN{print $*}";    
        echo "Answer: " `cal $exp`
        ;;
  
    b)  echo "\nEnter Trigonometric function : "
            read exp
            echo "Degree: "
            read degree
  
         e=$(awk "BEGIN{print $exp($degree*atan2(0,-1)/180)}")
            echo "
            $exp($degree)= $e" 
            ;;
  
        c)  echo "\nEnter the logarithmic value: "
            read value
            echo $value | awk '{printf "%11.9f\n",log($1)/log(10)}' 
            ;;
  
        d)  echo "\nEnter the base number x: "    
        read x
        echo "Enter exponent number y: "
        read y
        E=$(echo "$x 1" | awk "{print (($x/1)^$y)}")
        echo "$x^$y = $E"     
            ;;            
        *)
              break;;    
          *)
              break;;
esac
done


Output:

Scientific-Calculator-Using-awk-Command-in-Linux-1

Scientific-Calculator-Using-awk-Command-in-Linux-2


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 19 Feb, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials