Input and Variables in Shell Scripting
Input and variables in Shell ScriptingCommands
In this tutorial we are going to discuss
of shell scripting . Where taking input is easy way in shell scripting. Now let's make our own user friendly script
Keyboard or user input in Shell Scripting
if we need to take input we must use read command.
Copy below code and paste in terminal also don't forget to make this code executable
Where in line 1 shows shebang.it's a correct bash intepreter
In 2nd line shows the function of the code as a comment.
This line does not gives any result
Where in 4th line is a inbuilt command in linux.it asks users to enter some input from your keyboard.
by help of read commmand we have to instruct a variable that to store the input received from keyboard.
If you need to print some variable on output screen you have to add $ symbol before the variable.
The programming structure to other languages like javascript,linux,rubyc, c++,java and python in shell scripting.
lets checkout some examples.
Where shell declares it as a general statement.
For this we have to change that code into commandexpr.
But how i am going to explain now
you have to put expr before every mathematical expression then the code starts working.here You can use two ways to perform arithmetic operations in bash scripting . Through Expr function and some inbuilt functions in bash scripting like double parenthesis to perform arithmetic operations
How to use expr Function in Bash Scripting
copy the below code and paste it in Linux terminal and execute then code works fine
#!/bin/bash
#Arithmetic operations in bash
num1=134
num2=33
add=`expr $num1 + $num2`
rem=`expr $num1 % $num2 `
div=`expr $num1 / $num2`
multiplication=`expr $num1 \* $num2`
echo "addition is : $add"
echo "Remainder is: $rem"
echo "Multiplication is: $multiplication"
echo "Division is $div"
2)expressions must as `starts and ends with reversequote` can be commonly used for mathematical expression.
3)We have to put some space between variable and operator.
36+56 or num1+num2 is error expression.
35 + 56 or num1 + num2 is a valid expression.
4) Where * symbol can't be used for multiplication purposes in Linux because it us
is wild card operator .so we have to discard wild card operator by \.
1)You shouldn't use echo function
2)it can print expression value to general output from.
3)Get output quickly without typing vast code.Just type as i shows here get output result instantly
expr 9 + 8
expr 9 - 8
expr 9 \* 8
expr 9 % 8
if you want to do multiple calculations in shell scripting you must place your variables or values in between double parenthesis .it's the ultimate easy way to maths in bash.
copy the below code and paste in your Linux terminal tap enter to run
i have explained everything in previous articles.
Let's make Calculator in Linux
we might already developed calculator with html,java script,css.
Now we are going to make a working calucalator with bash script.
copy the below code and paste in your Linux terminal tap enter to run
I hope you may loved this cool post.If you have any doubts post in comment section.
Please check out advertisements on our website which helps us to post more content for you .
Don't forget to share this cool post with all your friends and groups. HAVE A GREAT DAY.
In this tutorial we are going to discuss
User Input Arithmetic Operator
Still we have learned basics ofof shell scripting . Where taking input is easy way in shell scripting. Now let's make our own user friendly script
Keyboard or user input in Shell Scripting
if we need to take input we must use read command.
Copy below code and paste in terminal also don't forget to make this code executable
#!/bin/bash
#Keyboard input in shell scripting
echo "Enter your name"
read name
echo "Hello boss MR $name"
Where in line 1 shows shebang.it's a correct bash intepreter
In 2nd line shows the function of the code as a comment.
This line does not gives any result
Where in 4th line is a inbuilt command in linux.it asks users to enter some input from your keyboard.
by help of read commmand we have to instruct a variable that to store the input received from keyboard.
If you need to print some variable on output screen you have to add $ symbol before the variable.
Arithmetic operations with Shell Scripting
We might have used Arithmetic operations in different programming language.The programming structure to other languages like javascript,linux,rubyc, c++,java and python in shell scripting.
lets checkout some examples.
num1=92num2=184Now we have to add both numbers.
echo $num1+$num2This is invalid statement gives a error in result on output screen.
Where shell declares it as a general statement.
For this we have to change that code into commandexpr.
But how i am going to explain now
you have to put expr before every mathematical expression then the code starts working.here You can use two ways to perform arithmetic operations in bash scripting . Through Expr function and some inbuilt functions in bash scripting like double parenthesis to perform arithmetic operations
How to use expr Function in Bash Scripting
copy the below code and paste it in Linux terminal and execute then code works fine
#!/bin/bash
#Arithmetic operations in bash
num1=134
num2=33
add=`expr $num1 + $num2`
rem=`expr $num1 % $num2 `
div=`expr $num1 / $num2`
multiplication=`expr $num1 \* $num2`
echo "addition is : $add"
echo "Remainder is: $rem"
echo "Multiplication is: $multiplication"
echo "Division is $div"
some important rules of expr function
expr: Is an inbuilt function it tells bash to evaluate expressions as the mathematical expressions.2)expressions must as `starts and ends with reversequote` can be commonly used for mathematical expression.
3)We have to put some space between variable and operator.
36+56 or num1+num2 is error expression.
35 + 56 or num1 + num2 is a valid expression.
4) Where * symbol can't be used for multiplication purposes in Linux because it us
is wild card operator .so we have to discard wild card operator by \.
More examples on expr
1)You shouldn't use echo function
2)it can print expression value to general output from.
3)Get output quickly without typing vast code.Just type as i shows here get output result instantly
expr 9 + 8
expr 9 - 8
expr 9 \* 8
expr 9 % 8
Use double parenthesis in bash script
if you want to do multiple calculations in shell scripting you must place your variables or values in between double parenthesis .it's the ultimate easy way to maths in bash.copy the below code and paste in your Linux terminal tap enter to run
#!/bin/bash
#Arithmetic operations in bash with double parenthesis format
num1=143
num2=363
div=$((num1 / num2))
add=$((num1 + num2))
multi=$((num1 * num2))
remainder=$((num1 % num2))
echo "addition is $add"
echo "division is $div"
echo "multiplication is $multi"
echo "remainder is $remainder"
i have explained everything in previous articles.
Let's make Calculator in Linux
we might already developed calculator with html,java script,css.
Now we are going to make a working calucalator with bash script.
copy the below code and paste in your Linux terminal tap enter to run
#!/bin/bash
#Arithmetic operations in bash with double parenthesis formatecho "Enter first integer"read num1echo "enter second integer"read num2add=$((num1+num2))multi=$((num1*num2))div=$((num1/num2))remain=$((num1%num2))echo "addition is $add"echo "Division is $div"echo "remainder is $remain"echo "multiplication is $multi"
conclusion
I hope you may loved this cool post.If you have any doubts post in comment section.
Please check out advertisements on our website which helps us to post more content for you .
Don't forget to share this cool post with all your friends and groups. HAVE A GREAT DAY.
Post a Comment
Post a Comment