Thursday, August 17, 2017

GNU and Unix commands

Working on the commandline:
We are talking about the shell in general and how to source the scripts.
That relate to the bash shell.
The bash shell is the default linux shell.most distributions,most interpret the commands we
mostly used for the system.
Bash is the superset of the shell,/bin/sh is the default shell.
In the past,bash would depend on the /bin/sh,
All of the functionality in the shell is implemented with in the actual bash.

There are number of shells,that are available.like tsh etc.


-->  sudo yum install csh ash zsh

we can install,using the tsh,zsh etc.
there are two special shells,they are bin/false shell and nologinshell.
Bin/false is an often to use your account,when that user,is not intead to be a interactive user.
(your not expected to ever login).


[user@tcox1 ~]$ sudo cat  /etc/passwd   |  grep false
[user@tcox1 ~]$ sudo cat  /etc/shadow   |  grep false


/etc/passwd is only used for local users only
The /etc/passwd contains one entry per line for each user (or user account) of the
system. All fields are separated by a colon (:) symbol. Total seven fields as follows.


oracle  : x  :  1021  : 1020 : Oracle user : /data/network.oracle:/bin/bash
     (1)     (2)      (3)        (4)          (5)                        (6)                         (7)


Username : It is used when user logs.It should be between  1 and 32 characters in length 

Password  :  An x character indicates  that encrypted password stored in  /etc/shadow file.

User ID  (UID)  : Each user  must be assigned  a user ID (UID). UID 0 (Zero) is  reserved for root and UIDs  1-99 are reserved for other predefined  accounts Further  UID 100-999 are reserved by system for administrative and system accounts/groups.

Group ID (GID) :  The  primary group ID (stored  in /etc/group file).

User ID info : The  comment field.It allow you to add extra information about the users  such as users full name,phone number etc.This field use by finger command.

Home directory  : The absolute path to the directory the user will be in when they login.If this directory does not exists then users directory becomes /
command/shell : The  absolute path of a command  or shell (/bin/bash). Typically,this is a shell.Please note that it does not have to be a shell.

Your password is stored in /etc/shadow file

There is a special file called the /etc/nologin
This is intended to prevent all the logins from the
system.

If there is a file called nologin,it doesn’t allow you ,when
you reboot the system,you have to login through the
resue mode and operate the things.


(becareful,you don’t create that and it shouldn’t/doesn’t
exist).


Coming in to the scripts:

There are couple of ways,to excute the scripts on our system.
Most of the time,scripts are named with a .sh.atleast for a customed developed scripts.
You develop or the other people develop.
Sometimes the script will exist in a normal file.
For example vsftpd might be a script for that particular service.
It does not have the .sh
Because it is intended to be a service file.
Eventhough that would excute the commands like script.
It is classified it is a service file.
In the /usr/bin/*.sh


[user@tcox1 ~]$ ls -al  /usr/bin/* .sh
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   /usr/bin/lesspipe.sh
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   /usr/bin/mksmbpasswd.sh
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   /usr/bin/setup-nssysinit.sh

going to the home directory and follow the below steps:

[user@tcox1 ~]$ pwd
/home/user
[user@tcox1 ~]$  sudo cp /usr/bin/mksmbpasswd.sh
[user@tcox1 ~]$   ll

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_history
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_out
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_profile
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bashrc
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      mksmbpasswd


[user@tcox1 ~]$ sudo chown user:user mksmbpasswd.sh
[user@tcox1 ~]$ clear

this is a general file,
that is the awk command that works with the smbpasswd file (samba password file).
Reminder the #!/bin/sh ----à this is simply a link to the bash.
(this is actally,run another bash instance and run the status.).
there are two ways to excute this script:


#! /bin/sh
awk  'Begin {FS=":"
             printf("#\n# SMB password file. \n#\n")
                }
{ printf(  "%s:%s:xxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx: [UD        ]: LCT-000000000:%s\n" , $1 ,$3, $5) }

replacing the script with these two lines:



[user@tcox1 ~]$  ll
total  5
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_history
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_out
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_profile
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bashrc
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      mytest.sh
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .viminfo


[user@tcox1 ~]$  /home/user/mytest.sh
This is my test script

one is  ./mytest.sh
second is  /home/user/mytest.sh

if you go and see in the home directory:
total  5
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_history
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_out
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bash_profile
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .bashrc
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      mytest.sh
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx      .viminfo



These are  the scripts, that are excited based upon the condition.

Related to my user.

so, let us go through the global content,that effectively changed.

When a system administrator,creates you as a user,they are certain settings applied to you.
That become applied to you,everything you login.
These files can be created or edited directly.
Or they inherit the functionality of the system wide.
/usr/bin ---à is mainly for the executable files.


# /etc/profile

#  System wide environment and startup programs, for login setup
#  Functions  and aliases go in /etc/bashrc
#  It's NOT a good idea to change this file unless you know what you
#  are doing It's much better to create a custom sh shell script in
#  /etc/profile.d/ to make custom changes to your environment, as this
#  will prevent the need for merging in future updates.

pathmunge () {
         case ":${PATH}: " IN
                 *: "$1" : *)
                    ; ;
               *)
               if [ "$2"  = "after" ] ; then
                     PATH=$PATH:$1
               else
                      PATH=$1:$PATH
                fi
             esac
    }
    if  [ -x /usr/bin/id  ]; then
       if [ -z "$EUID"  ]:; then
              # ssh workaround

  
Export,Hostname is everything is a environment variables.
Those things,help you to create the .profile for you.
They are couple of different types of profiles:
Etc/profile is the system wide configuration file.affected when a user is created.
Then what is called as a source script,this will excute,when you login as a login shell.
In script,there it is sourced,
Is the user is created,is a part of the group or owner of the file.
Normally,anytime I executed the script.
Disappears when the scripts exists?? (13.24)
Variables that are set,specific to that particular script,it exists because it is completely separate
environment.
Everything that happened with in that,goes out of the scope.
However,if you source the script,then that excutes in the same environment,your in when you
started the scripts.
Will be carried and will be there in the existing environement.
In this profile that is the /etc/bashrc
(good place to do things like,set the way that the command prompt looks).

How your environments prints based upon the other parameters.


where in the bashrc,you will environment stuff,does not go any,but functions and aliases,will
go in here,for example:

if you take the example of aliases,that is the bigger command in to a shorter one.


is the same function,we have to do it.
The system level aliases are set In the /etc/bashrc
That will carry forward in to your local configuration.
As a result,you will have couple of different files.
Inolder unix/linux systems,your personal profile will be .profile or .bash profile.
If the .profile exists,it will read and excute on the system login shell.for the user,just like bash
profile.
Becareful,while your doing this,because the .profile will be ignored than the .bash
Bash_profile will be what it express its.



Note:
with this,we can know how to source the script from one script to another script.
.bashrc is excuted or sourced from something.



these are specific to the path’s and including one source script in another script.
We can see environment variables.but env variables are things to set.


PATH=$PATH: $HOME/bin


environment variables are nothing but,that are referenced to the longer lists.
Path is a good example of a environment variables.
Rather that,worrying about,where the excutable files are stored on our system.

You can indicate to the system well.

We can keep in the long listing files and keep it with the delimiter : separating all the executable
files.
(this will tell your system,where to look for system executables).

We can reference variables,with in our scripts using the $ sign.

PATH=$PATH: $HOME/bin

so here,we sourced the .bashrc inside the .bashprofile
so effectively,both of them will run everytime.that I login to loginshell.
Whether it is a console or the remote.
And at the time,they were build,when my user was created.
/etc/profile and /etc/bashrc
one sources the other and it all applies in my environment.
And that .bashrc was created from the /etc/profile.
And that only effects the environment,when we are loginas,it doesn’t apply,everyone on the
system,it applies only to you.
And again if a ./profile exists in the ./bashprofile will superseded.
Bash login and the bash logout,execute also as source scripts.
They are based upon,when you login to the system.
Now,we are talking about the order of precedence in a second.
When we are finished setting the path’s and aliases,then we can say that,pur environment is
setup.
When we see our files in the user directory.(we have to know the order).
The first thing,happens when we login.
As a result,your .bashprofile works,meaning it will run.
Your .bashprofile will call the .bashrc from with In it.
Most of the time,you can commented out.
You have the ability to do so.
In .bashrc,will run your path’s and aliases.
Then you will interact with the system ingeneral.
And then you logout or exit.
And user.bashlogout.
The only time,the bashlogin or bash_login will run in your home directory.is if there is not
bash_profile
So if bash_profile doesn’t exist but bash_login will login if it exists.
Bash_profile will always take the precendence overall the directories in the home directory.
The only instance,they won’t run is,the root or the other user becomes new.
So If you use the su command to use the another user or to become the root user.
There is one important important point:
Su root - and the su root
If you use the su root - it will come with the session having the bash_rc and bash_history and
other related stuff.
If you use the su root it will only come with the session not having this all.
All of the environment variables set for the root account,log’s in.through
.bashhistory,.bashlogout,.bashprofile,.bashrc are not applied to the su root session.
(it’s not a login prompt). And if your not using the ‘-‘,if your saying that,you don’t want to run
the source scripts as a part of the session.
If I go to my user directory,which happens to be root,

[user@tcox1 ~]$su root -
Password:

we have to use this ‘-‘ after the user,when we switch.
How do you get to the user’s home directory?

there are 3 ways to reach home directory:


[user@tcox1 ~]$ cd /etc
[user@tcox1 etc]$  cd /home/user
[user@tcox1 ~]$ cd /etc
[user@tcox1 ~]$ cd

Second:
[user@tcox1 etc]$ cd ~
[user@tcox1 ~]$


Third:
cd

these are three ways to go to the home directory.
And we can even change to the sub directories and the home directories.
For example,I created the tmp directory in the home directory:

I can go to that path directly.


user@tcox1 etc]$ cd ~/tmp
user@tcox1 tmp]$



There are some special characters that are with in a shell prompt or with in a script.
That are interpreted in a specific way.
If you use a $ as a part of the command or as a part of the script or as a part of the search.
And this will allows us to identify as a variable.
We can escape any single character,by using the \ .
With the \\ we need to include with the double slash.
While we have to find out or search anything we will use the wild card.
I want to list anything that,starts that as a single character,for example
We want to list anything that have the wildcard

? means the anything


[user@tcox1 ~]$  ls -al  ?ytest.sh
xxxxxxxxxxxxxxxxxxxxxx         mytest.sh

? is a wildcard.
I can put this,in any value or in any command:

we have to know the length of the string,to know and provide the enough wildcards to provide
it.
Or
You have to use the 0 to n wildcard.
When we are working with the processes.


(when we want to run,something in the back ground).
Say,we want to list all the files and directories in the system:


 [user@tcox1 ~]$ ls -alR /


we can run things in the background with a single & .
we can run many commands running excuting on the same line.
There are couple of different ways,that I can do it.
If I want to excute the two commands for example.
But I want to excute the second command.
One is successfully,I can use the && .
If the first one is successful and I want to use the second one.we can use the && (double
ambersent).
What is that mean?
I have two applications I have,which is a special show,we talked about.
That is the bin true and bin false.


Bin true: will always evaluates the true.
Bin false :will always evaluates the false.

Steps to execute the commands


If I want to execute the two commands:
(where I want to excute the second one).if the first one is successful.
Then I can test that,by doing this.

[user@tcox1 ~]$ /bin/false && echo "This executed"


if the first one is successful,then only it goes for the second one.

[user@tcox1 ~]$ /bin/false && echo "This executed"
This executed

conversely,if I want to only,excute the second command,if the first is unsuccessful.
We can do that,by a double pipe character.

[user@tcox1 ~]$ /bin/false ||  echo "This executed"
This executed


Another type of executing the command:

[user@tcox1 ~]$ /bin/false ;  echo "This executed"
This executed

it will execute the command number one and command number two.
This is the good example,when your upgrading or updating the system.

Note
For example:you made changes to the repositories and you want to update the caches.
every command,

[user@tcox1 ~]$  sudo yum update && sudo yum install telnet
complete

Note:
every command,will return a exit code:is one of two values is either 0 or 1.
0 is success.
1 is failure.

example:
[user@tcox1 ~]$ echo $?
0

0 means the success.

Going through the redirects:
I can use the pipe character to redirect from one place to another.
Ls –la | more
The ls –la was redirected to more.
list all the directories.

Note:
I can redirect the output,from the console or the stdout to a file:

ls -al  > output.txt

I take the content in the file and send it to the cpio.

[user@tcox1 ~]$ cpio -id  <  test.cpio

These all special characters are built in setup for the bash.






No comments:

Post a Comment