SlideShare a Scribd company logo
Linux Shell script (Bash) Basics
PART - 1
1
2
 A script is a list of system commands stored in a
file.
 Steps to write a script :-
 Use any editor like vi or vim
 chmod permission your-script-name.
 Examples:-

$ chmod +x <filename.sh>

$ chmod 755 <filename.sh>
 Execute your script as:
 $ bash filename.sh
 $ bash fileneme.sh
 $ ./filename.sh
3
 My first shell script
clear
 echo “hello world“
 $ ./first
 $ chmod 755 first
 $ ./first
 Variables in Shell:
 In Linux (Shell), there are two types of variable:
 (1) System variables :
 (2) User defined variables :
4
 $ echo $USERNAME
 $ echo $HOME
 User defined variables :
 variable name=value
 Examples:
 $x = 10
 echo Command:
 echo command to display text
 arithmetic operations
 Syntax:
expr op1 math-operator op2
Examples:
$ expr 10 + 30
$ expr 20 – 10
$ expr 100 / 20
$ expr 200 % 30
$ expr 100 * 30
$ echo `expr 60 + 30`
5
Renaming files
mv test1 test2
Deleting files
rm -i test1
Creating directories
mkdir dir3
Deleting directories
rmdir dir3
6
 $ ps
PID TTY TIME CMD
 $ ps -ef
UID PID PPID C STIME TTY TIME CMD
 $ ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME
CMD
 $ ps -efH
UID PID PPID C STIME TTY TIME CMD
7
Creating files:
$ touch test1
$ ls -il test1
Copying files:
cp source destination
cp test1 test2
Linking files:
There are two different types of file links in Linux:
a. A symbolic, or soft, link
b. A hard link
8
" Double
Quotes
Double Quotes" -
Anything enclose in
double quotes removed
meaning of that
characters (except  and
$).
' Single quotes 'Single quotes' - Enclosed in
single quotes remains
unchanged.
` Back quote `Back quote` - To execute
command
9
 Pipes:
who | wc –l
 Reading from Files:
$ read message
$ echo $message
Read command to read lines from files
 Command substitution:
Var=`date`
Var=$(date)
 Background Processes:
 ls -R /tmp &
1
0
while read ip name alias
do
if [ ! -z “$name” ]; then
# Use echo -en here to suppress ending the line;
# aliases may still be added
echo -en “IP is $ip - its name is $name”
if [ ! -z “$aliases” ]; then
echo “ Aliases: $aliases”
else
# Just echo a blank line
echo
fi
fi
done < /etc/hosts
1
1
Stopping processes
kill pid
disk space
$ df
$ df –h
Disk usages:
$ du
Commands:
$ cat file1
$ sort file1
$ cat file2
$ sort file2
$ sort -n file2
1
2
 grep [options] pattern [file]
 The grep command searches either the input
or the file you specify for lines that contain
characters that match the specified pattern.
The output from grep is the lines that contain
the matching pattern.
1
3
$ cat random.sh
#!/bin/bash
MIN=200
MAX=500
let “scope = $MAX - $MIN”
if [ “$scope” -le “0” ]; then
echo “Error - MAX is less than MIN!”
fi
for i in `seq 1 10`
do
let result=”$RANDOM % $scope + $MIN”
echo “A random number between $MIN and $MAX is $result”
Done
$ ./random.sh
1
4
$ cat hypotenuse.sh
#!/bin/sh
# calculate the length of the hypotenuse of a Pythagorean
triangle
# using hypotenuse^2 = adjacent^2 + opposite^2
echo -n “Enter the Adjacent length: “
read adjacent
echo -n “Enter the Opposite length: “
read opposite
osquared=$(($opposite ** 2)) # get o^2
asquared=$(($adjacent ** 2)) # get a^2
hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2
hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc`
# bc does sqrt
echo “The Hypotenuse is $hypotenuse”
1
5
 There are two types of environment variables in the
bash shell
 Global variables
 Local variables
1
6
 An array is a variable that can hold multiple values.
 To set multiple values for an environment variable, just list
them in parentheses, with each value
 separated by a space:
 $ mytest=(one two three four five)
 $
 Not much excitement there. If you try to display the array
as a normal environment variable,
 you’ll be disappointed:
 $ echo $mytest
 one
 $
 Only the first value in the array appears. To reference an
individual array element, you must use
 a numerical index value, which represents its place in the
array. The numeric value is enclosed in
 square brackets:
 $ echo ${mytest[2]}
 three
 $
1
7
$ date ; who
$ chmod u+x test1
$ ./test1
$ echo This is a test
This is a test
$ echo Let’s see if this’ll work
Lets see if thisll work
$
1
8
One of the most useful features of shell scripts is the lowly
back quote character, usually called the
backtick (`) in the Linux world.
You must surround the entire command line command with
backtick characters:
testing=`date`
$ cat test5
#!/bin/bash
# using the backtick character
testing=`date`
echo "The date and time are: " $testing
$
1
9
 Output redirection
The most basic type of redirection is sending output from a
command to a file. The bash shell uses the greater-than
symbol for this:
command > outputfile
Input redirection
Input redirection is the opposite of output redirection
The input redirection symbol is the less-than symbol (<):
command < inputfile
2
0
$ expr 1 + 5
6
The bash shell includes the expr command to stay
compatible with the Bourne shell; however, it
also provides a much easier way of performing mathematical
equations
$ var1=$[1 + 5]
$ echo $var1
6
$ var2 = $[$var1 * 2]
$ echo $var2
12
$
2
1
Ad

Recommended

Basics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
Shell programming
Shell programming
Moayad Moawiah
 
Apache ppt
Apache ppt
poornima sugumaran
 
Linux file system
Linux file system
Midaga Mengistu
 
Basic Linux Internals
Basic Linux Internals
mukul bhardwaj
 
Shell Scripting
Shell Scripting
Gaurav Shinde
 
Shell scripting
Shell scripting
Manav Prasad
 
Linux systems - Linux Commands and Shell Scripting
Linux systems - Linux Commands and Shell Scripting
Emertxe Information Technologies Pvt Ltd
 
Linux file system
Linux file system
Md. Tanvir Hossain
 
Unix - An Introduction
Unix - An Introduction
Deepanshu Gahlaut
 
Introduction to Rust language programming
Introduction to Rust language programming
Rodolfo Finochietti
 
Introduction to System Calls
Introduction to System Calls
Vandana Salve
 
System call
System call
DarakhshanNayyab
 
Linux process management
Linux process management
Raghu nath
 
Operating system components
Operating system components
Syed Zaid Irshad
 
Linux command ppt
Linux command ppt
kalyanineve
 
Linux file system
Linux file system
Burhan Abbasi
 
Linux booting process!!
Linux booting process!!
sourav verma
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)
raj upadhyay
 
Introduction to Shell script
Introduction to Shell script
Bhavesh Padharia
 
Linux Commands
Linux Commands
Ramasubbu .P
 
An Insight into the Linux Booting Process
An Insight into the Linux Booting Process
Hardeep Bhurji
 
Linux File System
Linux File System
Anil Kumar Pugalia
 
Unix Operating System
Unix Operating System
subhsikha
 
Linux booting Process
Linux booting Process
Gaurav Sharma
 
Ipc in linux
Ipc in linux
Dr. C.V. Suresh Babu
 
Linux Internals - Part II
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
Debugging linux kernel tools and techniques
Debugging linux kernel tools and techniques
Satpal Parmar
 
Shell Script Tutorial
Shell Script Tutorial
Quang Minh Đoàn
 
shell script introduction
shell script introduction
Jie Jin
 

More Related Content

What's hot (20)

Linux file system
Linux file system
Md. Tanvir Hossain
 
Unix - An Introduction
Unix - An Introduction
Deepanshu Gahlaut
 
Introduction to Rust language programming
Introduction to Rust language programming
Rodolfo Finochietti
 
Introduction to System Calls
Introduction to System Calls
Vandana Salve
 
System call
System call
DarakhshanNayyab
 
Linux process management
Linux process management
Raghu nath
 
Operating system components
Operating system components
Syed Zaid Irshad
 
Linux command ppt
Linux command ppt
kalyanineve
 
Linux file system
Linux file system
Burhan Abbasi
 
Linux booting process!!
Linux booting process!!
sourav verma
 
Terminal Commands (Linux - ubuntu) (part-1)
Terminal Commands (Linux - ubuntu) (part-1)
raj upadhyay
 
Introduction to Shell script
Introduction to Shell script
Bhavesh Padharia
 
Linux Commands
Linux Commands
Ramasubbu .P
 
An Insight into the Linux Booting Process
An Insight into the Linux Booting Process
Hardeep Bhurji
 
Linux File System
Linux File System
Anil Kumar Pugalia
 
Unix Operating System
Unix Operating System
subhsikha
 
Linux booting Process
Linux booting Process
Gaurav Sharma
 
Ipc in linux
Ipc in linux
Dr. C.V. Suresh Babu
 
Linux Internals - Part II
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
Debugging linux kernel tools and techniques
Debugging linux kernel tools and techniques
Satpal Parmar
 

Viewers also liked (20)

Shell Script Tutorial
Shell Script Tutorial
Quang Minh Đoàn
 
shell script introduction
shell script introduction
Jie Jin
 
The bash – defensive scripting
The bash – defensive scripting
timobezjak
 
Bash Scripting Workshop
Bash Scripting Workshop
Ahmed Magdy Ezzeldin, MSc.
 
Bash script
Bash script
Okba Mahdjoub
 
BASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
Quick start bash script
Quick start bash script
Simon Su
 
Basic Unix
Basic Unix
Rajesh Kumar
 
Intro to Linux Shell Scripting
Intro to Linux Shell Scripting
vceder
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scripting
Akshay Siwal
 
Unix Shell Scripting
Unix Shell Scripting
Mustafa Qasim
 
Shell scripting
Shell scripting
simha.dev.lin
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Shell Scripting in Linux
Shell Scripting in Linux
Anu Chaudhry
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
Kenny (netman)
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Shell Scripting
Shell Scripting
Anil Kumar Pugalia
 
Linux Introduction (Commands)
Linux Introduction (Commands)
anandvaidya
 
UNIX/Linux training
UNIX/Linux training
Michael Olafusi
 
shell script introduction
shell script introduction
Jie Jin
 
The bash – defensive scripting
The bash – defensive scripting
timobezjak
 
BASH Guide Summary
BASH Guide Summary
Ohgyun Ahn
 
Quick start bash script
Quick start bash script
Simon Su
 
Intro to Linux Shell Scripting
Intro to Linux Shell Scripting
vceder
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scripting
Akshay Siwal
 
Unix Shell Scripting
Unix Shell Scripting
Mustafa Qasim
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Shell Scripting in Linux
Shell Scripting in Linux
Anu Chaudhry
 
Linux fundamental - Chap 14 shell script
Linux fundamental - Chap 14 shell script
Kenny (netman)
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Linux Introduction (Commands)
Linux Introduction (Commands)
anandvaidya
 
Ad

Similar to Bash Shell Scripting (20)

SHELL PROGRAMMING
SHELL PROGRAMMING
jinal thakrar
 
Unix
Unix
nazeer pasha
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
mugeshmsd5
 
Introduction to shell scripting ____.ppt
Introduction to shell scripting ____.ppt
nalinisamineni
 
Linux Shell Scripting
Linux Shell Scripting
Raghu nath
 
Examples -partII
Examples -partII
Kedar Bhandari
 
Spsl by sasidhar 3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
 
Bash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
Kuntal Bhowmick
 
Shell scripting
Shell scripting
Mufaddal Haidermota
 
Basics of shell programming
Basics of shell programming
Chandan Kumar Rana
 
First steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
Lnx
Lnx
PlanetExpressATX
 
Basic linux commands
Basic linux commands
Harikrishnan Ramakrishnan
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdf
harikrishnapolaki
 
shellScriptAlt.pptx
shellScriptAlt.pptx
NiladriDey18
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
Acácio Oliveira
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
root_fibo
 
Shell Scripts
Shell Scripts
Dr.Ravi
 
34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
mugeshmsd5
 
Introduction to shell scripting ____.ppt
Introduction to shell scripting ____.ppt
nalinisamineni
 
Linux Shell Scripting
Linux Shell Scripting
Raghu nath
 
Bash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Basic shell programs assignment 1_solution_manual
Basic shell programs assignment 1_solution_manual
Kuntal Bhowmick
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdf
harikrishnapolaki
 
shellScriptAlt.pptx
shellScriptAlt.pptx
NiladriDey18
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
Acácio Oliveira
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
root_fibo
 
Shell Scripts
Shell Scripts
Dr.Ravi
 
34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Ad

More from Raghu nath (20)

Mongo db
Mongo db
Raghu nath
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)
Raghu nath
 
MS WORD 2013
MS WORD 2013
Raghu nath
 
Msword
Msword
Raghu nath
 
Ms word
Ms word
Raghu nath
 
Javascript part1
Javascript part1
Raghu nath
 
Regular expressions
Regular expressions
Raghu nath
 
Selection sort
Selection sort
Raghu nath
 
Binary search
Binary search
Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
Raghu nath
 
Stemming algorithms
Stemming algorithms
Raghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp role
Raghu nath
 
Network essentials chapter 4
Network essentials chapter 4
Raghu nath
 
Network essentials chapter 3
Network essentials chapter 3
Raghu nath
 
Network essentials chapter 2
Network essentials chapter 2
Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2
Raghu nath
 
python chapter 1
python chapter 1
Raghu nath
 
Perl
Perl
Raghu nath
 
Adv excel® 2013
Adv excel® 2013
Raghu nath
 
Ftp (file transfer protocol)
Ftp (file transfer protocol)
Raghu nath
 
Javascript part1
Javascript part1
Raghu nath
 
Regular expressions
Regular expressions
Raghu nath
 
Selection sort
Selection sort
Raghu nath
 
Binary search
Binary search
Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)
Raghu nath
 
Stemming algorithms
Stemming algorithms
Raghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp role
Raghu nath
 
Network essentials chapter 4
Network essentials chapter 4
Raghu nath
 
Network essentials chapter 3
Network essentials chapter 3
Raghu nath
 
Network essentials chapter 2
Network essentials chapter 2
Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2
Raghu nath
 
python chapter 1
python chapter 1
Raghu nath
 
Adv excel® 2013
Adv excel® 2013
Raghu nath
 

Recently uploaded (20)

From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
Curietech AI in action - Accelerate MuleSoft development
Curietech AI in action - Accelerate MuleSoft development
shyamraj55
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
AI vs Human Writing: Can You Tell the Difference?
AI vs Human Writing: Can You Tell the Difference?
Shashi Sathyanarayana, Ph.D
 
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
 

Bash Shell Scripting

  • 1. Linux Shell script (Bash) Basics PART - 1 1
  • 2. 2  A script is a list of system commands stored in a file.  Steps to write a script :-  Use any editor like vi or vim  chmod permission your-script-name.  Examples:-  $ chmod +x <filename.sh>  $ chmod 755 <filename.sh>  Execute your script as:  $ bash filename.sh  $ bash fileneme.sh  $ ./filename.sh
  • 3. 3  My first shell script clear  echo “hello world“  $ ./first  $ chmod 755 first  $ ./first  Variables in Shell:  In Linux (Shell), there are two types of variable:  (1) System variables :  (2) User defined variables :
  • 4. 4  $ echo $USERNAME  $ echo $HOME  User defined variables :  variable name=value  Examples:  $x = 10  echo Command:  echo command to display text
  • 5.  arithmetic operations  Syntax: expr op1 math-operator op2 Examples: $ expr 10 + 30 $ expr 20 – 10 $ expr 100 / 20 $ expr 200 % 30 $ expr 100 * 30 $ echo `expr 60 + 30` 5
  • 6. Renaming files mv test1 test2 Deleting files rm -i test1 Creating directories mkdir dir3 Deleting directories rmdir dir3 6
  • 7.  $ ps PID TTY TIME CMD  $ ps -ef UID PID PPID C STIME TTY TIME CMD  $ ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD  $ ps -efH UID PID PPID C STIME TTY TIME CMD 7
  • 8. Creating files: $ touch test1 $ ls -il test1 Copying files: cp source destination cp test1 test2 Linking files: There are two different types of file links in Linux: a. A symbolic, or soft, link b. A hard link 8
  • 9. " Double Quotes Double Quotes" - Anything enclose in double quotes removed meaning of that characters (except and $). ' Single quotes 'Single quotes' - Enclosed in single quotes remains unchanged. ` Back quote `Back quote` - To execute command 9
  • 10.  Pipes: who | wc –l  Reading from Files: $ read message $ echo $message Read command to read lines from files  Command substitution: Var=`date` Var=$(date)  Background Processes:  ls -R /tmp & 1 0
  • 11. while read ip name alias do if [ ! -z “$name” ]; then # Use echo -en here to suppress ending the line; # aliases may still be added echo -en “IP is $ip - its name is $name” if [ ! -z “$aliases” ]; then echo “ Aliases: $aliases” else # Just echo a blank line echo fi fi done < /etc/hosts 1 1
  • 12. Stopping processes kill pid disk space $ df $ df –h Disk usages: $ du Commands: $ cat file1 $ sort file1 $ cat file2 $ sort file2 $ sort -n file2 1 2
  • 13.  grep [options] pattern [file]  The grep command searches either the input or the file you specify for lines that contain characters that match the specified pattern. The output from grep is the lines that contain the matching pattern. 1 3
  • 14. $ cat random.sh #!/bin/bash MIN=200 MAX=500 let “scope = $MAX - $MIN” if [ “$scope” -le “0” ]; then echo “Error - MAX is less than MIN!” fi for i in `seq 1 10` do let result=”$RANDOM % $scope + $MIN” echo “A random number between $MIN and $MAX is $result” Done $ ./random.sh 1 4
  • 15. $ cat hypotenuse.sh #!/bin/sh # calculate the length of the hypotenuse of a Pythagorean triangle # using hypotenuse^2 = adjacent^2 + opposite^2 echo -n “Enter the Adjacent length: “ read adjacent echo -n “Enter the Opposite length: “ read opposite osquared=$(($opposite ** 2)) # get o^2 asquared=$(($adjacent ** 2)) # get a^2 hsquared=$(($osquered + $asquared)) # h^2 = a^2 + o^2 hypotenuse=`echo “scale=3;sqrt ($hsquared)” | bc` # bc does sqrt echo “The Hypotenuse is $hypotenuse” 1 5
  • 16.  There are two types of environment variables in the bash shell  Global variables  Local variables 1 6
  • 17.  An array is a variable that can hold multiple values.  To set multiple values for an environment variable, just list them in parentheses, with each value  separated by a space:  $ mytest=(one two three four five)  $  Not much excitement there. If you try to display the array as a normal environment variable,  you’ll be disappointed:  $ echo $mytest  one  $  Only the first value in the array appears. To reference an individual array element, you must use  a numerical index value, which represents its place in the array. The numeric value is enclosed in  square brackets:  $ echo ${mytest[2]}  three  $ 1 7
  • 18. $ date ; who $ chmod u+x test1 $ ./test1 $ echo This is a test This is a test $ echo Let’s see if this’ll work Lets see if thisll work $ 1 8
  • 19. One of the most useful features of shell scripts is the lowly back quote character, usually called the backtick (`) in the Linux world. You must surround the entire command line command with backtick characters: testing=`date` $ cat test5 #!/bin/bash # using the backtick character testing=`date` echo "The date and time are: " $testing $ 1 9
  • 20.  Output redirection The most basic type of redirection is sending output from a command to a file. The bash shell uses the greater-than symbol for this: command > outputfile Input redirection Input redirection is the opposite of output redirection The input redirection symbol is the less-than symbol (<): command < inputfile 2 0
  • 21. $ expr 1 + 5 6 The bash shell includes the expr command to stay compatible with the Bourne shell; however, it also provides a much easier way of performing mathematical equations $ var1=$[1 + 5] $ echo $var1 6 $ var2 = $[$var1 * 2] $ echo $var2 12 $ 2 1