Linux - Set variables
632 views
Nov 28, 2024
Linux - Set variables
View Video Transcript
0:00
so into our previous video we have seen
0:02
how we can run a small shell script file
0:05
where have added simple commands and
0:07
pose that I have just run the command
0:09
now what if I want to store something
0:12
somewhere so that into the program I can
0:14
use those particular date again now for
0:17
example you have a huge host name or
0:20
hostname which you cannot remember so we
0:23
can use it later so that I can host or
0:26
we can save those particular host name
0:28
into a particular variable or I can take
0:30
an example if you have an address and in
0:33
the form or in a program you want to add
0:35
your address multiple thing if you want
0:38
to add those multiple at your address
0:40
into multiple lines what will happen you
0:42
have to type everything every time if I
0:45
don't want to type it again and again
0:46
what I can do I can store it somewhere
0:48
with some letters variable and the data
0:51
which you are going to add now here an
0:53
example like your address that forbid a
0:55
value so we can add any value to any of
0:58
the variable so that into the program I
1:01
can use the variable instead of value
1:03
which reduces my program size and my
1:06
efforts that is the task of your
1:09
variable and that is the variable which
1:11
we use now if I'm talking about
1:12
variables again we have different type
1:15
of variable declaration now here the
1:17
variables can be declared for a local
1:19
local purpose means if I am using a
1:22
variable I can use within a program only
1:24
once that is a local variable now if
1:27
suppose I have created a program where I
1:29
have a sub programs of those particular
1:31
programs so I want to declare those
1:35
particular variables every time into
1:37
these sub programs know I can inspite I
1:40
can define a environmentally variable
1:42
and water mental variable means I can
1:45
set a value in particular main program
1:49
post that if you have some deviation of
1:51
those programs they can again call this
1:53
particular variable which is called as
1:55
an environment variables so environment
1:58
variables can be used into these sub
2:00
programs into that particular program
2:03
itself the next we have that is called
2:05
as a shell variable so shell variable
2:08
be using any of the programs into your
2:10
Linux operating systems
2:12
so basically shell variables are by
2:14
default declared they are declared by
2:16
the Linux itself even though if I want
2:18
to add some shell variables I can even
2:20
add the two which can be called into any
2:23
of the programs any of these sub
2:24
programs created into the same terminal
2:27
different terminal same location or a
2:29
different term location that the reason
2:31
there's also called as a global variable
2:33
so here we have three type of variables
2:36
local environmental and your shell
2:39
variable let us let us see how we
2:42
generally use this variables into our
2:44
program and create a program by using or
2:47
setting the variables into it so let us
2:49
begin with our terminal so in our
2:52
previous video we have seen how we can
2:54
create a sample file a sample file which
2:57
we have created that was again
3:00
containing a variable which we have
3:02
already discussed like we have a code a
3:05
particular text that I am asking my user
3:09
to enter his first name so when the user
3:13
enters first name that is accepted into
3:15
the variable a so this is a variable and
3:18
I'm adding the value to this variable
3:20
and letter to it I'm going to print echo
3:24
hello and this variable name the same
3:28
way we can create more variable files
3:30
like for example I am going to create a
3:32
name dot SH file now for example I do
3:38
not want my user to enter the name right
3:40
so I can also run hello
3:47
you are talking to
3:54
you
3:56
Church shapes right and same way we can
4:01
read it by you know I don't have to read
4:04
it i anything here so just so I'm
4:05
defining our name like you know a text
4:07
that is you are talking to your mr.
4:09
James and we can also define the
4:12
designation of this person designation
4:16
equal to whatever the designation he has
4:20
like for example senior or we can say
4:23
CEO of the company that we need to
4:28
define and to the courts
4:38
and then we can say his contact number
4:51
you
4:57
okay so this is his contact number now I
5:00
want this again to be displayed so I'll
5:03
be using it as echo and my designation
5:13
and the company it should be displayed
5:18
to the user now else I can give comma
5:23
and we can define designation so
5:30
generally here this designation and this
5:33
contact is the variable and I want to
5:35
print this particular thing same way we
5:38
can also define echo
5:42
you can contact me at and then I'll
5:53
define contact done and just I'm going
5:59
to save the file now let's see how
6:01
generally this particular file runs so
6:03
here I should get it come on I should
6:06
get this message you are talking to mr.
6:08
James my resignation in the company is
6:11
CEO of the company and you can contact
6:14
me at this number so let us check I'll
6:18
just run this file and see I got the
6:23
complete text right now it's suppose I'm
6:29
using this particular file itself named
6:31
or as such and someone lecture on has
6:33
changed the designation like you know
6:35
here somewhere someone has changed or
6:38
added his own designation for example I
6:41
just find echo I am an assistant
6:56
managed
6:59
okay then he again he added the
7:01
designation our technician it goes to
7:08
assistant manager okay I'm just going to
7:17
define the name Jones okay
7:22
and then as contact number
7:33
you
7:37
I can't sir there's SS contact number
7:40
again you want to print the same thing
7:47
as king you're fine the second Asian
7:57
again echo contact number
8:07
Quan Chi so generally you can see now
8:09
what happened I have saved the two
8:12
values over here the mr. Jones you know
8:16
designation and contacts over here into
8:18
the same variable and the programmers
8:21
Pig will just assume the program is a
8:22
huge program and later on even the
8:25
assistant manager Jones has added his
8:27
designation in the same variable now
8:29
what is going to happen that we are
8:30
going to see into the output
8:33
okay now I'm going to run the same you
8:38
can find it is giving you this part as
8:42
this but while I type over here you know
8:47
I get the message mr. assistant roll my
8:49
it is again accepted as it is not a
8:52
variable and post that again we've all
8:54
found that there isn't some error rate
8:56
of all there will be and there is
8:58
actually a different error we have got
8:59
here
9:01
[Music]
9:09
you
9:11
there are actually the characters which
9:13
we use use a several different kind of
9:23
so let us check now I've just changed it
9:28
name dot I searched and I can find found
9:33
contact number is this one this contact
9:37
number is this one let us check that is
9:39
again error which we found so so you can
9:42
also check for the errors and really get
9:44
formed so this is the assistant manager
9:47
we need to define it cold patience right
9:52
as we are using more than one word down
9:58
with that know everything else is ready
10:00
again name dot Sh
10:05
all right so it's showing you the skirt
10:09
again it is not showing you the company
10:11
name right so this is the company name
10:14
oh there is a CEO of the company here
10:16
again if you have some mistake it's not
10:17
displaying or designation and most
10:21
probably it also happens like you know
10:22
if you give the same contact numbers now
10:24
and the same variables sometime it might
10:26
display you the wrong entries correct so
10:30
that's reason like for example into this
10:33
the name dot as such I want to display
10:37
just display
10:41
designation
10:47
chin and a contact number at the end now
10:55
what is going to display that we are
10:58
going to check here which display which
11:01
our designation will be seen over here
11:04
that we need to check you can see it is
11:05
showing you the designation of your
11:08
assistant manager right means I want to
11:12
become to know like you know why this is
11:13
completely skipped so it is taking the
11:15
latest one that is is assistant manager
11:18
now for avoiding such reason if you know
11:21
such happenings like where the complete
11:25
variable is getting overlapped with
11:27
something else so what you can do is you
11:30
can make your variable read-only so what
11:33
will happen you can see
11:36
so for making your variable read-only
11:39
what you need to do is just after
11:41
defining the variable you need to define
11:45
read-only there's a mention same way
11:52
read-only
11:58
on cat
12:01
our
12:04
and now see what happened I'll save the
12:07
file I'll hit enter
12:09
I'll give come on name God as such so
12:14
it's giving me an error that the
12:17
variable which we have used for your
12:19
assistant manager for defining his
12:21
designation and defining his contact
12:24
cannot be used because they are the read
12:27
only variable so when we run or we know
12:30
when we run a file or when we run a
12:33
script will come to know like why we are
12:36
not able to provide designation so that
12:39
this user can move into thee again
12:44
descript file called as named or as such
12:46
and he has to change the designation
12:52
variable with a different name like we
12:54
can also change it with
12:58
any other available
13:05
you
13:08
that's it chin and
13:16
and for contact Hagan use font and here
13:20
again you can display in the same way
13:30
and again so much like and check giving
13:35
a such name daughter such now it should
13:37
not give you error but now here it is
13:39
showing you the correct now contact
13:41
number and you know the designation of
13:44
your mr. Jones that means here the entry
13:47
is what we have you know the variables
13:49
what we have added over here they are
13:51
made read-only so that someone else in
13:53
the near future and that the same scrub
13:56
should not use the same variable for
13:57
storing the other value because the
14:00
variables generally we are using over
14:02
here there are a scalar variables means
14:04
at the time they can store only one
14:07
value so that's a reason in the last
14:09
line when we have given the command or
14:11
when we have you know a printer to the
14:13
designation as well as in contact of
14:16
your name dot as such it frenches me
14:18
with the designation last time of these
14:21
last designation which we have added but
14:24
now as I made it read-only and I have a
14:27
particular you know individual variable
14:29
and individual variable contain our
14:31
individual value that's the reason we
14:33
are able to producing so it is you know
14:35
recommended that whenever you declare
14:37
your variable into you know a script
14:40
when you come to know that the Skyrim
14:43
script might be used by many other
14:44
people and you can also you know forget
14:49
what the variables you have used earlier
14:51
so what you can do you can make your
14:52
readable variables so this is how
14:55
generally we use the variables now again
14:58
we can use a particular command called
15:01
as unset so that if suppose I do not
15:04
want to set a variable for example the
15:07
same file I'm going to open a name dot
15:09
as such and later on I want to use these
15:12
variables that is contact and
15:20
designation for example he has already
15:21
designed so I'll just comment this
15:24
correct so for unsetting this particular
15:27
variables either we can directly comment
15:30
out everything or what we can do is we
15:32
can give the command unset Oh
15:36
variable name so it will automatically
15:41
unset a variable so even after that if I
15:44
right to pin or you know get access to
15:48
the variable value that is like it
15:52
should not print anything right so just
15:56
a sec I need to provide the dollar sign
15:58
if I want to run a particular variable
16:01
and this is again unset it is yes I see
16:05
so I'm just going to define this
16:07
variable I want one set I do not want to
16:09
display this particular variable but I
16:11
want to display the other variable which
16:13
I have right now that is a kill
16:16
dollar and your
16:22
once we have done with that I'm going to
16:24
save the same and hit enter let us run
16:27
the file again and such name dot Sh
16:31
you can find the contacts as in
16:33
displayed the designation is not
16:35
displayed because I have unset that
16:38
particular variable along with that we
16:40
can see the other data is what we have
16:43
so here we have seen how we can set up
16:45
variable that is your scalar variables
16:47
we have also seen like how we can you
16:49
know make a variable read-only so that
16:51
no one else can replace your value of
16:54
that particular variable along with that
16:56
we have also seen how we can print a
16:58
variable and unset a variable so this is
17:01
we have talked about the variables which
17:03
we can add into our shell script and how
17:08
we generally use it so into next video
17:10
we are going to see more of your shell
17:13
scripting so keep watching
#Computer Education
#Other
#Programming
#Scripting Languages