1. The Bash Love
Ish Sookun
Executive Member, Linux User Group of Mauritius
Member, Mauritius Software Craftsmanship Community
ishwon@openSUSE.org
Ish Sookun The Bash Love
May 2014 @ University of Mauritius
2. Ish Sookun The Bash Love
First things first
A shell is a program that provides an
interface to the operating system,
which itself is an interface to the underlying
hardware (^^,) ...
A shell script is ridiculously just a bunch of
commands. One may use arguments and variables
coupled with logic control & a little bit of
arithmetic to �gure out what and when to run.
Popular shells are Bourne Shell
(sh), Bourne Again Shell (bash),
C Shell (csh), Korn Shell (ksh) ...
You may Google the rest and
have a moment of Shell history!
3. Ish Sookun The Bash Love
When to use Bash?
Run a series of external commands
Pipeline the output of one command as input to another
Code something with only simple logic
Develop incrementally
Create tools to extend your command-line environment
4. Ish Sookun The Bash Love
Know your Coreutils
Coreutils is a bunch of basic programs that run on Unix-like operating
systems, doing file, shell and text manipulation.
Most of these programs...
do one thing & do it well
work together
handle text streams
5. Ish Sookun The Bash Love
Files & directories
Everything
Links
Directories
cp
mv
rm
Copy files
Move/rename files
Delete files
ln Make links
mkdir
rmdir
Make directories
Remove directories
6. Ish Sookun The Bash Love
Text
Rows
cat
head
tail
sort
uniq
All rows
First rows
Last rows
Sort rows
Unique rows
Fields
cut
paste
join
Subset fields
Union fields
(rows in order)
Union fields
(rows joined by a field)
7. Ish Sookun The Bash Love
Pipes & redirects
Bash uses three standard I/O streams, each of which is associated with a
well-known file descriptor:
stdin having file descriptor 0
stdout having file descriptor 1
stderr having file descriptor 2
Examples:
ps -ef | grep -i httpd
ls -lRth > DirectoryContent.log
mysql -u admin -p awesomeDB < awesomeDB_Backup.sql
./myScript.sh 2> error.log
8. Ish Sookun The Bash Love
Let's begin with Shebang
Shebang, yes, that's how your Bash script starts. Oh! You don't write
shebang but write #! instead. Read more about Shebang on Google. (^^,)
#!/bin/bash
# This is an innocent comment
ME=`whoami`
echo "You've gone nuts, $ME."
chmod u+x myScript.sh
By convention give your Bash scripts a .sh extension.
You need to give execution right to your script before you're able to run it.
Now, execute it at the terminal prompt as follows:
./myScript.sh