Công nghệ Lang thang Linux

Linux bash shell addition, subtraction, multiplication and division

Table of Content

Linux bash shell addition, subtraction, multiplication and division

Write a program that allows a user to enter 2 numbers in the console and the program finds the addition, subtraction, multiplication and division of the numbers.

#!/bin/bash
# read -s -p "Enter Password: " pswd
read -p "Enter Num 1: " num1
read -p "Enter Num 2: " num2
# Phép cộng
kq=$(($num1 + $num2))
echo "Kết quả phép cộng:" $kq

# Phép trừ
kq=$(($num1 - $num2))
echo "Kết quả phép trừ:" $kq

# Phép nhân
kq=$(($num1 * $num2))
echo "Kết quả phép nhân:" $kq

# Phép chia
kq=$(echo "scale=3; $num1/$num2" | bc -l )
echo "Kết quả phép chia:" $kq

# echo $(echo "2/3" | bc -l )

Tham khảo

https://www.unix.com/shell-programming-and-scripting/174290-how-perform-floating-division-shell-script.html?s=168c958a3513a9dfe721395b1968e9ff

https://unix.stackexchange.com/questions/264280/is-it-possible-to-get-a-decimal-output-from-doing-division-in-bash

https://www.log2base2.com/shell-script-examples/operator/shell-script-for-division-of-two-numbers.html

Leave a Reply

Your email address will not be published. Required fields are marked *