SlideShare a Scribd company logo
1 of 90
Download to read offline
Bash Shell Scripting
Programming Tools And
Environments
Objective
Objective: To introduce students to the concept
of a shell, effective use of the shell, the shell as
programming language, and shell scripts. We
will use the bash shell for the examples and
details provided.
Shell syntax and commands
history and command completion
job control
variables
programming constructs
scripts
What does the shell do?
User interface to the computer
– reads and executes commands
• some handled by the shell itself (pwd, echo,…)
• some are programs stored in some directory
(look in directories in PATH). Start a subshell to
execute these
Supports scripting (is a programming
language)
Executing a Command
Some commands, shell built ins, are handled
immediately by the shell
If not a shell command, then a utility out on the disk
– Shell searches list of directories in PATH
• Modify PATH in your .bash_profile
– Executes first match it finds
– Executed in a subshell
– Bash remembers where it was found, remembers
(hashes) it
Finding out about Commands
type – tells you if a command is a built-in, an
alias, or a program (external to the shell)
which – tells in which directory a utility is
located
help – displays information about built-in
commands (it is a builtin itself)
man bash – a good place to read about the
BASH shell
info bash – another place to read about the
BASH shell
Example
[jjohnson@ws44 cs265]$ which echo
/bin/echo
[jjohnson@ws44 cs265]$ type -a echo
echo is a shell builtin
echo is /bin/echo
[jjohnson@ws44 cs265]$ help echo
echo: echo [-neE] [arg ...]
Write arguments to the standard output.
Display the ARGs, separated by a single space character and followed by a
newline, on the standard output.
Options:
-n do not append a newline
-e enable interpretation of the following backslash escapes
-E explicitly suppress interpretation of backslash escapes
`echo' interprets the following backslash-escaped characters:
a alert (bell)
b backspace
...
Initialisation Files
• commands & variables read when shell is
started
• if variables are placed in system-wide init files,
available to every shell
• customizations must be exported to be available
• commands & aliases cannot be exported so
must be placed in user-specific init
/etc/profile -
System-Wide Initialization File
Read first by shell
Tasks accomplished:
Sets and exports variables:
PATH, TERM, PS1, etc.
Displays contents of /etc/motd (msg of the
day)
Sets default file creation permissions
(umask)
User-specific initialisation
If the shell is a login shell, it looks for
one of the following (in order)
~/.bash_profile
~/.bash_login
~/.profile
If a non-login interactive shell, reads
~/.bashrc
Other customizations
PS1 – prompt
PATH – add to the search path
Set shell options
noclobber
ignoreeof
command-line editing option (vi or emacs)
See ~kschmidt/Public/.bash_profile
and .bashrc for examples
(Feel free to take them, as you wish)
Shell Variables
The shell keeps track of a set of
parameter names and values.
Some of these parameters determine
the behavior of the shell.
We can access these variables:
set new values for some to customize the
shell.
find out the value of some to help
accomplish a task.
Example Shell Variables
sh / ksh / bash
PWD current working directory
PATH list of places to look for
commands
HOME home directory of user
MAIL where your email is stored
TERM what kind of terminal you have
HISTFILE where your command
history is saved
Displaying Shell Variables
Prefix the name of a shell variable with
"$" to dereference
The echo command will do:
echo $HOME
echo $PATH
You can use these variables on any
command line:
ls -al $HOME
Setting Shell Variables
You can change the value of a shell
variable with an assignment command
(this is a shell builtin command):
HOME=/etc
PATH=/usr/bin:/usr/etc:/sbin
NEWVAR="blah blah blah"
Note the lack of spaces around the '='
export
export variables, functions
make it available to subshells
value passed in
changes made to a value in the subshell do
not persist in the caller
Subshell is created whenever a script or
a program is run
Inherits parent shell's exported variables
The PATH
Each time you give the shell a command line it
does the following:
Checks to see if the command is a shell built-in.
If not - tries to find a program whose name (the
filename) is the same as the command.
The PATH variable tells the shell where to look
for programs (non built-in commands).
echo $PATH
======= [tux] - 22:43:17 =======
$ echo $PATH
~/bin:/usr/local/sbin:/usr/local/bin:/usr/sbi
n:/usr/bin:/sbin:/bin:/usr/games
The PATH is a list of ":" delimited directories.
The PATH is a list and a search order.
You can add stuff to your PATH by changing the
shell startup file.
Notes about PATH
Avoid putting the CWD in your path
– If ya do, put it last
– Beware of other programs with the same
name higher in the PATH
To invoke an executable in the current
directory:
./command
set command (shell builtin)
The set command with no parameters
will print out a list of all the shell varibles.
Some common options:
noclobber – Keeps redirection from
deleting an existing file
-o vi (or -o emacs) – sets command-line
editing mode
ignoreeof – ctrl-D won't exit the shell
$PS1 and $PS2
The PS1 shell variable is your command
line prompt.
The PS2 shell variable is used as a
prompt when the shell needs more input
(in the middle of processing a
command).
By changing PS1 and/or PS2 you can
change the prompt.
Fancy bash prompts
Bash supports some fancy stuff in the
prompt string:
t is replaced by the current time
w is replaced by the current directory
h is replaced by the hostname
u is replaced by the username
n is replaced by a newline
Example bash prompt
======= [queen] - 22:43:17 =======
~ echo $PS1
======= [h] - t =======nw
You can change your prompt by
changing PS1:
PS1="Yes Master? "
Making Changes Stick
If you want to tell the shell (bash) to
always use the prompt "Yes Master ?",
you need to store the change in a shell
startup file.
For bash - change the file ~/.bashrc.
HOME
The HOME variable contains the path to your
home directory.
When you use cd command with no arguments,
the command uses the value of the HOME
variable as the argument.
echo $HOME
/home/vzaychik
Metacharacters
Shell metacharacters are characters that are
handled specially by the shell.
Below is an (incomplete) list of shell
metacharacters
> >> < << | Command redirection, pipe
* [] ? {} File specification
; & || && () Command Sequencing
# Line comment
Metacharacters - quoting
Single character escape -  (backslash)
Character immediately following the backslash is
treated literally
To remove a file named “#bogus”:
rm #bogus
Weak quotes (“) inhibit
Space (token separator)
Filename expansion (wildcards)
Strong quotes (') inhibit pretty much
everything
Aliases
You can create aliases for shell builtins and disk
utilites
– Common use is to add options to the default
behavior of commands (e.g. ls, rm, mv, …)
E.g.: alias rm='rm -i'
To see current aliases, use the alias command
Alias (cont)
Create an alias using the builtin command
alias name[=value]
You should usually enclose value in quotes.
no spaces around the '='
Without a value, alias prints out the alias associated
with name
alias dir=“ls”
alias ls=“ls -CF”
dir
Output same as for “ls -CF”
Variable Substitution
Shells support the use of variables
A variable is a name that is bound to a value.
To set a variable (in Bash) just enter
name=value
No spaces around '='
To see the settings of all variables just enter
“set”.
To kill a variable:
unset name
Variable Substitution (cont)
When processing a command, variable
substitution occurs.
A variable in a command is flagged by a
dollar sign prefix “$”
$ echo My shell is $SHELL
My shell is /bin/bash
echo Writes out its arguments after substitution
is performed.
Variable Substitution (cont)
Variable substitution will occur within
double quotes
$ echo "My shell is $SHELL"
My shell is /usr/local/bin/tcsh
Substitution does not occur within single
quotes.
$ echo 'My shell is $SHELL'
My shell is $SHELL
Variable Substitution (cont)
When the usage of a variable name is not
clear, enclose it within braces ${name}
$ prefix=cs265
$ suffix=.pdf
$ echo $prefix03$suffix
.pdf
$ echo ${prefix}03${suffix}
cs26503.pdf
This occurs when constructing file names in
script files.
Variable Substitution (cont)
Many programs use shell variables (also called
environmental variables) to get configuration
information.
Examples
PRINTER is used by printing commands to
determine the default printer.
TERM is used by programs (e.g., vi, pine) to
determine what type of terminal is being used.
VISUAL is used by network news programs, etc., to
determine what editor to use.
Command Substitution
Command substitution allows the output (stdout) of
a command to replace the command name. There
are two forms:
The original Bourne:
`command`
The Bash (and Korn) extension:
$(command)
Command-substitution
The output of the command is substituted in:
$ echo $(ls)
foo fee file?
$ echo "Today is $(date '+%A %d %B %Y')"
Today is Thursday 30 September 2010
Strong quoting – Single quotes
Inhibits all substitution, and the special
meaning of metacharacters:
$ echo '$USER is $USER'
$USER is $USER
$ echo 'today is `date`'
today is `date`
$ echo 'No background&'
No background&
$ echo 'I said, "radio!"'
I said, "radio"
Weak quoting – double quotes
Allows command and variable
substitution
Inhibits special meaning of all other
metacharacters
$ echo "My name is $USER &"
My name is kschmidt &
$ echo "$2.00 says `date`"
$2.00 says Sun Jan 15 01:43:32
EST 200
Command Execution
Sometimes we need to combine several commands.
There are four formats for combining commands
into one line
– Sequenced
– Grouped
– Chained
– Conditional
Chained Commands
Several commands on the same line
Separated by semicolons
There is no direct relationship between the
commands.
command1; command2; command3
Grouped Commands
• If we apply the same operation to the group, we
can group commands
• Commands are grouped by placing them into
parentheses
• Commands are run in a subshell
Example:
echo “Month” > file; cal 10 2000 >> file
(echo “Month” ; cal 10 2000 ) > file
Conditional Commands
We can combine two or more commands using
conditional relationships AND (&&) and OR (||).
If we AND two commands, the second is executed
only if the first is successful.
If we OR two commands, the second is executed
only of the first fails.
cp file1 file2 && echo “Copy successful”
cp file1 file2 || echo “Copy failed”
Shell Syntax
Comments
# This is a comment
ls # list the files in the current directory
Line continuation
echo A long 
> line
; #Command separator – you can list more
than one command per line separated by ;
ls ; who
/ #Pathname separator
cd /home/jjohnson
Shell Syntax –
wildcards (globbing)
Wildcards, and pathname expansion
* # match any string (including empty)
? # match any single character
[set] # match characters listed in set (can be
range)
[!set] # match any character not given in set
Examples
ls *.c
ls *.?
ls *.[Hh][Tt][Ll]
ls [a-z]
Shell Syntax –
redirection and pipes
File redirection and pipes
< # redirect input from specified source
> # redirect output to specified source
>> # redirect output and append to specified
source
| # pipe the output from one command to the
input to the next
Examples
grep word < /usr/dict/words
ls > listing
ls >> listing
ls -l| wc -l
Shell Syntax - stderr
Distinct from stdout
Also goes to screen, by default
stdout is designated by the file descriptor 1
and stderr by 2 (standard input is 0)
To redirect standard error use 2>
ls filenothere > listing 2> error
ls filenothere 2>&1 > listing # both stdout and
stderr redirected to listing
Shell Syntax – bg jobs
Background jobs
& # run command in the background
grep 'we.*'< /usr/word/dict > wewords &
• This runs the grep command in the background –
you immediately get a new prompt and can
continue your work while the command is run.
jobs – Builtin. Lists active jobs (stopped, or
running in the background). Also see the command
ps
kill will take a PID (see ps) or jobspec (see jobs)
What Is A Script?
A script is a small program that is executed by
the shell.
The script is a text file which will contain:
Shell commands you normally use.
Shell flow control constructs (e.g., if-then-else, etc.)
A heavier use of variables than you normally would
use from the command line.
Why Write Scripts?
Sequences of operations which you perform
often can be placed into a script file and then
executed like a single command.
For example, renaming all files of the form
cs265l*.ppt to cs265l*n.ppt requires a mv command
for each file.
The Unix shells are very powerful, but there
are some things that they do not do well.
Shell scripting - Why Not ?
resource-intensive tasks, especially where speed
is a factor
complex applications, where structured
programming is a necessity
mission-critical applications upon which you are
betting the ranch, or the future of the
company
situations where security is important, where you
need to protect against hacking
Why Not ? (cont.)
Project consists of subcomponents with
interlocking dependencies
Extensive file operations required (Bash is
limited to serial file access, and that only
in a particularly clumsy and inefficient
line-by-line fashion)
Need to generate or manipulate graphics
or GUIs
Why Not ? (cont.)
Need direct access to system hardware
Need port or socket I/O
Need to use libraries or interface with
legacy code
Very Simple Script
#!/bin/sh
echo Hello World
Example: Simple Script
Let’s create a shell script to give us
information about the system.
We create the script using a text editor.
Let’s call the file “status”
#!/bin/bash
uptime
users # maybe hit [Enter] here
Exit the editor
Running a Script
To execute the shell we can do
$ bash status
10:37 up 23 days, 23:54, 14 users, load
average …
afjhj billc …
We can also execute the file as a command if
the appropriate execute access is granted.
$ ./status
bash: ./status: Permission denied
$ chmod +x status
$ ./status # Works correctly.
#! – "sha-bang"
Not needed if bash kicks off the script,
but…
Shells look for "#!" at the very beginning
of the file
It is followed by the program
(interpreter) that will read this file:
#!/bin/bash
#!/usr/bin/python
# this is a Python program. Bash
# doesn't read it. Gives it right
# to the Python interpreter
Conditional Expressions
To perform ifs and whiles we need to be able
to construct conditional expressions.
A conditional expression is one that
evaluates to true or false depending on
its operands
A process’ return value of 0 is taken to
be true ; any nonzero value is false
test - Conditional Expressions
Actually a disk utility
[ is just shorthand
Provides for a great many tests
Is available to all shells
test expression
Or
[ expression ] Separate expression from brackets
spaces
Cond. Expressions (cont)
test returns an exit status of zero (success) for
true
test uses a variety of operators
Unary file operators can test various file properties.
Here are just a few:
-e True if file exists
-f True if file is a regular file
-d True if file is a directory
-w True if file exists and is writable
-O True if I own the file
E.g.
if [ -e ~kschmidt/public_html ] ; then
echo "Kurt has a public web directory"
fi
[] – file and string operators
Binary file operators “file1 op file2”
-nt True is file1 is newer than file2
-ot True is file1 is older than file2
-ef True if f1 and f2 refer to the same inode
Unary string operators “op string”
-z True if string is of zero length
-n True if string is not of zero length
-l Returns length of string
E.g.
if [ -z "$myVar" ] ; then
echo "$myVar has null length"
fi
[] – string operators
These compare lexical order
== != < > <= >=
Note, < > are file redirection. Escape them
E.g.
if [ "abc" != "ABC" ] ; then
echo 'See. Case matters.' ; fi
if [ 12 < 2 ] ; then
echo "12 is less than 2?" ; fi
[] – arithmetic operators
Only for integers
Binary operators:
-lt -gt -le -ge -eq -ne
E.g.
if [ 2 –le 3 ] ; then ;echo "cool!" ; fi
x=5
if [ "$x" –ne 12 ] ; then
echo "Still cool" ; fi
[] – Logical Operators
Logical expression tools
! expression Logical not (I.e., changes sense of
expression)
e1 -a e2 True if both expressions are true.
e1 -o e2True if e1 or e2 is true.
( expression ) Works like normal parentheses for
expressions; use spaces around the
expression.
Examples:
test -e bin -a -d /bin is true
[ -e ~/.bashrc -a ! -d ~/.bashrc ] && echo true
[[ test ]]
Bash added [[]] for more C-like usage:
if [[ -e ~/.bashrc && ! –d ~/.bashrc ]]
then
echo "Let's parse that puppy"
fi
if [[ -z "$myFile" || ! –r $myFile ]]
…
It's a built-in
Why sometimes quote $myFile, sometimes not (it's
usually a good idea to do so)?
Arithmetic Expressions
Bash usually treats variables as strings.
You can change that by using the arithmetic
expansion syntax: (( arithmeticExpr ))
(()) shorthand for the let builtin statement
$ x=1
$ x=x+1 # "x+1" is just a string
echo $x
x+1
Note, $[] is deprecated
Arithmetic Expression (cont)
$ x=1
$ x=$x+1 # still just a string
$ echo $x
1+1
Closer, but still not right.
$ x=1
$ (( x=x+1 ))
$ echo $x
2
Finally!
Sample: Basic conditional
example if .. then
#!/bin/bash
if [ "$1" = "foo" ] ; then
echo expression 
evaluated as true
fi
Sample: Basic conditional
example if .. then ... else
#!/bin/bash
if [ "$1" = "foo" ]
then
echo 'First argument is "foo"'
else
echo 'First arg is not "foo"'
fi
Sample: Conditionals with
variables
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" == "$T2" ] ; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
•Always quote variables in scripts!
Checking return value of a
command
if diff "$fileA" "$fileB" > /dev/null
then
echo "Files are identical"
else
echo "Files are different"
fi
Case Statement
case $opt in
a ) echo "option a";;
b ) echo "option b";;
c ) echo "option c";;
? ) echo 
'usage: alice [-a] [-b] [-c] args...'
exit 1;;
esac
Special Variables
$# the number of arguments
$* all arguments
$@ all arguments (quoted individually)
$? return value of last command
executed
$$ process id of shell
$HOME, $IFS, $PATH, $PS1, $PS2
Scripts and Arguments
Scripts can be started with parameters, just
like commands
aScript arg1 arg2 …
The scripts can access these arguments
through shell variables:
“$n” Is the value of the nth paramter.
The command is parameter zero
“$#” Is the number of parameters entered.
“$*” Expands as a list of all the parameters entered
except the command.
Scripts and Parameters (cont)
Let’s quickly write a script to see this:
(this first line is a quick and dirty way to write a file)
$ cat > xx # cat reads from stdin if no
file specified
echo $0
echo $#
echo $1 $2
echo $*
C-d # Control-D is the end of file
character.
$ chmod +x xx
The file xx is now an executable shell script.
Scripts and Parameters (cont)
$ ./xx a b c
./xx
3
a b
a b c
$ xx
./xx
0
Unspecified parameters expand as
empty strings (I.e.., as nothing)
Loops for, while and until
The for loop is a little bit different from other
programming languages. Basically, it let's you
iterate over a series of 'words' within a string.
The while executes a piece of code if the
control expression is true, and only stops
when it is false (or a explicit break is found
within the executed code.
The until loop is almost equivalent to the
while loop, except that the code is executed
while the control expression evaluates to
false.
For samples
Example
$ for x in 1 2 a; do
> echo $x
> done
1
2
a
Example
$ for x in *; do
> echo $x
> done
bin
mail
public_html
…
For samples
#!/bin/bash
for i in $(cat list.txt) ; do
echo item: $i
done
#!/bin/bash
for (( i=0; i<10; ++i )) ; do
echo item: $i
done
While sample
COUNTER=0
while (( COUNTER < 10 )) ; do
echo The counter is $COUNTER
(( COUNTER = COUNTER+1 ))
done
COUNTER=0
while [ $COUNTER -lt 10 ] ; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done
Until sample
#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]
do
echo COUNTER $COUNTER
let COUNTER-=1
done
Example: Renaming
Goal: Rename all the files of the form
cs265l*.ppt to cs265l*.exe where *
should be 02, 03 … 10.
Solution: for loop
for n in 02 03 04 05 06 07 08 09 10; do
mv cs265l$n.ppt cs265l$n.exe
done
Moves cs265l02.ppt to cs265l02.exe,
cs265l03.ppt to cs265l03.exe, etc.
Example: Renaming (cont)
Better:
for (( n=2; n<10; ++i )) ; do
mv cs265l0$n.ppt cs265l0$n.exe
done
mv cs265l10.ppt cs265l10.exe
Better still:
Write a script, pad, that will pad on the left
Count directly from 2 to 10
Loop Control
break terminates the loop
continue causes a jump to the next
iteration of the loop
Debugging Tip
If you want to watch the commands actually
being executed in a script file, insert the line
“set -x” in the script.
set -x
for n in *; do
echo $n
done
Will display the expanded command before executing it.
+ echo bin
bin
+ echo mail
mail
Functions
As in almost any programming language, you can use
functions to group pieces of code in a more logical way
or practice the divine art of recursion.
Declaring a function is just a matter of writing function
my_func { my_code }.
Calling a function is just like calling another program, you
just write its name.
Local variables
#!/bin/bash
HELLO=Hello
function hello {
local HELLO=World
echo $HELLO
}
$ echo $HELLO
$ hello
$ echo $HELLO
Functions with parameters
sample
#!/bin/bash
function quit {
echo 'Goodbye!'
exit
}
function hello {
echo "Hello $1"
}
for name in Vera Kurt ;
do
hello $name
done
quit
Output:
Hello Vera
Hello Kurt
Goodbye!
Parameter Expansion
${parameter:-word}
Use Default Values.
${parameter:=word}
Assign Default Values.
${parameter:?word}
Display Error if Null or Unset.
${parameter:+word}
Use Alternate Value.
More Parameter Expansion
We can remove parts of a value:
${param#pattern}
${param##pattern}
- removes shortest (#) or longest (##)
leading pattern, if there's a match
${param#pattern}
${param##pattern}
- removes shortest(%) or longest (%)
trailing pattern, if match
pattern is expanded just as in pathname
expansion (globbing) - *, ?, []
More Parameter Expansion
Find the length of a string:
echo ${#foo}
Extract substrings
echo ${foo:2:3}
Regex search and replace
There are more. See the Bash manpage
Example – Parameter Expansion
$ foo=j.i.c
$ echo ${foo#*.}
i.c
$ echo ${foo##*.}
c
$ echo ${foo%.*}
j.i
$ echo ${foo%%.*}
j

More Related Content

What's hot

Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linuxshravan saini
 
Linux command ppt
Linux command pptLinux command ppt
Linux command pptkalyanineve
 
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
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell ScriptingRaghu nath
 
Linux presentation
Linux presentationLinux presentation
Linux presentationNikhil Jain
 
Linux Directory Structure
Linux Directory StructureLinux Directory Structure
Linux Directory StructureKevin OBrien
 
Linux Administration
Linux AdministrationLinux Administration
Linux AdministrationHarish1983
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Scriptsbmguys
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Wave Digitech
 
Linux standard file system
Linux standard file systemLinux standard file system
Linux standard file systemTaaanu01
 
Introduction 2 linux
Introduction 2 linuxIntroduction 2 linux
Introduction 2 linuxPapu Kumar
 

What's hot (20)

Basic commands of linux
Basic commands of linuxBasic commands of linux
Basic commands of linux
 
Linux command ppt
Linux command pptLinux command ppt
Linux command ppt
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
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
 
Basic 50 linus command
Basic 50 linus commandBasic 50 linus command
Basic 50 linus command
 
Linux - Introductions to Linux Operating System
Linux - Introductions to Linux Operating SystemLinux - Introductions to Linux Operating System
Linux - Introductions to Linux Operating System
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux presentation
Linux presentationLinux presentation
Linux presentation
 
Linux Directory Structure
Linux Directory StructureLinux Directory Structure
Linux Directory Structure
 
Linux Administration
Linux AdministrationLinux Administration
Linux Administration
 
User management
User managementUser management
User management
 
Introduction to Unix
Introduction to UnixIntroduction to Unix
Introduction to Unix
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Linux file system
Linux file systemLinux file system
Linux file system
 
Shell programming
Shell programmingShell programming
Shell programming
 
Linux standard file system
Linux standard file systemLinux standard file system
Linux standard file system
 
Introduction 2 linux
Introduction 2 linuxIntroduction 2 linux
Introduction 2 linux
 

Viewers also liked

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
 
Bash scripting for beginner and intermediate
Bash scripting for beginner and intermediateBash scripting for beginner and intermediate
Bash scripting for beginner and intermediateAhmed Gamil
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Scriptstudent
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in LinuxAnu Chaudhry
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)anandvaidya
 

Viewers also liked (8)

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
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Bash scripting for beginner and intermediate
Bash scripting for beginner and intermediateBash scripting for beginner and intermediate
Bash scripting for beginner and intermediate
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Script
 
Shell Scripting in Linux
Shell Scripting in LinuxShell Scripting in Linux
Shell Scripting in Linux
 
Linux Introduction (Commands)
Linux Introduction (Commands)Linux Introduction (Commands)
Linux Introduction (Commands)
 

Similar to Shell scripting

Similar to Shell scripting (20)

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
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Using Unix
Using UnixUsing Unix
Using Unix
 
Customizing the unix environment
Customizing the unix environmentCustomizing the unix environment
Customizing the unix environment
 
Bash shell
Bash shellBash shell
Bash shell
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Licão 09 variables and arrays v2
Licão 09 variables and arrays v2Licão 09 variables and arrays v2
Licão 09 variables and arrays v2
 
Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
Linux com
Linux comLinux com
Linux com
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Scripting and the shell in LINUX
Scripting and the shell in LINUXScripting and the shell in LINUX
Scripting and the shell in LINUX
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
 

More from Manav Prasad (20)

Experience with mulesoft
Experience with mulesoftExperience with mulesoft
Experience with mulesoft
 
Mulesoftconnectors
MulesoftconnectorsMulesoftconnectors
Mulesoftconnectors
 
Mule and web services
Mule and web servicesMule and web services
Mule and web services
 
Mulesoft cloudhub
Mulesoft cloudhubMulesoft cloudhub
Mulesoft cloudhub
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Jpa
JpaJpa
Jpa
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
Json
Json Json
Json
 
The spring framework
The spring frameworkThe spring framework
The spring framework
 
Rest introduction
Rest introductionRest introduction
Rest introduction
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Junit
JunitJunit
Junit
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
Xpath
XpathXpath
Xpath
 
Xslt
XsltXslt
Xslt
 
Xhtml
XhtmlXhtml
Xhtml
 
Css
CssCss
Css
 
Introduction to html5
Introduction to html5Introduction to html5
Introduction to html5
 
Ajax
AjaxAjax
Ajax
 

Recently uploaded

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Recently uploaded (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 

Shell scripting

  • 1. Bash Shell Scripting Programming Tools And Environments
  • 2. Objective Objective: To introduce students to the concept of a shell, effective use of the shell, the shell as programming language, and shell scripts. We will use the bash shell for the examples and details provided. Shell syntax and commands history and command completion job control variables programming constructs scripts
  • 3. What does the shell do? User interface to the computer – reads and executes commands • some handled by the shell itself (pwd, echo,…) • some are programs stored in some directory (look in directories in PATH). Start a subshell to execute these Supports scripting (is a programming language)
  • 4. Executing a Command Some commands, shell built ins, are handled immediately by the shell If not a shell command, then a utility out on the disk – Shell searches list of directories in PATH • Modify PATH in your .bash_profile – Executes first match it finds – Executed in a subshell – Bash remembers where it was found, remembers (hashes) it
  • 5. Finding out about Commands type – tells you if a command is a built-in, an alias, or a program (external to the shell) which – tells in which directory a utility is located help – displays information about built-in commands (it is a builtin itself) man bash – a good place to read about the BASH shell info bash – another place to read about the BASH shell
  • 6. Example [jjohnson@ws44 cs265]$ which echo /bin/echo [jjohnson@ws44 cs265]$ type -a echo echo is a shell builtin echo is /bin/echo [jjohnson@ws44 cs265]$ help echo echo: echo [-neE] [arg ...] Write arguments to the standard output. Display the ARGs, separated by a single space character and followed by a newline, on the standard output. Options: -n do not append a newline -e enable interpretation of the following backslash escapes -E explicitly suppress interpretation of backslash escapes `echo' interprets the following backslash-escaped characters: a alert (bell) b backspace ...
  • 7. Initialisation Files • commands & variables read when shell is started • if variables are placed in system-wide init files, available to every shell • customizations must be exported to be available • commands & aliases cannot be exported so must be placed in user-specific init
  • 8. /etc/profile - System-Wide Initialization File Read first by shell Tasks accomplished: Sets and exports variables: PATH, TERM, PS1, etc. Displays contents of /etc/motd (msg of the day) Sets default file creation permissions (umask)
  • 9. User-specific initialisation If the shell is a login shell, it looks for one of the following (in order) ~/.bash_profile ~/.bash_login ~/.profile If a non-login interactive shell, reads ~/.bashrc
  • 10. Other customizations PS1 – prompt PATH – add to the search path Set shell options noclobber ignoreeof command-line editing option (vi or emacs) See ~kschmidt/Public/.bash_profile and .bashrc for examples (Feel free to take them, as you wish)
  • 11. Shell Variables The shell keeps track of a set of parameter names and values. Some of these parameters determine the behavior of the shell. We can access these variables: set new values for some to customize the shell. find out the value of some to help accomplish a task.
  • 12. Example Shell Variables sh / ksh / bash PWD current working directory PATH list of places to look for commands HOME home directory of user MAIL where your email is stored TERM what kind of terminal you have HISTFILE where your command history is saved
  • 13. Displaying Shell Variables Prefix the name of a shell variable with "$" to dereference The echo command will do: echo $HOME echo $PATH You can use these variables on any command line: ls -al $HOME
  • 14. Setting Shell Variables You can change the value of a shell variable with an assignment command (this is a shell builtin command): HOME=/etc PATH=/usr/bin:/usr/etc:/sbin NEWVAR="blah blah blah" Note the lack of spaces around the '='
  • 15. export export variables, functions make it available to subshells value passed in changes made to a value in the subshell do not persist in the caller Subshell is created whenever a script or a program is run Inherits parent shell's exported variables
  • 16. The PATH Each time you give the shell a command line it does the following: Checks to see if the command is a shell built-in. If not - tries to find a program whose name (the filename) is the same as the command. The PATH variable tells the shell where to look for programs (non built-in commands).
  • 17. echo $PATH ======= [tux] - 22:43:17 ======= $ echo $PATH ~/bin:/usr/local/sbin:/usr/local/bin:/usr/sbi n:/usr/bin:/sbin:/bin:/usr/games The PATH is a list of ":" delimited directories. The PATH is a list and a search order. You can add stuff to your PATH by changing the shell startup file.
  • 18. Notes about PATH Avoid putting the CWD in your path – If ya do, put it last – Beware of other programs with the same name higher in the PATH To invoke an executable in the current directory: ./command
  • 19. set command (shell builtin) The set command with no parameters will print out a list of all the shell varibles. Some common options: noclobber – Keeps redirection from deleting an existing file -o vi (or -o emacs) – sets command-line editing mode ignoreeof – ctrl-D won't exit the shell
  • 20. $PS1 and $PS2 The PS1 shell variable is your command line prompt. The PS2 shell variable is used as a prompt when the shell needs more input (in the middle of processing a command). By changing PS1 and/or PS2 you can change the prompt.
  • 21. Fancy bash prompts Bash supports some fancy stuff in the prompt string: t is replaced by the current time w is replaced by the current directory h is replaced by the hostname u is replaced by the username n is replaced by a newline
  • 22. Example bash prompt ======= [queen] - 22:43:17 ======= ~ echo $PS1 ======= [h] - t =======nw You can change your prompt by changing PS1: PS1="Yes Master? "
  • 23. Making Changes Stick If you want to tell the shell (bash) to always use the prompt "Yes Master ?", you need to store the change in a shell startup file. For bash - change the file ~/.bashrc.
  • 24. HOME The HOME variable contains the path to your home directory. When you use cd command with no arguments, the command uses the value of the HOME variable as the argument. echo $HOME /home/vzaychik
  • 25. Metacharacters Shell metacharacters are characters that are handled specially by the shell. Below is an (incomplete) list of shell metacharacters > >> < << | Command redirection, pipe * [] ? {} File specification ; & || && () Command Sequencing # Line comment
  • 26. Metacharacters - quoting Single character escape - (backslash) Character immediately following the backslash is treated literally To remove a file named “#bogus”: rm #bogus Weak quotes (“) inhibit Space (token separator) Filename expansion (wildcards) Strong quotes (') inhibit pretty much everything
  • 27. Aliases You can create aliases for shell builtins and disk utilites – Common use is to add options to the default behavior of commands (e.g. ls, rm, mv, …) E.g.: alias rm='rm -i' To see current aliases, use the alias command
  • 28. Alias (cont) Create an alias using the builtin command alias name[=value] You should usually enclose value in quotes. no spaces around the '=' Without a value, alias prints out the alias associated with name alias dir=“ls” alias ls=“ls -CF” dir Output same as for “ls -CF”
  • 29. Variable Substitution Shells support the use of variables A variable is a name that is bound to a value. To set a variable (in Bash) just enter name=value No spaces around '=' To see the settings of all variables just enter “set”. To kill a variable: unset name
  • 30. Variable Substitution (cont) When processing a command, variable substitution occurs. A variable in a command is flagged by a dollar sign prefix “$” $ echo My shell is $SHELL My shell is /bin/bash echo Writes out its arguments after substitution is performed.
  • 31. Variable Substitution (cont) Variable substitution will occur within double quotes $ echo "My shell is $SHELL" My shell is /usr/local/bin/tcsh Substitution does not occur within single quotes. $ echo 'My shell is $SHELL' My shell is $SHELL
  • 32. Variable Substitution (cont) When the usage of a variable name is not clear, enclose it within braces ${name} $ prefix=cs265 $ suffix=.pdf $ echo $prefix03$suffix .pdf $ echo ${prefix}03${suffix} cs26503.pdf This occurs when constructing file names in script files.
  • 33. Variable Substitution (cont) Many programs use shell variables (also called environmental variables) to get configuration information. Examples PRINTER is used by printing commands to determine the default printer. TERM is used by programs (e.g., vi, pine) to determine what type of terminal is being used. VISUAL is used by network news programs, etc., to determine what editor to use.
  • 34. Command Substitution Command substitution allows the output (stdout) of a command to replace the command name. There are two forms: The original Bourne: `command` The Bash (and Korn) extension: $(command)
  • 35. Command-substitution The output of the command is substituted in: $ echo $(ls) foo fee file? $ echo "Today is $(date '+%A %d %B %Y')" Today is Thursday 30 September 2010
  • 36. Strong quoting – Single quotes Inhibits all substitution, and the special meaning of metacharacters: $ echo '$USER is $USER' $USER is $USER $ echo 'today is `date`' today is `date` $ echo 'No background&' No background& $ echo 'I said, "radio!"' I said, "radio"
  • 37. Weak quoting – double quotes Allows command and variable substitution Inhibits special meaning of all other metacharacters $ echo "My name is $USER &" My name is kschmidt & $ echo "$2.00 says `date`" $2.00 says Sun Jan 15 01:43:32 EST 200
  • 38. Command Execution Sometimes we need to combine several commands. There are four formats for combining commands into one line – Sequenced – Grouped – Chained – Conditional
  • 39. Chained Commands Several commands on the same line Separated by semicolons There is no direct relationship between the commands. command1; command2; command3
  • 40. Grouped Commands • If we apply the same operation to the group, we can group commands • Commands are grouped by placing them into parentheses • Commands are run in a subshell Example: echo “Month” > file; cal 10 2000 >> file (echo “Month” ; cal 10 2000 ) > file
  • 41. Conditional Commands We can combine two or more commands using conditional relationships AND (&&) and OR (||). If we AND two commands, the second is executed only if the first is successful. If we OR two commands, the second is executed only of the first fails. cp file1 file2 && echo “Copy successful” cp file1 file2 || echo “Copy failed”
  • 42. Shell Syntax Comments # This is a comment ls # list the files in the current directory Line continuation echo A long > line ; #Command separator – you can list more than one command per line separated by ; ls ; who / #Pathname separator cd /home/jjohnson
  • 43. Shell Syntax – wildcards (globbing) Wildcards, and pathname expansion * # match any string (including empty) ? # match any single character [set] # match characters listed in set (can be range) [!set] # match any character not given in set Examples ls *.c ls *.? ls *.[Hh][Tt][Ll] ls [a-z]
  • 44. Shell Syntax – redirection and pipes File redirection and pipes < # redirect input from specified source > # redirect output to specified source >> # redirect output and append to specified source | # pipe the output from one command to the input to the next Examples grep word < /usr/dict/words ls > listing ls >> listing ls -l| wc -l
  • 45. Shell Syntax - stderr Distinct from stdout Also goes to screen, by default stdout is designated by the file descriptor 1 and stderr by 2 (standard input is 0) To redirect standard error use 2> ls filenothere > listing 2> error ls filenothere 2>&1 > listing # both stdout and stderr redirected to listing
  • 46. Shell Syntax – bg jobs Background jobs & # run command in the background grep 'we.*'< /usr/word/dict > wewords & • This runs the grep command in the background – you immediately get a new prompt and can continue your work while the command is run. jobs – Builtin. Lists active jobs (stopped, or running in the background). Also see the command ps kill will take a PID (see ps) or jobspec (see jobs)
  • 47. What Is A Script? A script is a small program that is executed by the shell. The script is a text file which will contain: Shell commands you normally use. Shell flow control constructs (e.g., if-then-else, etc.) A heavier use of variables than you normally would use from the command line.
  • 48. Why Write Scripts? Sequences of operations which you perform often can be placed into a script file and then executed like a single command. For example, renaming all files of the form cs265l*.ppt to cs265l*n.ppt requires a mv command for each file. The Unix shells are very powerful, but there are some things that they do not do well.
  • 49. Shell scripting - Why Not ? resource-intensive tasks, especially where speed is a factor complex applications, where structured programming is a necessity mission-critical applications upon which you are betting the ranch, or the future of the company situations where security is important, where you need to protect against hacking
  • 50. Why Not ? (cont.) Project consists of subcomponents with interlocking dependencies Extensive file operations required (Bash is limited to serial file access, and that only in a particularly clumsy and inefficient line-by-line fashion) Need to generate or manipulate graphics or GUIs
  • 51. Why Not ? (cont.) Need direct access to system hardware Need port or socket I/O Need to use libraries or interface with legacy code
  • 53. Example: Simple Script Let’s create a shell script to give us information about the system. We create the script using a text editor. Let’s call the file “status” #!/bin/bash uptime users # maybe hit [Enter] here Exit the editor
  • 54. Running a Script To execute the shell we can do $ bash status 10:37 up 23 days, 23:54, 14 users, load average … afjhj billc … We can also execute the file as a command if the appropriate execute access is granted. $ ./status bash: ./status: Permission denied $ chmod +x status $ ./status # Works correctly.
  • 55. #! – "sha-bang" Not needed if bash kicks off the script, but… Shells look for "#!" at the very beginning of the file It is followed by the program (interpreter) that will read this file: #!/bin/bash #!/usr/bin/python # this is a Python program. Bash # doesn't read it. Gives it right # to the Python interpreter
  • 56. Conditional Expressions To perform ifs and whiles we need to be able to construct conditional expressions. A conditional expression is one that evaluates to true or false depending on its operands A process’ return value of 0 is taken to be true ; any nonzero value is false
  • 57. test - Conditional Expressions Actually a disk utility [ is just shorthand Provides for a great many tests Is available to all shells test expression Or [ expression ] Separate expression from brackets spaces
  • 58. Cond. Expressions (cont) test returns an exit status of zero (success) for true test uses a variety of operators Unary file operators can test various file properties. Here are just a few: -e True if file exists -f True if file is a regular file -d True if file is a directory -w True if file exists and is writable -O True if I own the file E.g. if [ -e ~kschmidt/public_html ] ; then echo "Kurt has a public web directory" fi
  • 59. [] – file and string operators Binary file operators “file1 op file2” -nt True is file1 is newer than file2 -ot True is file1 is older than file2 -ef True if f1 and f2 refer to the same inode Unary string operators “op string” -z True if string is of zero length -n True if string is not of zero length -l Returns length of string E.g. if [ -z "$myVar" ] ; then echo "$myVar has null length" fi
  • 60. [] – string operators These compare lexical order == != < > <= >= Note, < > are file redirection. Escape them E.g. if [ "abc" != "ABC" ] ; then echo 'See. Case matters.' ; fi if [ 12 < 2 ] ; then echo "12 is less than 2?" ; fi
  • 61. [] – arithmetic operators Only for integers Binary operators: -lt -gt -le -ge -eq -ne E.g. if [ 2 –le 3 ] ; then ;echo "cool!" ; fi x=5 if [ "$x" –ne 12 ] ; then echo "Still cool" ; fi
  • 62. [] – Logical Operators Logical expression tools ! expression Logical not (I.e., changes sense of expression) e1 -a e2 True if both expressions are true. e1 -o e2True if e1 or e2 is true. ( expression ) Works like normal parentheses for expressions; use spaces around the expression. Examples: test -e bin -a -d /bin is true [ -e ~/.bashrc -a ! -d ~/.bashrc ] && echo true
  • 63. [[ test ]] Bash added [[]] for more C-like usage: if [[ -e ~/.bashrc && ! –d ~/.bashrc ]] then echo "Let's parse that puppy" fi if [[ -z "$myFile" || ! –r $myFile ]] … It's a built-in Why sometimes quote $myFile, sometimes not (it's usually a good idea to do so)?
  • 64. Arithmetic Expressions Bash usually treats variables as strings. You can change that by using the arithmetic expansion syntax: (( arithmeticExpr )) (()) shorthand for the let builtin statement $ x=1 $ x=x+1 # "x+1" is just a string echo $x x+1 Note, $[] is deprecated
  • 65. Arithmetic Expression (cont) $ x=1 $ x=$x+1 # still just a string $ echo $x 1+1 Closer, but still not right. $ x=1 $ (( x=x+1 )) $ echo $x 2 Finally!
  • 66. Sample: Basic conditional example if .. then #!/bin/bash if [ "$1" = "foo" ] ; then echo expression evaluated as true fi
  • 67. Sample: Basic conditional example if .. then ... else #!/bin/bash if [ "$1" = "foo" ] then echo 'First argument is "foo"' else echo 'First arg is not "foo"' fi
  • 68. Sample: Conditionals with variables #!/bin/bash T1="foo" T2="bar" if [ "$T1" == "$T2" ] ; then echo expression evaluated as true else echo expression evaluated as false fi •Always quote variables in scripts!
  • 69. Checking return value of a command if diff "$fileA" "$fileB" > /dev/null then echo "Files are identical" else echo "Files are different" fi
  • 70. Case Statement case $opt in a ) echo "option a";; b ) echo "option b";; c ) echo "option c";; ? ) echo 'usage: alice [-a] [-b] [-c] args...' exit 1;; esac
  • 71. Special Variables $# the number of arguments $* all arguments $@ all arguments (quoted individually) $? return value of last command executed $$ process id of shell $HOME, $IFS, $PATH, $PS1, $PS2
  • 72. Scripts and Arguments Scripts can be started with parameters, just like commands aScript arg1 arg2 … The scripts can access these arguments through shell variables: “$n” Is the value of the nth paramter. The command is parameter zero “$#” Is the number of parameters entered. “$*” Expands as a list of all the parameters entered except the command.
  • 73. Scripts and Parameters (cont) Let’s quickly write a script to see this: (this first line is a quick and dirty way to write a file) $ cat > xx # cat reads from stdin if no file specified echo $0 echo $# echo $1 $2 echo $* C-d # Control-D is the end of file character. $ chmod +x xx The file xx is now an executable shell script.
  • 74. Scripts and Parameters (cont) $ ./xx a b c ./xx 3 a b a b c $ xx ./xx 0 Unspecified parameters expand as empty strings (I.e.., as nothing)
  • 75. Loops for, while and until The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string. The while executes a piece of code if the control expression is true, and only stops when it is false (or a explicit break is found within the executed code. The until loop is almost equivalent to the while loop, except that the code is executed while the control expression evaluates to false.
  • 76. For samples Example $ for x in 1 2 a; do > echo $x > done 1 2 a Example $ for x in *; do > echo $x > done bin mail public_html …
  • 77. For samples #!/bin/bash for i in $(cat list.txt) ; do echo item: $i done #!/bin/bash for (( i=0; i<10; ++i )) ; do echo item: $i done
  • 78. While sample COUNTER=0 while (( COUNTER < 10 )) ; do echo The counter is $COUNTER (( COUNTER = COUNTER+1 )) done COUNTER=0 while [ $COUNTER -lt 10 ] ; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done
  • 79. Until sample #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ] do echo COUNTER $COUNTER let COUNTER-=1 done
  • 80. Example: Renaming Goal: Rename all the files of the form cs265l*.ppt to cs265l*.exe where * should be 02, 03 … 10. Solution: for loop for n in 02 03 04 05 06 07 08 09 10; do mv cs265l$n.ppt cs265l$n.exe done Moves cs265l02.ppt to cs265l02.exe, cs265l03.ppt to cs265l03.exe, etc.
  • 81. Example: Renaming (cont) Better: for (( n=2; n<10; ++i )) ; do mv cs265l0$n.ppt cs265l0$n.exe done mv cs265l10.ppt cs265l10.exe Better still: Write a script, pad, that will pad on the left Count directly from 2 to 10
  • 82. Loop Control break terminates the loop continue causes a jump to the next iteration of the loop
  • 83. Debugging Tip If you want to watch the commands actually being executed in a script file, insert the line “set -x” in the script. set -x for n in *; do echo $n done Will display the expanded command before executing it. + echo bin bin + echo mail mail
  • 84. Functions As in almost any programming language, you can use functions to group pieces of code in a more logical way or practice the divine art of recursion. Declaring a function is just a matter of writing function my_func { my_code }. Calling a function is just like calling another program, you just write its name.
  • 85. Local variables #!/bin/bash HELLO=Hello function hello { local HELLO=World echo $HELLO } $ echo $HELLO $ hello $ echo $HELLO
  • 86. Functions with parameters sample #!/bin/bash function quit { echo 'Goodbye!' exit } function hello { echo "Hello $1" } for name in Vera Kurt ; do hello $name done quit Output: Hello Vera Hello Kurt Goodbye!
  • 87. Parameter Expansion ${parameter:-word} Use Default Values. ${parameter:=word} Assign Default Values. ${parameter:?word} Display Error if Null or Unset. ${parameter:+word} Use Alternate Value.
  • 88. More Parameter Expansion We can remove parts of a value: ${param#pattern} ${param##pattern} - removes shortest (#) or longest (##) leading pattern, if there's a match ${param#pattern} ${param##pattern} - removes shortest(%) or longest (%) trailing pattern, if match pattern is expanded just as in pathname expansion (globbing) - *, ?, []
  • 89. More Parameter Expansion Find the length of a string: echo ${#foo} Extract substrings echo ${foo:2:3} Regex search and replace There are more. See the Bash manpage
  • 90. Example – Parameter Expansion $ foo=j.i.c $ echo ${foo#*.} i.c $ echo ${foo##*.} c $ echo ${foo%.*} j.i $ echo ${foo%%.*} j