SlideShare une entreprise Scribd logo
1  sur  56
Télécharger pour lire hors ligne
Linux Bash Shell script
4/29/2014 Linux Bash Shell script 1
• 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
4/29/2014 Linux Bash Shell script 2
• 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/29/2014 Linux Bash Shell script 3
• $ echo $USERNAME
• $ echo $HOME
• User defined variables :
• variable name=value
• Examples:
• $x = 10
• echo Command:
• echo command to display text
4/29/2014 Linux Bash Shell script 4
Shell Arithmetic
• 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`
4/29/2014 Linux Bash Shell script 5
Renaming files
mv test1 test2
Deleting files
rm -i test1
Creating directories
mkdir dir3
Deleting directories
rmdir dir3
4/29/2014 Linux Bash Shell script 6
processes
• $ 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
4/29/2014 Linux Bash Shell script 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
4/29/2014 Linux Bash Shell script 8
Quotes
" 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
4/29/2014 Linux Bash Shell script 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 &
4/29/2014 Linux Bash Shell script 10
Reading with While
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
4/29/2014 Linux Bash Shell script 11
Stopping processes
kill pid
disk space
$ df
$ df –h
Disk usages:
$ du
Commands:
$ cat file1
$ sort file1
$ cat file2
$ sort file2
$ sort -n file2
4/29/2014 Linux Bash Shell script 12
Searching for data
• 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.
4/29/2014 Linux Bash Shell script 13
RANDOM produces a random number between 0 and 32767.
This simple recipe produces 10 random
numbers between 200 and 500:
$ 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
4/29/2014 Linux Bash Shell script 14
Problem 1: Code to calculate the length of the hypotenuse of
a Pythagorean triangle
$ 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”
4/29/2014 Linux Bash Shell script 15
Environment Variables
• There are two types of environment variables in the
bash shell
• Global variables
• Local variables
4/29/2014 Linux Bash Shell script 16
Variable Arrays
• 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
• $
4/29/2014 Linux Bash Shell script 17
Scripting basics
$ 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
$
4/29/2014 Linux Bash Shell script 18
The backtick
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
$
4/29/2014 Linux Bash Shell script 19
Redirecting Input and Output
• 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
4/29/2014 Linux Bash Shell script 20
The expr command
$ 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
$
4/29/2014 Linux Bash Shell script 21
$ chmod u+x test7
$ ./test7
The final result is 500
$
Using bc in scripts
variable=`echo "options; expression" | bc`
$ chmod u+x test9
$ ./test9
The answer is .6880
$
$ cat test10
#!/bin/bash
var1=100
4/29/2014 Linux Bash Shell script 22
var2=45
var3=`echo "scale=4; $var1 / $var2" | bc`
echo The answer for this is $var3
$
$ cat test11
#!/bin/bash
var1=20
var2=3.14159
var3=`echo "scale=4; $var1 * $var1" | bc`
var4=`echo "scale=4; $var3 * $var2" | bc`
echo The final result is $var4
$
4/29/2014 Linux Bash Shell script 23
$ cat test12
#!/bin/bash
var1=10.46
var2=43.67
var3=33.2
var4=71
var5=`bc << EOF
scale = 4
a1 = ( $var1 * $var2)
b1 = ($var3 * $var4)
a1 + b1
EOF
`
echo The final answer for this mess is $var5
• $
4/29/2014 Linux Bash Shell script 24
Checking the exit status
$ date
Sat Sep 29 10:01:30 EDT 2007
$ echo $?
0
$
$ cat test13
#!/bin/bash
# testing the exit status
var1=10
var2=30
var3=$[ $var1 + var2 ]
echo The answer is $var3
exit 5
$
4/29/2014 Linux Bash Shell script 25
$ cat test14
#!/bin/bash
# testing the exit status
var1=10
var2=30
var3=$[ $var1 + var2 ]
exit $var3
$
$ chmod u+x test14
$ ./test14
$ echo $?
40
$
4/29/2014 Linux Bash Shell script 26
Structured Commands
• if-then Statement:
The if-then statement has the following format:
if command
then
commands
Fi
The if-then-else Statement
if command
then
commands
else
commands
fi
4/29/2014 Linux Bash Shell script 27
Nesting ifs
if command1
then
commands
elif command2
then
more commands
Fi
• You can continue to string elif statements together, creating one huge if-
then-elif conglomeration:
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
elif command4
then
command set 4
fi
4/29/2014 Linux Bash Shell script 28
Numeric comparisons
• The most common method for using the test command is
to perform a comparison of two numeric values.
#!/bin/bash
# using numeric test comparisons
val1=10
val2=11
if [ $val1 -gt 5 ]
then
echo "The test value $val1 is greater than 5"
fi
if [ $val1 -eq $val2 ]
then
echo "The values are equal"
else
echo "The values are different"
fi
4/29/2014 Linux Bash Shell script 29
Comparison Description
n1 -eq n2 Check if n1 is equal to n2.
n1 -ge n2 Check if n1 is greater than or equal to
n2.
n1 -gt n2 Check if n1 is greater than n2.
n1 -le n2 Check if n1 is less than or equal to
n2.
n1 -lt n2 Check if n1 is less than n2.
4/29/2014 Linux Bash Shell script 30
String comparisons
• String equality
• The equal and not equal conditions are fairly self-
explanatory with strings. It’s pretty easy to know
• when two string values are the same or not:
#!/bin/bash
# testing string equality
testuser=rich
if [ $USER = $testuser ]
then
echo "Welcome $testuser"
fi
$ ./test7
Welcome rich
$
4/29/2014 Linux Bash Shell script 31
The test Command String Comparisons
Comparison Description
str1 = str2 Check if str1 is the same as
string str2.
str1 != str2 Check if str1 is not the same as
str2.
str1 < str2 Check if str1 is less than str2.
str1 > str2 Check if str1 is greater than
str2.
4/29/2014 Linux Bash Shell script 32
The for Command
for var in list
do
commands
Done
Reading values in a list
#!/bin/bash
# basic for command
for test in Alabama Alaska Arizona Arkansas California Colorado
do
echo The next state is $test
done
$ ./test1
4/29/2014 Linux Bash Shell script 33
reading values from a file
file="states"
for state in `cat $file`
do
echo "Visit beautiful $state"
Done
4/29/2014 Linux Bash Shell script 34
internal field separator
 A space
 A tab
 A newline
file="states"
IFS=$’n’
for state in `cat $file`
do
echo "Visit beautiful $state"
Done
4/29/2014 Linux Bash Shell script 35
Reading a directory using wildcards
for file in /home/tmp/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
Then
echo "$file is a file"
fi
Done
4/29/2014 Linux Bash Shell script 36
For loop
for (( i=1; i ‹= 10; i++ ))
do
echo "The next number is $i"
done
4/29/2014 Linux Bash Shell script 37
Multi.sh
#!/bin/sh
a=$1
if [ $a -lt 1 -o $a -gt 9 ]; then
echo “The number is out of range [1,9]”
exit
fi
echo "Multiplication Table for $a"
for i in 1 2 3 4 5 6 7 8 9
do
m=$[a * i]
echo "$a x $i = $m"
done
4/29/2014 Linux Bash Shell script 38
Shell Arguments
#!/bin/sh
echo "Total number of arguments = $#"
echo "Shell script name = $0"
echo "First arguemnt = $1"
echo "Second arguemnt = $2"
echo “Third argument = $3”
echo "All arguments (a word) = $*"
echo "All arguments in array = $@"
4/29/2014 Linux Bash Shell script 39
Loops
#!/bin/sh
for i in 1 2 3 4 5 6 7 8 9
do
echo “number $i”
done
#!/bin/sh
for i in `seq 1 100`
do
if [ $i -lt 5 ]; then
continue
elif [ $i -gt 10 ]; then
break
fi
echo "number $i"
done
4/29/2014 Linux Bash Shell script 40
Loops (while statement)
while
expression
do
statements
done
#!/bin/sh
i=1
sum=0
while [ $i -le 10 ]
do
sum=$[sum + i]
echo "$i sum = $sum"
i=$[i+1]
done
4/29/2014 Linux Bash Shell script 41
multiple variables
for (( a=1, b=10; a ‹= 10; a++, b-- ))
do
echo "$a - $b"
Done
Nesting Loops:
for (( a = 1; a ‹= 3; a++ ))
do
echo "Starting loop $a:"
for (( b = 1; b ‹= 3; b++ ))
do
echo " Inside loop: $b"
done
done
4/29/2014 Linux Bash Shell script 42
functions
• #!/bin/sh
• function tm_score () {
• local pdb1=$1
• local pdb2=$2
• echo "`tmscore $pdb1 $pdb2 | grep ^TM | awk '{print
$3}'`"
• }
• a=$1
• b=$2
• tm_score $a $b
4/29/2014 Linux Bash Shell script 43
Breaking out of an inner loop
for (( a = 1; a ‹ 4; a++ ))
do
echo "Outer loop: $a"
for (( b = 1; b ‹ 100; b++ ))
do
if [ $b -eq 5 ]
then
break
fi
echo " Inner loop: $b"
done
done
4/29/2014 Linux Bash Shell script 44
Breaking out of an outer loop
for (( a = 1; a ‹ 4; a++ ))
do
echo "Outer loop: $a"
for (( b = 1; b ‹ 100; b++ ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " Inner loop: $b"
done
done
4/29/2014 Linux Bash Shell script 45
The continue command
for (( var1 = 1; var1 ‹ 15; var1++ ))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo "Iteration number: $var1"
done
4/29/2014 Linux Bash Shell script 46
Continue
for (( a = 1; a ‹= 5; a++ ))
do
echo "Iteration $a:"
for (( b = 1; b ‹ 3; b++ ))
do
if [ $a -gt 2 ] && [ $a -lt 4 ]
then
continue 2
fi
var3=$[ $a * $b ]
echo " The result of $a * $b is $var3"
done
done
4/29/2014 Linux Bash Shell script 47
Read file into bash array
– exec < $1
let count=0
while read LINE; do
ARRAY[$count]=$LINE
((count++))
done
echo Number of elements: ${#ARRAY[@]}
# echo array's content
echo ${ARRAY[@]}
# restore stdin from filedescriptor 10
# and close filedescriptor 10
exec 0<&10 10<&-
4/29/2014 Linux Bash Shell script 48
Processing the Output of a
Loop
for file in /home/rich/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif
echo "$file is a file"
fi
done > output.txt
4/29/2014 Linux Bash Shell script 49
Creating a function
function name {
commands
}
name() {
commands
}
4/29/2014 Linux Bash Shell script 50
Using functions
function func1 {
echo "This is an example of a
function"
}
count=1
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done
4/29/2014 Linux Bash Shell script 51
4/29/2014 Linux Bash Shell script 52
count=1
echo "This line comes before the function
definition"
function func1 {
echo "This is an example of a function"
}
while [ $count -le 5 ]
do
func1
count=$[ $count + 1 ]
done
echo "This is the end of the loop"
func2
echo "Now this is the end of the script"
function func2 {
echo "This is an example of a function"
}
Regex
• $echo {a..z}
• $ echo {5..-1}
• if [[ $digit =~ [0-9] ]]; then echo
'$digit is a digit' else echo "oops" fi
4/29/2014 Linux Bash Shell script 53
Regular expression operators
Operator
. Matches any single character.
? The preceding item is optional
and will be matched, at most,
once.
* The preceding item will be
matched zero or more times.
+ The preceding item will be
matched one or more times.
{N} The preceding item is matched
exactly N times.
{N,} The preceding item is matched N
or more times.
{N,M} The preceding item is matched at
least N times, but not more than
M times.
4/29/2014 Linux Bash Shell script 54
Regular expressions
As with other comparison operators (e.g., -lt or ==), bash will
return a zero if an expression like $digit =~ "[[0-9]]"
shows that the variable on the left matches the expression
on the right and a one otherwise. This example test asks
whether the value of $digit matches a single digit.
if [[ $digit =~ [0-9] ]]; then
echo '$digit is a digit'
else
echo "oops"
fi
You can also check whether a reply to a prompt is
numeric with similar syntax:
echo -n "Your answer> "
read REPLY
if [[ $REPLY =~ ^[0-9]+$ ]]; then
echo Numeric
else
echo Non-numeric
fi
4/29/2014 Linux Bash Shell script 55
Sample bash script to perform the unpack/compile process
#!/usr/bin/env bash
if [ -d work ]
then
# remove old work directory if it exists
rm -rf work
fi
mkdir work
cd work
tar xzf /usr/src/distfiles/sed-3.02.tar.gz
cd sed-3.02
./configure --prefix=/usr
make
4/29/2014 Linux Bash Shell script 56

Contenu connexe

Tendances

Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Ahmed El-Arabawy
 
Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2) Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2) Ahmed El-Arabawy
 
Course 102: Lecture 26: FileSystems in Linux (Part 1)
Course 102: Lecture 26: FileSystems in Linux (Part 1) Course 102: Lecture 26: FileSystems in Linux (Part 1)
Course 102: Lecture 26: FileSystems in Linux (Part 1) Ahmed El-Arabawy
 
Course 102: Lecture 6: Seeking Help
Course 102: Lecture 6: Seeking HelpCourse 102: Lecture 6: Seeking Help
Course 102: Lecture 6: Seeking HelpAhmed El-Arabawy
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programmingsudhir singh yadav
 
Bash shell
Bash shellBash shell
Bash shellxylas121
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Ahmed El-Arabawy
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic CommandsHanan Nmr
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShellBoulos Dib
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingTushar B Kute
 

Tendances (20)

Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems
 
Basic 50 linus command
Basic 50 linus commandBasic 50 linus command
Basic 50 linus command
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2) Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2)
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Course 102: Lecture 26: FileSystems in Linux (Part 1)
Course 102: Lecture 26: FileSystems in Linux (Part 1) Course 102: Lecture 26: FileSystems in Linux (Part 1)
Course 102: Lecture 26: FileSystems in Linux (Part 1)
 
Course 102: Lecture 6: Seeking Help
Course 102: Lecture 6: Seeking HelpCourse 102: Lecture 6: Seeking Help
Course 102: Lecture 6: Seeking Help
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Bash shell
Bash shellBash shell
Bash shell
 
Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Linux Basic Commands
Linux Basic CommandsLinux Basic Commands
Linux Basic Commands
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Shell programming
Shell programmingShell programming
Shell programming
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
Part 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module ProgrammingPart 02 Linux Kernel Module Programming
Part 02 Linux Kernel Module Programming
 

En vedette

Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Scriptstudent
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting BasicsDr.Ravi
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell ScriptingMustafa Qasim
 
Licão 11 decision making - statement
Licão 11 decision making - statementLicão 11 decision making - statement
Licão 11 decision making - statementAcácio Oliveira
 
Bash scripting for beginner and intermediate
Bash scripting for beginner and intermediateBash scripting for beginner and intermediate
Bash scripting for beginner and intermediateAhmed Gamil
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shelllebse123
 
Shell Scripting With Arguments
Shell Scripting With ArgumentsShell Scripting With Arguments
Shell Scripting With ArgumentsAlex Shaw III
 
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Joachim Jacob
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash scriptSimon Su
 
Shell script
Shell scriptShell script
Shell scriptTiago
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaJames Falkner
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scriptingAkshay Siwal
 
Linux Bash Shell Cheat Sheet for Beginners
Linux Bash Shell Cheat Sheet for BeginnersLinux Bash Shell Cheat Sheet for Beginners
Linux Bash Shell Cheat Sheet for BeginnersDavide Ciambelli
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Introduction To Apache Mesos
Introduction To Apache MesosIntroduction To Apache Mesos
Introduction To Apache MesosTimothy St. Clair
 
Building and Deploying Application to Apache Mesos
Building and Deploying Application to Apache MesosBuilding and Deploying Application to Apache Mesos
Building and Deploying Application to Apache MesosJoe Stein
 
Introduction to Apache Mesos
Introduction to Apache MesosIntroduction to Apache Mesos
Introduction to Apache MesosJoe Stein
 
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...Codemotion
 

En vedette (20)

Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Script
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Unix Shell Scripting
Unix Shell ScriptingUnix Shell Scripting
Unix Shell Scripting
 
Licão 11 decision making - statement
Licão 11 decision making - statementLicão 11 decision making - statement
Licão 11 decision making - statement
 
Bash scripting for beginner and intermediate
Bash scripting for beginner and intermediateBash scripting for beginner and intermediate
Bash scripting for beginner and intermediate
 
Quize on scripting shell
Quize on scripting shellQuize on scripting shell
Quize on scripting shell
 
Shell Scripting With Arguments
Shell Scripting With ArgumentsShell Scripting With Arguments
Shell Scripting With Arguments
 
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
Part 5 of "Introduction to Linux for Bioinformatics": Working the command lin...
 
Shell Script Tutorial
Shell Script TutorialShell Script Tutorial
Shell Script Tutorial
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Shell script
Shell scriptShell script
Shell script
 
Linux+02
Linux+02Linux+02
Linux+02
 
Asynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and JavaAsynchronous Web Programming with HTML5 WebSockets and Java
Asynchronous Web Programming with HTML5 WebSockets and Java
 
Easiest way to start with Shell scripting
Easiest way to start with Shell scriptingEasiest way to start with Shell scripting
Easiest way to start with Shell scripting
 
Linux Bash Shell Cheat Sheet for Beginners
Linux Bash Shell Cheat Sheet for BeginnersLinux Bash Shell Cheat Sheet for Beginners
Linux Bash Shell Cheat Sheet for Beginners
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Introduction To Apache Mesos
Introduction To Apache MesosIntroduction To Apache Mesos
Introduction To Apache Mesos
 
Building and Deploying Application to Apache Mesos
Building and Deploying Application to Apache MesosBuilding and Deploying Application to Apache Mesos
Building and Deploying Application to Apache Mesos
 
Introduction to Apache Mesos
Introduction to Apache MesosIntroduction to Apache Mesos
Introduction to Apache Mesos
 
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
Serverless Data Architecture at scale on Google Cloud Platform - Lorenzo Ridi...
 

Similaire à Bash Shell Scripting

Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.pptmugeshmsd5
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfHIMANKMISHRA2
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboyKenneth Geisshirt
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.pptKiranMantri
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on LinuxTushar B Kute
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scriptsMichael Boelen
 
The Unbearable Lightness: Extending the Bash shell
The Unbearable Lightness: Extending the Bash shellThe Unbearable Lightness: Extending the Bash shell
The Unbearable Lightness: Extending the Bash shellRoberto Reale
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaiCreateWorld
 
Types of Linux Shells
Types of Linux Shells Types of Linux Shells
Types of Linux Shells BIT DURG
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptroot_fibo
 
The Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docxThe Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docxSUBHI7
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxMARRY7
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxadkinspaige22
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationLicão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationAcácio Oliveira
 

Similaire à Bash Shell Scripting (20)

Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Slides
SlidesSlides
Slides
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scripts
 
The Unbearable Lightness: Extending the Bash shell
The Unbearable Lightness: Extending the Bash shellThe Unbearable Lightness: Extending the Bash shell
The Unbearable Lightness: Extending the Bash shell
 
Unix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU KarnatakaUnix 1st sem lab programs a - VTU Karnataka
Unix 1st sem lab programs a - VTU Karnataka
 
Types of Linux Shells
Types of Linux Shells Types of Linux Shells
Types of Linux Shells
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Scripting and the shell in LINUX
Scripting and the shell in LINUXScripting and the shell in LINUX
Scripting and the shell in LINUX
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Unit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell scriptUnit 11 configuring the bash shell – shell script
Unit 11 configuring the bash shell – shell script
 
The Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docxThe Korn Shell is the UNIX shell (command execution program, often c.docx
The Korn Shell is the UNIX shell (command execution program, often c.docx
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationLicão 12 decision loops - statement iteration
Licão 12 decision loops - statement iteration
 

Plus de Raghu nath

Ftp (file transfer protocol)
Ftp (file transfer protocol)Ftp (file transfer protocol)
Ftp (file transfer protocol)Raghu nath
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Selection sort
Selection sortSelection sort
Selection sortRaghu nath
 
Binary search
Binary search Binary search
Binary search Raghu nath
 
JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)JSON(JavaScript Object Notation)
JSON(JavaScript Object Notation)Raghu nath
 
Stemming algorithms
Stemming algorithmsStemming algorithms
Stemming algorithmsRaghu nath
 
Step by step guide to install dhcp role
Step by step guide to install dhcp roleStep by step guide to install dhcp role
Step by step guide to install dhcp roleRaghu nath
 
Network essentials chapter 4
Network essentials  chapter 4Network essentials  chapter 4
Network essentials chapter 4Raghu nath
 
Network essentials chapter 3
Network essentials  chapter 3Network essentials  chapter 3
Network essentials chapter 3Raghu nath
 
Network essentials chapter 2
Network essentials  chapter 2Network essentials  chapter 2
Network essentials chapter 2Raghu nath
 
Network essentials - chapter 1
Network essentials - chapter 1Network essentials - chapter 1
Network essentials - chapter 1Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2Raghu nath
 
python chapter 1
python chapter 1python chapter 1
python chapter 1Raghu nath
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013Raghu nath
 

Plus de Raghu nath (20)

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

Dernier

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 

Dernier (20)

MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 

Bash Shell Scripting

  • 1. Linux Bash Shell script 4/29/2014 Linux Bash Shell script 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 4/29/2014 Linux Bash Shell script 2
  • 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/29/2014 Linux Bash Shell script 3
  • 4. • $ echo $USERNAME • $ echo $HOME • User defined variables : • variable name=value • Examples: • $x = 10 • echo Command: • echo command to display text 4/29/2014 Linux Bash Shell script 4
  • 5. Shell Arithmetic • 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` 4/29/2014 Linux Bash Shell script 5
  • 6. Renaming files mv test1 test2 Deleting files rm -i test1 Creating directories mkdir dir3 Deleting directories rmdir dir3 4/29/2014 Linux Bash Shell script 6
  • 7. processes • $ 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 4/29/2014 Linux Bash Shell script 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 4/29/2014 Linux Bash Shell script 8
  • 9. Quotes " 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 4/29/2014 Linux Bash Shell script 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 & 4/29/2014 Linux Bash Shell script 10
  • 11. Reading with While 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 4/29/2014 Linux Bash Shell script 11
  • 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 4/29/2014 Linux Bash Shell script 12
  • 13. Searching for data • 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. 4/29/2014 Linux Bash Shell script 13
  • 14. RANDOM produces a random number between 0 and 32767. This simple recipe produces 10 random numbers between 200 and 500: $ 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 4/29/2014 Linux Bash Shell script 14
  • 15. Problem 1: Code to calculate the length of the hypotenuse of a Pythagorean triangle $ 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” 4/29/2014 Linux Bash Shell script 15
  • 16. Environment Variables • There are two types of environment variables in the bash shell • Global variables • Local variables 4/29/2014 Linux Bash Shell script 16
  • 17. Variable Arrays • 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 • $ 4/29/2014 Linux Bash Shell script 17
  • 18. Scripting basics $ 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 $ 4/29/2014 Linux Bash Shell script 18
  • 19. The backtick 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 $ 4/29/2014 Linux Bash Shell script 19
  • 20. Redirecting Input and Output • 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 4/29/2014 Linux Bash Shell script 20
  • 21. The expr command $ 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 $ 4/29/2014 Linux Bash Shell script 21
  • 22. $ chmod u+x test7 $ ./test7 The final result is 500 $ Using bc in scripts variable=`echo "options; expression" | bc` $ chmod u+x test9 $ ./test9 The answer is .6880 $ $ cat test10 #!/bin/bash var1=100 4/29/2014 Linux Bash Shell script 22
  • 23. var2=45 var3=`echo "scale=4; $var1 / $var2" | bc` echo The answer for this is $var3 $ $ cat test11 #!/bin/bash var1=20 var2=3.14159 var3=`echo "scale=4; $var1 * $var1" | bc` var4=`echo "scale=4; $var3 * $var2" | bc` echo The final result is $var4 $ 4/29/2014 Linux Bash Shell script 23
  • 24. $ cat test12 #!/bin/bash var1=10.46 var2=43.67 var3=33.2 var4=71 var5=`bc << EOF scale = 4 a1 = ( $var1 * $var2) b1 = ($var3 * $var4) a1 + b1 EOF ` echo The final answer for this mess is $var5 • $ 4/29/2014 Linux Bash Shell script 24
  • 25. Checking the exit status $ date Sat Sep 29 10:01:30 EDT 2007 $ echo $? 0 $ $ cat test13 #!/bin/bash # testing the exit status var1=10 var2=30 var3=$[ $var1 + var2 ] echo The answer is $var3 exit 5 $ 4/29/2014 Linux Bash Shell script 25
  • 26. $ cat test14 #!/bin/bash # testing the exit status var1=10 var2=30 var3=$[ $var1 + var2 ] exit $var3 $ $ chmod u+x test14 $ ./test14 $ echo $? 40 $ 4/29/2014 Linux Bash Shell script 26
  • 27. Structured Commands • if-then Statement: The if-then statement has the following format: if command then commands Fi The if-then-else Statement if command then commands else commands fi 4/29/2014 Linux Bash Shell script 27
  • 28. Nesting ifs if command1 then commands elif command2 then more commands Fi • You can continue to string elif statements together, creating one huge if- then-elif conglomeration: if command1 then command set 1 elif command2 then command set 2 elif command3 then command set 3 elif command4 then command set 4 fi 4/29/2014 Linux Bash Shell script 28
  • 29. Numeric comparisons • The most common method for using the test command is to perform a comparison of two numeric values. #!/bin/bash # using numeric test comparisons val1=10 val2=11 if [ $val1 -gt 5 ] then echo "The test value $val1 is greater than 5" fi if [ $val1 -eq $val2 ] then echo "The values are equal" else echo "The values are different" fi 4/29/2014 Linux Bash Shell script 29
  • 30. Comparison Description n1 -eq n2 Check if n1 is equal to n2. n1 -ge n2 Check if n1 is greater than or equal to n2. n1 -gt n2 Check if n1 is greater than n2. n1 -le n2 Check if n1 is less than or equal to n2. n1 -lt n2 Check if n1 is less than n2. 4/29/2014 Linux Bash Shell script 30
  • 31. String comparisons • String equality • The equal and not equal conditions are fairly self- explanatory with strings. It’s pretty easy to know • when two string values are the same or not: #!/bin/bash # testing string equality testuser=rich if [ $USER = $testuser ] then echo "Welcome $testuser" fi $ ./test7 Welcome rich $ 4/29/2014 Linux Bash Shell script 31
  • 32. The test Command String Comparisons Comparison Description str1 = str2 Check if str1 is the same as string str2. str1 != str2 Check if str1 is not the same as str2. str1 < str2 Check if str1 is less than str2. str1 > str2 Check if str1 is greater than str2. 4/29/2014 Linux Bash Shell script 32
  • 33. The for Command for var in list do commands Done Reading values in a list #!/bin/bash # basic for command for test in Alabama Alaska Arizona Arkansas California Colorado do echo The next state is $test done $ ./test1 4/29/2014 Linux Bash Shell script 33
  • 34. reading values from a file file="states" for state in `cat $file` do echo "Visit beautiful $state" Done 4/29/2014 Linux Bash Shell script 34
  • 35. internal field separator  A space  A tab  A newline file="states" IFS=$’n’ for state in `cat $file` do echo "Visit beautiful $state" Done 4/29/2014 Linux Bash Shell script 35
  • 36. Reading a directory using wildcards for file in /home/tmp/* do if [ -d "$file" ] then echo "$file is a directory" elif [ -f "$file" ] Then echo "$file is a file" fi Done 4/29/2014 Linux Bash Shell script 36
  • 37. For loop for (( i=1; i ‹= 10; i++ )) do echo "The next number is $i" done 4/29/2014 Linux Bash Shell script 37
  • 38. Multi.sh #!/bin/sh a=$1 if [ $a -lt 1 -o $a -gt 9 ]; then echo “The number is out of range [1,9]” exit fi echo "Multiplication Table for $a" for i in 1 2 3 4 5 6 7 8 9 do m=$[a * i] echo "$a x $i = $m" done 4/29/2014 Linux Bash Shell script 38
  • 39. Shell Arguments #!/bin/sh echo "Total number of arguments = $#" echo "Shell script name = $0" echo "First arguemnt = $1" echo "Second arguemnt = $2" echo “Third argument = $3” echo "All arguments (a word) = $*" echo "All arguments in array = $@" 4/29/2014 Linux Bash Shell script 39
  • 40. Loops #!/bin/sh for i in 1 2 3 4 5 6 7 8 9 do echo “number $i” done #!/bin/sh for i in `seq 1 100` do if [ $i -lt 5 ]; then continue elif [ $i -gt 10 ]; then break fi echo "number $i" done 4/29/2014 Linux Bash Shell script 40
  • 41. Loops (while statement) while expression do statements done #!/bin/sh i=1 sum=0 while [ $i -le 10 ] do sum=$[sum + i] echo "$i sum = $sum" i=$[i+1] done 4/29/2014 Linux Bash Shell script 41
  • 42. multiple variables for (( a=1, b=10; a ‹= 10; a++, b-- )) do echo "$a - $b" Done Nesting Loops: for (( a = 1; a ‹= 3; a++ )) do echo "Starting loop $a:" for (( b = 1; b ‹= 3; b++ )) do echo " Inside loop: $b" done done 4/29/2014 Linux Bash Shell script 42
  • 43. functions • #!/bin/sh • function tm_score () { • local pdb1=$1 • local pdb2=$2 • echo "`tmscore $pdb1 $pdb2 | grep ^TM | awk '{print $3}'`" • } • a=$1 • b=$2 • tm_score $a $b 4/29/2014 Linux Bash Shell script 43
  • 44. Breaking out of an inner loop for (( a = 1; a ‹ 4; a++ )) do echo "Outer loop: $a" for (( b = 1; b ‹ 100; b++ )) do if [ $b -eq 5 ] then break fi echo " Inner loop: $b" done done 4/29/2014 Linux Bash Shell script 44
  • 45. Breaking out of an outer loop for (( a = 1; a ‹ 4; a++ )) do echo "Outer loop: $a" for (( b = 1; b ‹ 100; b++ )) do if [ $b -gt 4 ] then break 2 fi echo " Inner loop: $b" done done 4/29/2014 Linux Bash Shell script 45
  • 46. The continue command for (( var1 = 1; var1 ‹ 15; var1++ )) do if [ $var1 -gt 5 ] && [ $var1 -lt 10 ] then continue fi echo "Iteration number: $var1" done 4/29/2014 Linux Bash Shell script 46
  • 47. Continue for (( a = 1; a ‹= 5; a++ )) do echo "Iteration $a:" for (( b = 1; b ‹ 3; b++ )) do if [ $a -gt 2 ] && [ $a -lt 4 ] then continue 2 fi var3=$[ $a * $b ] echo " The result of $a * $b is $var3" done done 4/29/2014 Linux Bash Shell script 47
  • 48. Read file into bash array – exec < $1 let count=0 while read LINE; do ARRAY[$count]=$LINE ((count++)) done echo Number of elements: ${#ARRAY[@]} # echo array's content echo ${ARRAY[@]} # restore stdin from filedescriptor 10 # and close filedescriptor 10 exec 0<&10 10<&- 4/29/2014 Linux Bash Shell script 48
  • 49. Processing the Output of a Loop for file in /home/rich/* do if [ -d "$file" ] then echo "$file is a directory" elif echo "$file is a file" fi done > output.txt 4/29/2014 Linux Bash Shell script 49
  • 50. Creating a function function name { commands } name() { commands } 4/29/2014 Linux Bash Shell script 50
  • 51. Using functions function func1 { echo "This is an example of a function" } count=1 while [ $count -le 5 ] do func1 count=$[ $count + 1 ] done 4/29/2014 Linux Bash Shell script 51
  • 52. 4/29/2014 Linux Bash Shell script 52 count=1 echo "This line comes before the function definition" function func1 { echo "This is an example of a function" } while [ $count -le 5 ] do func1 count=$[ $count + 1 ] done echo "This is the end of the loop" func2 echo "Now this is the end of the script" function func2 { echo "This is an example of a function" }
  • 53. Regex • $echo {a..z} • $ echo {5..-1} • if [[ $digit =~ [0-9] ]]; then echo '$digit is a digit' else echo "oops" fi 4/29/2014 Linux Bash Shell script 53
  • 54. Regular expression operators Operator . Matches any single character. ? The preceding item is optional and will be matched, at most, once. * The preceding item will be matched zero or more times. + The preceding item will be matched one or more times. {N} The preceding item is matched exactly N times. {N,} The preceding item is matched N or more times. {N,M} The preceding item is matched at least N times, but not more than M times. 4/29/2014 Linux Bash Shell script 54
  • 55. Regular expressions As with other comparison operators (e.g., -lt or ==), bash will return a zero if an expression like $digit =~ "[[0-9]]" shows that the variable on the left matches the expression on the right and a one otherwise. This example test asks whether the value of $digit matches a single digit. if [[ $digit =~ [0-9] ]]; then echo '$digit is a digit' else echo "oops" fi You can also check whether a reply to a prompt is numeric with similar syntax: echo -n "Your answer> " read REPLY if [[ $REPLY =~ ^[0-9]+$ ]]; then echo Numeric else echo Non-numeric fi 4/29/2014 Linux Bash Shell script 55
  • 56. Sample bash script to perform the unpack/compile process #!/usr/bin/env bash if [ -d work ] then # remove old work directory if it exists rm -rf work fi mkdir work cd work tar xzf /usr/src/distfiles/sed-3.02.tar.gz cd sed-3.02 ./configure --prefix=/usr make 4/29/2014 Linux Bash Shell script 56