Shell Programming Blog Shell Programming Blog

Sunday 9 September 2012

How to print or access value of UDV (User defined variables)

Unknown | 06:58 |


 


To print or access UDV use following syntax

Syntax:
$variablename

Define variable vech and n as follows:
$ vech=Bus
$ n=10


To print contains of variable 'vech' type
$ echo $vech

It will print 'Bus',To print contains of variable 'n' type command as follows
$ echo $n

Caution: Do not try $ echo vech, as it will print vech instead its value 'Bus' and $ echo n, as it will print n instead its value '10', You must use $ followed by variable name.

Exercise
Q.1.How to Define variable x with value 10 and print it on screen.
Q.2.How to Define variable xn with value Rani and print it on screen
Q.3.How to print sum of two numbers, let's say 6 and 3?
Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y)
Q.5.Modify above and store division of x and y to variable called z
Q.6.Point out error if any in following script


Answers : - 

Q.1.How to Define variable x with value 10 and print it on screen.
$ x=10
$ echo $x

Q.2.How to Define variable xn with value Rani and print it on screen
Ans.
$ xn=Rani
$ echo $xn

Q.3.How to print sum of two numbers, let's say 6 and 3
$ echo 6 + 3
This will print 6 + 3, not the sum 9, To do sum or math operations in shell use expr, syntax is as follows 
Syntax:
expr   op1   operator   op2
Where, op1 and op2 are any Integer Number (Number without decimal point) and operator can be
+ Addition
- Subtraction
/ Division
% Modular, to find remainder For e.g. 20 / 3 = 6 , to find remainder 20 % 3 = 2, (Remember its integer calculation)
\* Multiplication
$ expr 6 + 3
Now It will print sum as 9 , But
$ expr 6+3
will not work because space is required between number and operator (See Shell Arithmetic)

Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y)
Ans.
$x=20
$ y=5
$ expr x / y


Q.5.Modify above and store division of x and y to variable called z
Ans.
$ x=20
$ y=5
$ z=`expr x / y`
$ echo $z


Q.6.Point out error if any in following script


ERROR 1 is due to the space left between the equal to sign and the variable. This should not have spaces

ERROR 2 is due to the missing of $sign before the variable 'myno'

Following script should work now, after bug fix!




0 Comments:

   

Post a Comment

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.
3. Please do not spam, spam comments will be deleted immediately upon my review.

Regards,
Mohamed Abubakar Sittik A

 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming