Bash Scripting with Add Numbers via Expr and let cmd

Hacking Truth
1

Bash Scripting with Add Numbers via Expr and let cmd



Bash Scripting

 
In Linux, all tasks from execution of services to loading and unloading of modules are carried out by programs and all programs need to be executed. You use the commands to access all the basic features of kernel. Shell scripting is a way to automate such tasks, and bash is one of the language, that has capabilities enough to be called as scripting as well as a language that can be used for programming on the POSIX platform, for small tasks.  Bash Scripting else if statement

 

Add Two Numbers

You can do the arithmetical operations in bash in different ways. How you can add two integer numbers in bash using double brackets is shown in the following script. Create a file named ‘add_two_numbers.sh’ with the following code. Two integer values will be taken from the user and printed the result of addition. Bash Scripting with Add Two Numbers via variables



#!/bin/bash
echo "Enter first number"
read x
echo "Enter second number"
read y
(( sum=x+y ))
echo "The result of addition=$sum"




Run the file with bash command.
 



Bash Scripting with Add Numbers via Expr and let cmd

 

 

Example – 1: Using ‘expr’ command

The oldest command for doing arithmetic operations in bash is ‘expr’. This command can work with integer values only and prints the output directly in the terminal. You have to use space with each operand when you want to use ‘expr’ command to do any mathematical operations. Create a bash file and add the various ‘expr’ commands to check how the ‘expr’ command works. Bash Scripting with Add Numbers via Expr and let cmd



#!/bin/bash
# Works as string
expr '10 + 30'
# Works as string
expr 10+30
#Perform the addition
expr 10 + 30
#Find out the remainder value
expr 30 % 9
#Using expr with backtick
myVal1=`expr 30 / 10`
echo $myVal1
#Using expr within command substitute
myVal2=$( expr 30 - 10 )
echo $myVal2




Run the file arithmetic.sh
 



Bash Scripting with Add Numbers via Expr and let cmd

 

 

Explain

The output shows that arithmetic operators worked only when space is used with each numeric value and no single quotation is used with expr command. You can also assign the output of expr command into a variable and print the variable later by using backtick or command substitute. 30/10 is calculated by using backtick and 30-10 is calculated by using command substitute.



 

Example – 2: Using ‘let’ command

‘let’ is another built-in command to do arithmetic operations in bash. ‘let’ command can’t print the output to the terminal without storing the value in a variable. But ‘let’ command can be used to remove the other limitations of the ‘expr’ command. Create a bash file and add the following code for seeing how the ‘let’ command works.



#!/bin/bash
# Multiplying 9 by 8
let val1=9*3
echo $val1
# Dividing 8 by 3y
let "val2 = 8 / 3"
echo $val2
# Subtracting 3 from 9
let val3=9-3
echo $val3
# Applying increment
let val4=7
let val4++
echo $val4
# Using argument value in arithmetic operation
let "val5=50+$1"
echo $val5




Run the file arithmetic2.sh
 



Bash Scripting with Add Numbers via Expr and let cmd

 

Explain

The output shows that arithmetic operators worked only when space is used with each numeric value and no single quotation is used with expr command. You can also assign the output of expr command into a variable and print the variable later by using backtick or command substitute. 30/10 is calculated by using backtick and 30-10 is calculated by using command substitute.



I hope you liked this post, then you should not forget to share this post at all.
Thank you so much :-)

 

Disclaimer


This was written for educational purpose and pentest only.
The author will not be responsible for any damage ..!
The author of this tool is not responsible for any misuse of the information.
You will not misuse the information to gain unauthorized access.
This information shall only be used to expand knowledge and not for causing  malicious or damaging attacks. Performing any hacks without written permission is illegal ..!

All video’s and tutorials are for informational and educational purposes only. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. We believe that it is impossible to defend yourself from hackers without knowing how hacking is done. The tutorials and videos provided on www.hackingtruth.in is only for those who are interested to learn about Ethical Hacking, Security, Penetration Testing and malware analysis. Hacking tutorials is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used.


All tutorials and videos have been made using our own routers, servers, websites and other resources, they do not contain any illegal activity. We do not promote, encourage, support or excite any illegal activity or hacking without written permission in general. We want to raise security awareness and inform our readers on how to prevent themselves from being a victim of hackers. If you plan to use the information for illegal purposes, please leave this website now. We cannot be held responsible for any misuse of the given information.



  - Hacking Truth by Kumar Atul Jaiswal

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
  1. The best slots games in UK and Ireland! - LuckyClub
    List of all The UK's best slot games with 5 reels and 3 paylines, The Dog House 2 is one of the most popular casino sites around. luckyclub.live It is the best casino

    ReplyDelete
Post a Comment
Our website uses cookies to enhance your experience. Learn More
Accept !