Friday, August 18, 2017

Work on command line environment variables-displaying,setting and using

Environment variables are effectively shortcut methods.


That allows us to indicate path’s,commands,aliases and other information which is
easily,remembered shortcut designation.


How do I view environment variables?

Variables that set in my particular session.

They are couple of different commands,that are set based on,what I want to see.

If I want to see everything,specific to my shell instance,bash for my user,everything is on the
systemlevel and customize.
I can use the disc command,it will allow me to see shell settings and variables for the current
session.
And allow me to control, -----------------------------à I on or off.

[ root@tcox1  ~]# set | more

we can see,large number of environment variables.


Bash= /bin/bash
BASHOPTS=checkwinsize:cmdist
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
..
..
..
..
..
..
--More--


Important points related to the Environement variable
  1. usually variables are indicated by the capitial letters.
  2. If variables are more than,one full word,in mean time you can see it was separated by _(underscore).
  3. My bash variable in the top is set to /bin/bash .
  4. If you use underscore,because we can have spaces in our variable names.


  • But you will see,in some cases ,some variables are empty.designated by the empty parenthesis.
  • Although have one or more values associated with it.
  • For example my bash environment variable:Is set to /bin/bash.
  • This allows any application,to source that environment variable.by referring to a $ sign.
  • In this way,any application can ask,per a particular value.In this case bash,it can get that our,default,bash path is /bin/bash,it points to that particular item.
  • They are both environment variables and shell settings.
  • And we can control the shell settings and come back.
  • I can see both the environment variables and shell settings by using the set command.



If i just want to see my environment variables,


[ root@tcox1  ~]# env | more

by193/root/usr/bin:/opt/rh/ruby193/root/usr/bin:/opt/rh/ruby193.....................................
.................


I just want to view the variables,that are specific to the shell.with out seeing the environment
variables.


[ root@tcox1  ~]#  shopt

execfail     off
expand_aliases   on
extdebug             off


command description:
then in that case.we can see through the shopt command.
These are the values,that I can turn on or off using the set command.
These are the three commands,that gives the env,set and shopt
Using and viewing,what I want to control or make changes to based on I wanted it for all.
Settings to both my shell and my environment variables to the one or the other.
Typical and commonly used environment variables:
Using the shell settings and the set command.
(we can se something either on or off).
If you list,using the set command,there are number of different options that we can set.
Hash all,which maintains the hash table of commands.
History,which allows history file to maintains history of commands,that we have excuted.
Monitor which runs,background processes in a different group and lets the console know,when
there are done.no clobber which is one of those,you may see on your lpic exam.


Which is by default turned off,what clobber does it wont allow a redirect to override a file that
exists.
By default,I can override a file with a single redirect.
What is mean by that is,
If I have a file,that I created.




[ root@tcox1  ~]#  ls -al  >  tmp/test.txt
[ root@tcox1  ~]#  ls -al    tmp/test.txt

I have that information,that was piped to that file.
When I do the cat,it will display the contents:

[ root@tcox1  ~]#  cat tmp/test.txt

[ root@tcox1  ~]#  ls -al /etc  > tmp/test.txt

it will silently override the existing content in that file.
this is called the appending.
This is called the appending the contents.


How to the set the clobber?

I want not to override the file, by using the single redirect and use no clobber

Set the clobber,that will prevent me,even though I am using the single redirect,it will prevent
me from being over to override.then it will tell not to override the file.
If the file doesn’t exist,it will create the file for us.that is called the no clobber.

We can see in the following:
We can run the syntax check,when enabled:
When terminate the jobs,notify the console.
Immediately,instead of next execution of the job.



We can see the set commands,we can see which of the following are enabled.
That is whatever is available in the SHELLOPTS environment variable or shell variable.


[ root@tcox1  ~]#  ls -al /etc  > tmp/test.txt 
[ root@tcox1  ~]#  set  | grep SHELLOPTS
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
[ root@tcox1  ~]#  


these are all,turned on.
That is no clobber,that is turned on by default.
And the way that you turned on,is little bit calculated.
Something is on,with –o or off with +o
And that may seen like,it may be opposite.


Once we do this


[ root@tcox1  ~]#  set  -o noclobber
[ root@tcox1  ~]# 

we have to run the set command




[ root@tcox1  ~]#  set  -o noclobber
[ root@tcox1  ~]#  set  | grep SHELLOPTS

SHELLOPTS IS A VARIABLE,THAT CONTAINS ALL OF THIS,WE WILL SEE THAT NOCLOBBER IS
ENABLED.


Before,we see this:


[ root@tcox1  ~]#  ls -al /etc  > tmp/test.txt 

we created ls –al and redirected to the tmp/test.txt


we can see,how we override previously,by issuing that command and on another directory and
redirecting to the tmp.

Most important point:

NOW,IF I SAY TO THE /var DIRECTORY,

[ root@tcox1  ~]#  ls -al /var  >  tmp/test.txt
-bash: tmp/test.txt: cannot overwrite existing file
[ root@tcox1  ~]# 

It will tell me,I cannot override.

This can be fixed immediately,


[ root@tcox1  ~]# set +o noclobber
[ root@tcox1  ~]# 

by set that to noclobber

then it will execute to that file:

[ root@tcox1  ~]#  ls -al /var  > tmp/test/txt
[ root@tcox1  ~]# 

any of these options,we can turn on or off by using the set command.
There are other variables,that are specific to our environment.

The most common one,probably be the path.
And the path environment variable is typically used,to the system where we need to look
for,binary excutable files or shell scripts that have permission secs.
And in other words,that anything that I can run.
Typically,the /usr/bin is one of those locations.that has the excutable file.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 xz
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 xzcat  --> xz
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 xzcmp --> xzdiff
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                  xzdec


we can see all of the files have excutable permissions listed here.which means I can run any of
those commands directly(with out providing the direct path or relative path).




[ root@tcox1  ~]#  pwd
/root
[ root@tcox1  ~]#  mkdir tmpbin
[ root@tcox1  ~]#  cd tmpbin
[ root@tcox1  ~]#  vim test.sh
[ root@tcox1  ~]# chmod 755 test.sh

I created the test.sh in the root directory,but if want to run that test.sh directly I
cannot,because the system looks for the executable file.(because my system don’t know where
to find that executable file).


when I do the test.sh,it says so such file or directory.


[ root@tcox1  ~]#   test.sh
-bash:  /root/bin/test.sh: No such file or directory
[ root@tcox1  ~]#  


I run In following ways:

[ root@tcox1  ~]#  tmpbin/test.sh
My test script
[ root@tcox1  ~]#  cd tmpbin


Note:

it will only work when I give the relative path : ./test.sh


An entry in the environment variable for path:

Now,obiviously I don’t want to override the current value for the path.


When i do the echo path:



[ root@tcox1  ~]#  echo $PATH
/opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin:


it is a whole bunch of directories.
It is also include the path’s like:
/usr/local/bin
/local/sbin
all of the places you can find the binaries or the shell script.
The highlighted region is a default path.

[ root@tcox1  tmpbin]#  echo $PATH
/opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin:
/usr/local/sbin: /usr/local/bin: /sbin : /bin: /usr/sbin : /usr.bin: /root/bin

and I don’t want to accidentially clobber,those values.
I have to provide the path’s for all my executables.



[ root@tcox1  tmpbin]# pwd
/root/tmpbin
[ root@tcox1  tmpbin]#

I can do by the export PATH which means export to my environment.
$PATH current
(the path=to the current path).
: and I want to append the path,which I want to see.



[ root@tcox1  tmpbin]#  export PATH=$PATH: /root/tmpbin
[ root@tcox1  tmpbin]#  echo $PATH
/opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin:
/usr/local/sbin: /usr/local/bin: /sbin : /bin: /usr/sbin : /usr.bin: /root/bin: /root/tmpbin


you can see it was added to that.
And if I am not in that test directory,I cannot provide the path for the directory.

[ root@tcox1  tmpbin]#   cd
[ root@tcox1 ~ ]#  test.sh
My test script
[ root@tcox1 ]# 

will successfully executed



And we can do that,with any variable is accessible or we can set the new environment variable.


[ root@tcox1 ]#  export MYPATH=/home/user
[ root@tcox1 ]#   echo $MYPATH
/home/user
[ root@tcox1 ~]# 

if I want to add anything to that.


[ root@tcox1 ~]#  export MYPATH=$MYPATH: /etc
[ root@tcox1 ~]# 

[ root@tcox1 ~]#  echo $MYPATH
/home/user: /etc
[ root@tcox1 ~]# 

I am building the path,that I can provide the path to any application and any script.
That equals mypath.
It does not always have to be the path.
If I wanted it,to someother value.
I could say,
it


[ root@tcox1 ~]#  export MYSHORTCUT=128
[ root@tcox1 ~]# 


[ root@tcox1 ~]#  echo $MYSHORTCUT
128
[ root@tcox1 ~]# 


[ root@tcox1 ~]$ cat  .bash_history

if you see the path of the HISTFILE

[ root@tcox1 ~]$ echo $HISTFILE
/home/user/.bash history
[ root@tcox1 ~]# 

lets echo out the history control variable:

[ root@tcox1 ~]$ echo $HISTFILE
/home/user/.bash_history
[ root@tcox1 ~]$ echo $HISTCONTROL
ignored ups
[ root@tcox1 ~]$

it is saying the ignoredups.
Whatever you execute first,will be the first.
If you do echo hi for two mins,it will now allow the duplicate:

History of the commands:
cat .bash_histroy
echo "hi"

[ root@tcox1 ~]$


Important point:

I don’t want,particular file to track in the history file.
Here I added the space and in the HISTCONTROL FILE I mentioned ignore space.


[ root@tcox1 ~]$ export HISTCONTROL=ignorespace
[ root@tcox1 ~]$ echo $HISTCONTROL
ignorespace
[ root@tcox1 ~]$  ls -al
total 36

when are working with the variables:we can override them,by exporting the command.
The completely the new value or we can append them by exporting with the current value.
With the (dollars)$ sign and name of the variable and the : and the name value we want to
append at the end.
Ofcourse,we can use the command substitution and incorporate these variable values in the
command.
Like we are the command susbstitution to say:
echo $PATH


[ root@tcox1 ~]$ echo $HISTFILESIZE
100000
[ root@tcox1 ~]$ echo $PATH
/opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /opt/rh/ruby193/root/usr/bin: /usr/local/sbin: /usr/sbin: /sbin: /home/user/bin

I want to change  to my home directory

[ root@tcox1 ~]$ pwd
/
[ root@tcox1 ~]$ cd $HOME
[ root@tcox1 ~]$ pwd
/home/user
[ root@tcox1 ~]$ 


Any variable, I can use in any number of command that allow me to use the value of the
variable.
In the command,the value of variable will be substituted.as a part of the command.
So that the output uses it,and retype the exact value.
So these are the ways,we work with the shell environment variables.


































No comments:

Post a Comment