SlideShare a Scribd company logo
1 of 23
Shell Scripting and LINUX
commands useful at work:
Topics covered:
• What is shell scripting
• Types of Shell scripting
• Benefits of Shell scripting
• What is Bash Scripting
• Basic Bash Scripting
• Basic Linux Commands –
• Package installation
• LINUX commands
• Session monitoring LINUX commands
• NMAP usage and commands
• A shell script is a list of commands in a computer program that is
run by the UNIX shell which is a command line interpreter. A shell
script usually has comments that describe the steps. The different
operations performed by shell scripts are program execution, file
manipulation and text printing.
Types of Shell Scripting:
• Bash aka Bourne Again Shell
• CSH / C shell
• KSH or Korn Shell
• TCSH
Benefits of shell script
• Shell scripts don’t have to be boring:
• Most shell scripts are uninteresting. They don’t
show anything, nor have any colouring .
• Reusing:
• Why recurring the same statement in your shell
scripts every time? Develop an effective set of
functions and consist of that in your existing and
new shell scripts.
What is Bash scripting?
• Bash- is a type of interpreter that processes shell commands. A shell
interpreter takes commands in plain test format and calls OS services
to do something.
• A shell scripting is writing a program for the shell to execute and a
shell script is a file or program that shell will execute.
• Bash is one of many available UNIX shells. And is a
replacement/improvement of the original Borne-shell.
• Shell scripting is scripting in any shell,whereas bash scripting
specifically for Bash.
• Always available:
• Shell scripting can constantly be used, on each
program you encounter
• Readability:
• With a shell script, the possibility of creating
things really unreadable is much lower. Sure you
can use unique features of the shell, which
others don’t comprehend.
• e[ – Begin color changes
• Shell scripting is powerful:
Is is convenient, no compilation
required and helps almost every
single UNIX based system there is.
Merge it with the commonly
accessible tools like awk, grep and
sed and you have a great bias.
Setting Of PS1 variable: export PS1="e[0;32m[u@h W]$ e[0m"
• e[ – Begin color changes
• 0;32m – Specify the color code
• [u@h W]$ – This is the code for your normal BASH prompt (username@hostname Workingdirectory $)
• e[0m – Exit color-change mode
The first number in the color code specifies the typeface:
• 0 – Normal
• 1 – Bold (bright)
• 2 – Dim
• 4 – Underlined
The second number indicates the color you want:
• 30 – Black
• 31 – Red
• 32 – Green
• 33 – Brown
• 34 – Blue
• 35 – Purple
• 36 – Cyan
• 37 – Light gray
Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead of brown.

The second number indicates the color you want:
• 30 – Black
• 31 – Red
• 32 – Green
• 33 – Brown
• 34 – Blue
• 35 – Purple
• 36 – Cyan
• 37 – Light gray
Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if
you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead
of brown.
Basic Linux Commands:
• pwd- get the full path of the current
working directory
• cd – Navigate to the last directory you
were working in
• cd – or just cd Navigate to the current
user home directory
• cd .. Go to the parent directory of
current directory
• ls –l : List the files and directories in the current directory in long
format
• ls –ld dir-name: - List information about the directory dir-name
instead of its contents
• ls –a : List all the files including the hidden ones
• lf –F: Appends a symbol at the end of a file name to indicate its
type (* means executable , /means directory, @ means symbolic
link, = means socket, I mean named pipe, > means door)
• ls –lt : List the files sorted by last
modified time with most recently
modified files showing at the top
• ls –lh: List file sizes in human readable
format
• ls –lR : shows all subdirectories
recursively
• Tree : Will generate a tree
representation of the file system
starting from the current directory
• cp –p source destination- Will copy the file from source to destination . –p:
stands for preservation. It preserves the original attributes of file while
copying like file owner, timestamp , group , permissions
• cp –R source_directory destination_directory – Will copy source directory to
specified destination recursively
• mv file1 file2 : In Linux there is no rename command as such. Hence mv
moves/renames the file file1 to file2
• rm –i filename : Asks you before
every file removal for
confirmation.
• rm –R dir-name : Will remove
the directory dir-name
recursively
• rm –rf dir-name: Will remove
the directory dir recursively,
ignoring non-existent files
• rmdir dir-name : Will remove the
directoty dir-name , if it’s empty.
• mkdir dir-name : Create a directory
dir-name
• mkdir –p dir-name/dir-name :
Create a directory hierarchy
• touch filename: Create a filename,
if it doesn’t exist, otherwise change
the timestamp of the file to current
time.
Basic Linux Commands :
• File/ Directory permissions and groups:
• chmod <specification> filename: Change the
file permissions , specifications
• chmod –R <specification> dir-name: Change
the perimissions of a directory recursively.
• chmod go=+r myfile : Add read permission for
the owner and the group
• chmod a+rwx myfile : allow all users to read , write or execute myfile.
• chmod go-r myfile: Remove read permissions from the group and others
• chown owner1 filename – change ownership of a file to user owner1
• chgrp grp_owner_filename: change primary group ownership of directory
dir-name grp_owner.
• chgrp-R grp_owner dir-name: change primary group ownership of directory
dir-name to group grp_owner
Basic Bash scripting:
• Variables:
NAME=“John”
echo $NAME
echo “$NAME”
echo “${NAME}”
• String quotes:
NAME=“John”
echo “Hi $NAME”
echo ‘Hi $NAME’
• Shell Execution:
echo “I’m in $(pwd)”
echo “I’m in `pwd`”
• Functions:
get_name(){
echo “John”
}
echo “You are $(get_name)”
Basic Bash Scripting:
• Conditional execution:
git commit && git push
git commit || echo
• Conditions:
if [[-z “$string”]];
then
echo “String is empty”
elif [[-n “$string”]]; then
Echo “String is not empty”
fi
• Brace expansion:
• echo {A,B}.js
• {A,B} – Same as A B
• {A, B}.js – Same as A.js B.js
• {1…5}- Same as 1 2 345
• Strict mode:
• Set –euo pipefail
• IFS=$’nt’
Useful commands with LINUX machine:
1. ss: used to dump socket statistics.
• [root@ins-qasvr-common05 Automation]# ss -a | grep ssh
• LISTEN 0 128 :::ssh :::*
• LISTEN 0 128 *:ssh *:*
• ESTAB 0 0 172.31.201.214:37118 172.31.146.95:ssh
• ESTAB 0 0 172.31.201.214:51378 172.31.144.92:ssh
• [root@ins-qasvr-common05 Automation]#
We have active SSH connections from 172.31.146.95
• 2) Last : Searches back through
the file /var/log/wtmp (or the
file designated by the -f flag) and
displays a list of all users logged
in (and out) since that file was
created.
• 3) W: Displays information
about the users currently on
the machine, and their
processes
[root@node3 ~]# last -a | grep -i still
deepak pts/1 Fri May 31 16:58 still logged in 10.0.2.31
root pts/2 Fri May 31 16:50 still logged in 10.0.2.30
root pts/0 Fri May 31 09:17 still logged in 10.0.2.2
[root@node3 ~]# w
17:01:41 up 7:44, 3 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.2.2 09:17 9:41 0.31s 0.00s less -s
deepak pts/1 10.0.2.31 16:58 3:06 0.03s 0.03s -bash
root pts/2 10.0.2.30 16:50 5.00s 0.07s 0.02s w
IP address to find who using the SSH server
• echo $SSH_CLIENT | awk ‘{print$1}’
• Provides the IP address of the user who logged in through SSH
• Pinky
• Command lists the login users with IP details
Environment variables:
• HOME – users home directory
• PATH – colon separated list of directories used by the shell when
searching for programs to run when typed on the command line
• MAIL – users mail filename, which is used to check new mail
• MAILPATH – colon-separated list of filenames used to check for new
mail
• MAILCHECK – how often to check for new mail
• PS1 – user’s shell prompt displayed on the command line
• PS2 – secondary shell prompt when waiting for more input from the
user
• CDPATH – colon-separated list of directories used by the cd command
when searching , for the directory specified to change into
• SHELL – name of the current user’s shell
• DISPLAY- specify the display name for X WINDOWS applications
• $$ - process ID of the current shell
• $! – process ID of the most recent background
• $? – return code from the previously executed command
Setting Shell variables:
• Export command is used to
export variable names to new
sub-shells. The user can also
create his/her own variables by
setting them to an initial value.
• $PATH = /bin:/usr/sbin:
/usr/etc:/usr/ucb:/usr/X11R6/bin
• PS1 = ‘Command>’
• Command > MYNAME = “John
Smith”
• Command > echo $MYNAME
• Command> export PATH PS1
Finding files and directories with grep utility:
• find . –name resume.txt –print
• grep –ilnv search-pattern file-list: search-pattern is the word being
searched for and file-list is the list of files on which to perform the
search.
Network Administration related commands:
• Host address – IP address of your system ; it has a network part to identify the network you
are on and a host part to identify your own system.
• Network address - IP address of your network
• Broadcast address – IP address for sending messages to all hosts of your network at once
• Gateway address – IP address of your gateway system if you have one
• Domain nameserver addresses: IP addresses of domain nameservers that your network uses
• Netmask – Network part of your host IP address set to 255 with host part set to 0
File:
• /etc/hosts: Associates hostnames with IP addresses
• /etc/networks: Associate's domain names with network addresses
• /etc/rc.d/init.d/inet : Script to configure your ethernet interface when you boot up
• /etc/host.conf: List resolver options
• /etc/hosts: Lists domain names for remote hosts with their IP addresses
• /etc/resolv.conf: Lists domain nameserver names, IP addresses and domain names where remote hosts may be located.
• /etc/protocols: Lists protocols available on your system
• /etc/services: Lists available network services such as ftp and telnet
• /etc/hostname: Holds the name of your system

More Related Content

Similar to Shell Scripting and Linux Commands

Linux week 2
Linux week 2Linux week 2
Linux week 2Vinoth Sn
 
Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Chander Pandey
 
Presentation for RHCE in linux
Presentation  for  RHCE in linux Presentation  for  RHCE in linux
Presentation for RHCE in linux Kuldeep Tiwari
 
Online Training in Unix Linux Shell Scripting in Hyderabad
Online Training in Unix Linux Shell Scripting in HyderabadOnline Training in Unix Linux Shell Scripting in Hyderabad
Online Training in Unix Linux Shell Scripting in HyderabadRavikumar Nandigam
 
linux commands that might be helpful.pptx
linux commands that might be helpful.pptxlinux commands that might be helpful.pptx
linux commands that might be helpful.pptxMeghrajPatil11
 
PowerPoint_merge.ppt on unix programming
PowerPoint_merge.ppt on unix programmingPowerPoint_merge.ppt on unix programming
PowerPoint_merge.ppt on unix programmingPriyadarshini648418
 
Linux operating system by Quontra Solutions
Linux operating system by Quontra SolutionsLinux operating system by Quontra Solutions
Linux operating system by Quontra SolutionsQUONTRASOLUTIONS
 
proj2-the UNIX SYSTEM.ppt
proj2-the UNIX SYSTEM.pptproj2-the UNIX SYSTEM.ppt
proj2-the UNIX SYSTEM.pptssuserc95683
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!Zubair Nabi
 
BASICS OF LINUX OPERATING SYSTEM.pptx
BASICS OF LINUX OPERATING SYSTEM.pptxBASICS OF LINUX OPERATING SYSTEM.pptx
BASICS OF LINUX OPERATING SYSTEM.pptxAlkaYadav79
 

Similar to Shell Scripting and Linux Commands (20)

Linux week 2
Linux week 2Linux week 2
Linux week 2
 
redhat_by_Cbitss.ppt
redhat_by_Cbitss.pptredhat_by_Cbitss.ppt
redhat_by_Cbitss.ppt
 
Linux Basics.pptx
Linux Basics.pptxLinux Basics.pptx
Linux Basics.pptx
 
Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01Linuxtraining 130710022121-phpapp01
Linuxtraining 130710022121-phpapp01
 
Presentation for RHCE in linux
Presentation  for  RHCE in linux Presentation  for  RHCE in linux
Presentation for RHCE in linux
 
Online Training in Unix Linux Shell Scripting in Hyderabad
Online Training in Unix Linux Shell Scripting in HyderabadOnline Training in Unix Linux Shell Scripting in Hyderabad
Online Training in Unix Linux Shell Scripting in Hyderabad
 
linux commands that might be helpful.pptx
linux commands that might be helpful.pptxlinux commands that might be helpful.pptx
linux commands that might be helpful.pptx
 
Group13
Group13Group13
Group13
 
PowerPoint_merge.ppt on unix programming
PowerPoint_merge.ppt on unix programmingPowerPoint_merge.ppt on unix programming
PowerPoint_merge.ppt on unix programming
 
Linuxnishustud
LinuxnishustudLinuxnishustud
Linuxnishustud
 
Linux operating system by Quontra Solutions
Linux operating system by Quontra SolutionsLinux operating system by Quontra Solutions
Linux operating system by Quontra Solutions
 
Linux And perl
Linux And perlLinux And perl
Linux And perl
 
Linux Basics
Linux BasicsLinux Basics
Linux Basics
 
cisco
ciscocisco
cisco
 
3. intro
3. intro3. intro
3. intro
 
proj2-the UNIX SYSTEM.ppt
proj2-the UNIX SYSTEM.pptproj2-the UNIX SYSTEM.ppt
proj2-the UNIX SYSTEM.ppt
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux commands
Linux commandsLinux commands
Linux commands
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
 
BASICS OF LINUX OPERATING SYSTEM.pptx
BASICS OF LINUX OPERATING SYSTEM.pptxBASICS OF LINUX OPERATING SYSTEM.pptx
BASICS OF LINUX OPERATING SYSTEM.pptx
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Shell Scripting and Linux Commands

  • 1. Shell Scripting and LINUX commands useful at work:
  • 2. Topics covered: • What is shell scripting • Types of Shell scripting • Benefits of Shell scripting • What is Bash Scripting • Basic Bash Scripting • Basic Linux Commands – • Package installation • LINUX commands • Session monitoring LINUX commands • NMAP usage and commands
  • 3. • A shell script is a list of commands in a computer program that is run by the UNIX shell which is a command line interpreter. A shell script usually has comments that describe the steps. The different operations performed by shell scripts are program execution, file manipulation and text printing.
  • 4. Types of Shell Scripting: • Bash aka Bourne Again Shell • CSH / C shell • KSH or Korn Shell • TCSH
  • 5. Benefits of shell script • Shell scripts don’t have to be boring: • Most shell scripts are uninteresting. They don’t show anything, nor have any colouring . • Reusing: • Why recurring the same statement in your shell scripts every time? Develop an effective set of functions and consist of that in your existing and new shell scripts.
  • 6. What is Bash scripting? • Bash- is a type of interpreter that processes shell commands. A shell interpreter takes commands in plain test format and calls OS services to do something. • A shell scripting is writing a program for the shell to execute and a shell script is a file or program that shell will execute. • Bash is one of many available UNIX shells. And is a replacement/improvement of the original Borne-shell. • Shell scripting is scripting in any shell,whereas bash scripting specifically for Bash.
  • 7. • Always available: • Shell scripting can constantly be used, on each program you encounter • Readability: • With a shell script, the possibility of creating things really unreadable is much lower. Sure you can use unique features of the shell, which others don’t comprehend. • e[ – Begin color changes • Shell scripting is powerful: Is is convenient, no compilation required and helps almost every single UNIX based system there is. Merge it with the commonly accessible tools like awk, grep and sed and you have a great bias.
  • 8. Setting Of PS1 variable: export PS1="e[0;32m[u@h W]$ e[0m" • e[ – Begin color changes • 0;32m – Specify the color code • [u@h W]$ – This is the code for your normal BASH prompt (username@hostname Workingdirectory $) • e[0m – Exit color-change mode The first number in the color code specifies the typeface: • 0 – Normal • 1 – Bold (bright) • 2 – Dim • 4 – Underlined The second number indicates the color you want: • 30 – Black • 31 – Red • 32 – Green • 33 – Brown • 34 – Blue • 35 – Purple • 36 – Cyan • 37 – Light gray Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead of brown.  The second number indicates the color you want: • 30 – Black • 31 – Red • 32 – Green • 33 – Brown • 34 – Blue • 35 – Purple • 36 – Cyan • 37 – Light gray Additionally, if you combine the bright option with a color code, you get a lighter version of that color. For example, if you use color code 1;32, you would get light green instead of the normal green. If you use 1;33, you get yellow instead of brown.
  • 9. Basic Linux Commands: • pwd- get the full path of the current working directory • cd – Navigate to the last directory you were working in • cd – or just cd Navigate to the current user home directory • cd .. Go to the parent directory of current directory • ls –l : List the files and directories in the current directory in long format • ls –ld dir-name: - List information about the directory dir-name instead of its contents • ls –a : List all the files including the hidden ones • lf –F: Appends a symbol at the end of a file name to indicate its type (* means executable , /means directory, @ means symbolic link, = means socket, I mean named pipe, > means door)
  • 10. • ls –lt : List the files sorted by last modified time with most recently modified files showing at the top • ls –lh: List file sizes in human readable format • ls –lR : shows all subdirectories recursively • Tree : Will generate a tree representation of the file system starting from the current directory • cp –p source destination- Will copy the file from source to destination . –p: stands for preservation. It preserves the original attributes of file while copying like file owner, timestamp , group , permissions • cp –R source_directory destination_directory – Will copy source directory to specified destination recursively • mv file1 file2 : In Linux there is no rename command as such. Hence mv moves/renames the file file1 to file2
  • 11. • rm –i filename : Asks you before every file removal for confirmation. • rm –R dir-name : Will remove the directory dir-name recursively • rm –rf dir-name: Will remove the directory dir recursively, ignoring non-existent files • rmdir dir-name : Will remove the directoty dir-name , if it’s empty. • mkdir dir-name : Create a directory dir-name • mkdir –p dir-name/dir-name : Create a directory hierarchy • touch filename: Create a filename, if it doesn’t exist, otherwise change the timestamp of the file to current time.
  • 12. Basic Linux Commands : • File/ Directory permissions and groups: • chmod <specification> filename: Change the file permissions , specifications • chmod –R <specification> dir-name: Change the perimissions of a directory recursively. • chmod go=+r myfile : Add read permission for the owner and the group • chmod a+rwx myfile : allow all users to read , write or execute myfile. • chmod go-r myfile: Remove read permissions from the group and others • chown owner1 filename – change ownership of a file to user owner1 • chgrp grp_owner_filename: change primary group ownership of directory dir-name grp_owner. • chgrp-R grp_owner dir-name: change primary group ownership of directory dir-name to group grp_owner
  • 13. Basic Bash scripting: • Variables: NAME=“John” echo $NAME echo “$NAME” echo “${NAME}” • String quotes: NAME=“John” echo “Hi $NAME” echo ‘Hi $NAME’ • Shell Execution: echo “I’m in $(pwd)” echo “I’m in `pwd`” • Functions: get_name(){ echo “John” } echo “You are $(get_name)”
  • 14. Basic Bash Scripting: • Conditional execution: git commit && git push git commit || echo • Conditions: if [[-z “$string”]]; then echo “String is empty” elif [[-n “$string”]]; then Echo “String is not empty” fi • Brace expansion: • echo {A,B}.js • {A,B} – Same as A B • {A, B}.js – Same as A.js B.js • {1…5}- Same as 1 2 345 • Strict mode: • Set –euo pipefail • IFS=$’nt’
  • 15. Useful commands with LINUX machine: 1. ss: used to dump socket statistics. • [root@ins-qasvr-common05 Automation]# ss -a | grep ssh • LISTEN 0 128 :::ssh :::* • LISTEN 0 128 *:ssh *:* • ESTAB 0 0 172.31.201.214:37118 172.31.146.95:ssh • ESTAB 0 0 172.31.201.214:51378 172.31.144.92:ssh • [root@ins-qasvr-common05 Automation]# We have active SSH connections from 172.31.146.95
  • 16. • 2) Last : Searches back through the file /var/log/wtmp (or the file designated by the -f flag) and displays a list of all users logged in (and out) since that file was created. • 3) W: Displays information about the users currently on the machine, and their processes [root@node3 ~]# last -a | grep -i still deepak pts/1 Fri May 31 16:58 still logged in 10.0.2.31 root pts/2 Fri May 31 16:50 still logged in 10.0.2.30 root pts/0 Fri May 31 09:17 still logged in 10.0.2.2 [root@node3 ~]# w 17:01:41 up 7:44, 3 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 10.0.2.2 09:17 9:41 0.31s 0.00s less -s deepak pts/1 10.0.2.31 16:58 3:06 0.03s 0.03s -bash root pts/2 10.0.2.30 16:50 5.00s 0.07s 0.02s w
  • 17. IP address to find who using the SSH server • echo $SSH_CLIENT | awk ‘{print$1}’ • Provides the IP address of the user who logged in through SSH • Pinky • Command lists the login users with IP details
  • 18. Environment variables: • HOME – users home directory • PATH – colon separated list of directories used by the shell when searching for programs to run when typed on the command line • MAIL – users mail filename, which is used to check new mail • MAILPATH – colon-separated list of filenames used to check for new mail • MAILCHECK – how often to check for new mail • PS1 – user’s shell prompt displayed on the command line
  • 19. • PS2 – secondary shell prompt when waiting for more input from the user • CDPATH – colon-separated list of directories used by the cd command when searching , for the directory specified to change into • SHELL – name of the current user’s shell • DISPLAY- specify the display name for X WINDOWS applications • $$ - process ID of the current shell • $! – process ID of the most recent background • $? – return code from the previously executed command
  • 20. Setting Shell variables: • Export command is used to export variable names to new sub-shells. The user can also create his/her own variables by setting them to an initial value. • $PATH = /bin:/usr/sbin: /usr/etc:/usr/ucb:/usr/X11R6/bin • PS1 = ‘Command>’ • Command > MYNAME = “John Smith” • Command > echo $MYNAME • Command> export PATH PS1
  • 21. Finding files and directories with grep utility: • find . –name resume.txt –print • grep –ilnv search-pattern file-list: search-pattern is the word being searched for and file-list is the list of files on which to perform the search.
  • 22. Network Administration related commands: • Host address – IP address of your system ; it has a network part to identify the network you are on and a host part to identify your own system. • Network address - IP address of your network • Broadcast address – IP address for sending messages to all hosts of your network at once • Gateway address – IP address of your gateway system if you have one • Domain nameserver addresses: IP addresses of domain nameservers that your network uses • Netmask – Network part of your host IP address set to 255 with host part set to 0
  • 23. File: • /etc/hosts: Associates hostnames with IP addresses • /etc/networks: Associate's domain names with network addresses • /etc/rc.d/init.d/inet : Script to configure your ethernet interface when you boot up • /etc/host.conf: List resolver options • /etc/hosts: Lists domain names for remote hosts with their IP addresses • /etc/resolv.conf: Lists domain nameserver names, IP addresses and domain names where remote hosts may be located. • /etc/protocols: Lists protocols available on your system • /etc/services: Lists available network services such as ftp and telnet • /etc/hostname: Holds the name of your system