What is Linux

                 Linux is a Kernel , a free open-source operating system based on UNIX. Linus Torvalds originally created the Linux (1991 ) with the help of developers from around the world.Linux is

 Free.
 Unix Like.
 Open source.
 Network Operating System.

                     Strictly saying that Linux is a kernel. A kernel provides the access to the  server hardware as well as control access to the other resources such as
 Files and data.
 Running Programs
 Loading program in to memory
 Network
 Security and firewall
 Other resources etc..

                    The Kernel decides who will use the resources , for how long and when . We can update our kernel to the latest one by downloading the kernel from official website . The main thing is that we can’t meet our all necessaries only with the Linux kernel, we also need a lot of application such as text editors, email clients, web browsers. office applications, software development tools and compilers etc…  These all applications and kernel combined and makes a Linux distribution.

 


 A Linux distribution ( often called as distro) is an operating system built on top of the Linux kernel and often around package management system. Popular Linux distro includes RHEL, CentOS, Ubuntu, Oracle

Linux, SUSE Enterprise Linux, Fedora, open SUSE, Slackware, Arch Linux, Chromium OS, Linux Mint, etc….




                    Corporates and small business users needs support while running Linux. So companies such as Red-Hat or Novell provide Linux tech support as their products.


Linux Kernel



      As we know the kernel is the heart of the Linux operating system. It manages the resources includes

 File management
 Multitasking
 Memory Management
 I/O Management
 Process Management
 Device Management
 Networking Support includes ipv4 aND IPV6
 Advanced features such as virtual memory, shared libraries, demand loading,
etc..


Why Linux Shell


       Computers are not able to understand any languages other than binary language. ie, zeros and ones. In earlier days programmers used this 0’s and 1’s to make the programs which is absolutely very difficult to write and read by human beings. But now shell do the translation of high level language to binary language which is very easy for programmers as well as sysadmins.
               Several shells are default included in Linux distro, such as BASH ( Bourne-Again-Shell ) , CSH ( C Shell), KSH ( Korn Shell ), POSIX, TCSH .

Short Cuts in CLI

Ctrl + L   – Clear the screen.
Ctrl + w   – Delete the word (put cursor at end of command and trigger).
Ctrl + u   – Delete the entire line (put cursor at end of command and
trigger).
Ctrl + r   – To recall the matched command which triggered before .
Ctrl + c   – To cancel currently running commands
Esc + t    – To swap the last two words before the cursor
Ctrl + a   – To move the cursor at the beginning of the command .
Ctrl + u   – To move the cursor at the end of the command.
Alt + u    – This will convert the word to uppercase starting from the cursor.
Alt + l     – This will convert the word to lowercase starting from the cursor.
>filename – This will truncate the file with filename as filename.
Ctrl + c   –  This will terminate the currently running process, ie , sending SIGINIT signal to the      currently running process.

Ctrl + z   – This will suspend the currently running process.




Introduction about Scripting

                      Whenever we are  doing the same task again and again , we can think about automation of that job where we can simply sit and watch the job status and the job will do by some text files (Scripts).  The scripts are easy to use,interactive and can debug the errors ,it will saves our sysadmins time  etc.. . At the same time it have disadvantages also includes slow execution speed, each running scripts creates a separate process id, compatibility issue in different platforms .

                       The main three scripting languages which is popular in IT industry are Bash, Perl and Python. Bash can be used to for some small tasks where we might need some loops to do something repeatedly . Perl is good to do anything some kind of text processing or file processing, especially if it’s a one off. Just do a dirty nasty perl script and be done with it. Python is something you might want to do again or something very like it. Then at least you have a chance of being able to reuse the script. Here I’m going to explain about bash scripting.


                                                        



Bash Scripting

                                            Bash scripting is one of the easiest type of scripting to learn. Normally shells are interactive, it will accept commands from us and execute them properly. While we are making our bash script, the first line will look like as follows
 #! /bin/bash
     
The “#!” is named as shebang , means this indicates the interpreter for execution under UNIX/Linux.Here our interpreter is /bin/bash.If we do not mention the shebang , then the default interpreter will be /bin/sh . But it is recommend  to mention shebang. All scripts under Linux execute using the interpreter mentioned in the first line followed by the ‘#!’ . The Bash scripts are starting with /bin/bash ( Assuming that bash has been installed in /bin).

#!/usr/bin/perl
#!/usr/bin/python

The first line with #! is the shebang line and if we want to make any comments about our scripts, we can put in the scripts after # ,this comments will help the other admins to understand our code, logic, and helps them to modify the script if necessary. If we want to put multiple comments, then put the entry like this .

#! /bin/bash
<<COMMENT 1
This script is to shows the commenting
Testing script
comments end
COMMENT 1

./test.sh -xv   –  To run the script in debugging mode ,we can put x. So it will print all standard outputs and errors while running the scripts.otherwise we can mention the same in shebang also like #! /bin/bash -x. Bash shell offers more debugging options to on/off using set options. set -x = Display commands and their arguments which they are executing.
set -n = Read commands,but do not execute them.
set -v = Display shell input lines as they are read.
 To turn OFF debugging mode use set +x, set +n, set +v.

 


   System variables :    We can use shell variables to store to store data.
  #set  or #env   or #printenv        –   To see the current system variables.
  # echo $HOSTNAME               –   To display the variables output

                       Do not use space while assigning variables. It should be something like jo=123. Variable names are case sensitive. ie jo are Jo are different variables.  We can define null variables ,means it doesn’t have value at the time of definining . eg jo=”” or jo= .
To print the value use $jo or $”jo”   or printf “%s\n” “jo”

The backslash (\) will alters the special meaning of the ‘ and ” . ie it cancels the special meaning of the next character. eg.
#name=text_file
#echo “file is $name” = ==> file is text_file
#echo “file is “$name””= ==> file is text_file
#echo “file is \”$name\”” = ==> file is “text_file”  ( Here it cancelled the meaning of “” and prints it as it is) .








 export :    The export command is useful to keep our variables permanently in the child process also. Eg .
#vehi=car
#echo “$vehi”
car
#bash    –    starting another bash terminal.
#echo “$vehi”
Now it will print a blank line because the variable is not defined in this bash terminal.To avoid this type of drawbacks we can use export command. Export command will make the variable acroisss all the child process.
#export “vech=car”

If we want to make a permanent entry , then
vi ~/.bash_proflle  and provide the entry over here.



   unset :    If we have unwanted variables declared and it creating issues
during scripting, then we can remove the variable value using unset command
#vech=jeep
#echo "$vech"
 jeep
#unset vech
#echo "$vech"
This will not shows the value jeep.


  printf  :         This command will format and  display the data on the screen. We can use this tool to display . eg. printf “\n\n\n%4s ERROR\t%4s alert\n\n\n\n”  ,here the %4s – will give 4 blank space.



There are 3 types of quoting in scripting
    1) ” ”  – Double quots will protect everything except $,”,\.
    2) ‘ ‘  – It is used to turn off the special meaning.
    3) \   – The backslash change the special meaning of the characters.

command                                  output using ” ”                                                 output using ‘ ‘                                                                                             
#echo “$SHELL”                      /bin/bash                                                                 $SHELL
#echo “/etc/*.conf”                   /etc/*.conf                                                             /etc/*.conf   
#echo “Today is $(date)”          Today is Wed Jun 18 15:05:45 IST 2014               Today is
                                                                                                                                   $(date)
 If we are using back slash, eg
#echo “Path is \$PATH”    =  Path is $PATH
#echo “Path is $PATH”     =  Path is
/usr/lib64/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/local/hadoop/bin:/usr/local/hadoop/sbin:/root/bin:/usr/local/hadoop/bin:/usr/local/hadoop/sbin
#FILE=”/etc/resolv.conf” #echo “File is \”$FILE\” ”   =      File is
“/etc/resolv.conf”



User input via keyboard

  read :    This can be use to to take input from keyboard.  eg.
#read -p “Enter your name :” name1 name2   – here name1 for first name and
 name2 for second name. read -p “propt” will display the prompt as it is.
#echo “$name1” “$name2”
 sam dennis
          To make the use of read command more effectively use man read.



 Arithmetic expression in bash script

          we can do the arithmetic expression also in the bash script. The syntax is something like
#echo $((expression)””
eg.# echo “$((568+756-4))”
1320

Sample script using the arithmetic expressions

 #! /bin/bash
  2 clear
  3 read -p “Enter the number to get the sum : ” num1 num2
  4 echo -e “\n”
  5 ans=”$(($num1+$num2))”
  6 echo ” the sum of “$num1” and “$num2” are= “$ans””
  7 echo -e “\n”
  8 printf “%s” “Thanks for using software calculator”
  9 echo -e “\n\n\n\n\n”


declare command

               Now we know how to declare string variables, like this we can assign integer variables also, for giving integer variable we are using declare command. The syntax will be like this #declare -i ann=”20176″.
For more details look at the man page

   Curly braces
                    Curly braces can play a major role in bash scripting. following example shows us how much it is helpful in scripting.

# echo file{1..6}.txt
output ==>  file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt

#ls /etc/{hosts,yum.conf,yp.conf,sysconfig}
output ==>  /etc/hosts  /etc/yp.conf  /etc/yum.conf /etc/sysconfig


   Wildcards
                      Bash support three wildcards, these are
* , ? , and […]
          The * wildcard we are commonly using everyday and we need to
familiarize with the other two also.
eg
#ls -ltrh /etc/*.conf
The ? wildcard can also use in this same case, Compare the two examples
provided below.

# ls -ltrh /etc/???.conf
-rw——- 1 root root  688 Dec 19  2006 /etc/amd.conf
-rw-r–r– 1 root root 1.3K Jan  4  2008 /etc/smi.conf
-rw-r–r– 1 root root  153 Dec 21  2011 /etc/esd.conf
-rw-r–r– 1 root root  876 Mar 23 17:33 /etc/hba.conf
-rw-r–r– 1 root root  425 May  3 10:10 /etc/yum.conf
-rw-r–r– 1 root root 2.0K May  6 19:51 /etc/ntp.conf


## ls -ltrh /etc/??.conf
-rw-r–r– 1 root root 585 Jun 17  2011 /etc/yp.conf

             From these example we found that the number of ‘?’ is playing a major roles. If we are putting ‘*’ , then it will print all irrespective of the character numbers.

        Just see the example for ‘[ …]’ wild card also
# ls [abc].conf
a.conf  b.conf  c.conf

ie, It lists the files which are containing letter a , b and c. It will not print files like aaa.conf,abc.conf etc, It will take only the first character and will display accordingly.

     alias
                         An alias is nothing, it is simply a shortcut to commands. The syntax to make alias is #alias name=’command’
# alias j=’reset’ , this will asign reset command to value j, if we put j in # prompt,it will will reset our terminal.  To remove this alias, we can use unalias. eg #unalias j.

                   In some cases we required to add the aliases permanently in the system or for all users. In this case we can add the entry in /etc/bashrc or /etc/profile.d/useralias.sh file. The /etc/profile.d useralias.sh is not default present in system , we need to create it.

if structure

              We can use if condition in bash as the four methods listed below.

if condition         OR     if test var == value     OR      if test -f /file/exists    OR     if [condition]
then                               then                                        then                                      then
command 1                   command                               command                              command
command 2                   fi                                            fi                                           fi
command n
fi


#[[ $(ls -lrt a 2> /dev/null) ]] && echo “Right” || echo “Wrong”

Logical AND &&  –  Run the command only if the first run successfully
Logical OR ||   –  Run the command only if the first not run successfully.

test command
test condition && true-command || false-command
eg . #test 5 != 2 && echo “large”||echo “wrong”       ==> Here if the condition is true, then it will execute the command next to &&, if the condition is not matching, then it will execute the command next to || . Take man test for more information’s. #test -f /etc/my.cnf && echo -e “\n MySQL config file is here” || echo -e “\n where is my.cnf file ”   ==> Here it will check whether the file is present or not.


Logical OR ||

          Logical OR is a Boolean operator, It execute commands or shells or functions based on the exit status of the another command.
command1 || command2
 Here command2 will execute only if command1 execute a non zero exit status. For example
#cat /etc/shadow 2>> /dev/null || echo ” This user is not eligible for open shadow file”   .
Output ==> This user is not eligible for open shadow file

Logical NOT !

         Logical NOT is a Boolean operator, which is used to test expression is true or not. The syntax for ! will be like
! expression       or       [ ! expression ]     or      if test ! condition         or       if  [ ! condition ]
                                                            then                              then
                                                            command1                          command1
                                                            command2                          command2
                                                            fi                                fi

Eg.
#test ! -d pluto && echo ” Directory pluto is not found, Use mkdir pluto command”
output ==>  Directory pluto is not found, Use mkdir pluto command

For file use test ! -f , for directory use -d.



          if…elif…else…fi  allows the script to have various possibilities and conditions . This will be useful when we want to compare multiple values.
eg.

#!/bin/bash
  2 read -p “Enter the number :” n
  3 if [ $n -gt 5 ]
  4 then
  5 echo “grater than 5”
  6 elif [ $n -lt 5 ];then
  7 echo “less than 5”
  8 elif [ $n -eq 5 ];then
  9 echo “value is equal to 5”
 10 else
 11 echo ” This is not a valid number”
 12 fi


 exit status

            Each Linux commands returns a status when it terminates normally or abnormally, exit status 0 means the command executed successfully, if any error occurs it will give an exit status from the value from 1 to 255. We can use this exit status inside the script to make some sort of another actions.
                     From the terminal we can simply put echo $? command to show the exit status of the last command. If we want to store the exit status inside the script, then assign the status to a variable like as follows
rm -rf /var/log/messages
exit_stat=$?
echo “$exit_stat
                             We can put exit N in bash scripts and based on the value of this N (0 to ..) we can make some decisions. Eg.
#!/bin/bash
echo “This is a test.”
# Terminate our shell script with success message
exit 0


Bash Loops

                   Bash shell can execute particular instructions again and again ,until the condition satisfies. A group of instructions that is executed is repeatedly is called bash loops.

The syntax for for loop is as follows

for var in item1 item2 … itemN
do
command1
command2
….

commandN
done

 OR
for var in $filename
OR
for var in $(linux-command)
OR
for (( EXPR1; EXPR2; EXPR3 ))


The while loop is useful where we need to execute the commands repeatedly . The syntax will be
while [ condition ]
do
command1
command2
….
….
commandN
done

Eg.
#!/bin/bash
n=1
while [ $n -le 5 ]
do
echo “Welcome $n times.”
n=$(( n+1 )) # increments $n
done