More Related Content
Similar to Shell Scripting
Similar to Shell Scripting(20)
More from Anil Kumar Pugalia
More from Anil Kumar Pugalia(10)
Recently uploaded(20)
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳AI: mind, matter, meaning, metaphors, being, becoming, life values Shell Scripting
- 1. © 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Shell Scripting
- 2. 2© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What to Expect?
W's of Shell Scripting
Shell Commands: Bash & Linux
Bash: The Language
Scripting: Programming the Shell
Regular Expressions
Automation & Testing
- 3. 3© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
W's of Shell Scripting
What is a Shell?
Various types of Shells
Bourne Shell (sh)
C Shell (csh)
Korn Shell (ksh)
Bourne Again Shell (bash)
TENEX csh (tcsh)
Z Shell (zsh)
Busybox (busybox) – Embedded Systems
What is Scripting?
Various types of Scripting
Shell
Perl, Python, Tcl/Tk, ...
PHP, Javascript, ...
- 4. 4© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Bourne Again SHell
env - shell environment variables
export [var_name] - export a shell variable
HOME - path to user’s home directory
PATH - executable search path
PWD - present user directory
PS1 - command prompt
which - shows executable path
history - command recall
- 5. 5© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Bourne Again SHell ...
alias - create shortcuts to commands
file - shows the information about a file
type - shows information about a command
Setup Scripts
/etc/profile – System wide startup script
~/.bash_profile – User specific startup script
~/.bashrc – Shell specific startup script
~/.bash_logout – User specific shutdown script
- 6. 6© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Usage & Commands
Root & System Directories
File Basics & related Commands
User Basics & related Commands
File Access Permissions
System & Help Information
Standard I/O, Redirection and Pipes
- 7. 7© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
/
the Root of an inverted tree
The top-most or super-parent directory
The container of your computer
Type: ls /
- 8. 8© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
System Directories
/bin, /sbin - system binaries/applications
/var - logs, mails
/proc, /sys - “virtual” windows into the kernel
/etc - configuration files
/lib - shared system libraries
/dev - device files
/boot - Linux kernel and boot related binary files
/opt - for third-party packages
/root, /home - home directory for super user & other users
/usr - user space related files and dirs (binaries, libraries, ...)
/tmp - scratch pad
/mnt - mount points
- 9. 9© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
File Basics
Every thing is viewed as a file in Linux
A file under the /
Seven Types
Regular (-)
Directory (d)
Character Device (c)
Block Device (b)
Named Pipe (p)
Socket (s)
Symbolic Link (l)
- 10. 10© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
File related Shell Commands
ls - list directory/file contents
cd - change directory
pwd - print working directory
df - disk free
du - disk usage
cp - copy
mv - move, rename
rm - remove
mkdir - make directory
rmdir - remove directory
cat, less, head, tail - used to
view text files
vi, vim - editors
touch - create and update
files
grep - search in files
find, locate - search for files
gzip, gunzip, bzip, bunzip -
compression
tar - archive
sed, awk - file manipulation
- 11. 11© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
User Basics
All Accesses into a Linux System are through a
User with a Password
Super User - root
Normal Users - <user_name>
Files: /etc/passwd, /etc/shadow
Users can be categorized into groups
root, bin, sys, adm, …
File: /etc/group
- 12. 12© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
User related Shell Commands
adduser - create user
deluser - delete user
moduser - modify user
su - <username> - start new shell as different
user
finger - user information lookup
passwd - change or create user password
who, w, users - to find out who is logged in
- 13. 13© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
User & File Access
All Files have User specific ownerships & access
permissions
Type: ls -l
–rw–r––r––
Symbol Name Number Position
r read 4 r--
w write 2 -w-
x execute 1 --x
type user group other
user (anil) group (anil)
- 14. 14© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Related Shell Commands
chmod – Change file permissions
chown – Change file owner
chgrp – Change file group
- 15. 15© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Few “Help”ful Shell Commands
uname - print system information
man <topic> - manual pages
info <topic> - information pages
- 16. 16© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Linux Manuals
Divided into sections
1 Shell commands e.g. mv, ls, cat
2 System calls e.g. _exit, read, write
3 Library calls e.g. exit, printf
4 Device and network specific info e.g. mouse, ttyS, null
5 File formats e.g. passwd, termcap
6 Games and demos e.g. fortune, worms
7 Miscellaneous e.g. ascii, fifo, pthreads
8 Administrative commands e.g. fsck, network daemons
9 POSIX Programmer Manual
Info pages are also available
- 17. 17© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Standard Input & Outputs
Standard Input – 0 (default: keyboard)
read
Standard Output – 1 (default: monitor)
echo
Standard Error – 2 (default: monitor)
q
- 18. 18© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Redirections & Pipes
command < file - reads standard input from file
command > file - directs standard output to file
command >> file - appends standard output to file
command 2> file - directs standard error to file
command 2>> file - appends standard error to file
command > file 2>&1 - both std output & error to file
cmd1 | cmd2 - transfer o/p of cmd1 as i/p to cmd2
<<word – here document
<<<word – here string
<>word – open file reading & writing
- 19. 19© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
bash: The Language
#!, Comments, Space Sensitivity
Command-line Arguments & shift
Return Values
Variables & their Values
Built-in: $#, $*, $1, $@, $?, $$, …
User-defined
Evaluation & Brackets - [], {}, ()
Quotes
- 20. 20© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Scripting the Shell
Control Structures
if ... then … else … fi
while ... do … done
for ... in … do … done (Helper: seq)
case ... in … ) … ;; … esac
Operators
Comparison: String, Integers
Logical: True & False
Functions
- 21. 21© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Regular Expressions
Atoms: Symbols, Ranges, Any
*, +, ?
Range repetition: {}
Negation: ^
Subexpressions: (...)
Back references: 1, 2, …
Words: <...>, ^, $
- 22. 22© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
RE & File Processing
vi, sed
echo, cut
grep, find
awk
- 23. 23© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Automation
Putting all these together
Mixing & Matching of various Languages
Based on Power of Expression
Based on Ease of Use
Examples
Interactivity: Expect
File Processing & RE: Perl
GUI: Tcl/Tk, Python
- 24. 24© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Test Automation
Common Requirements
Batch Processing
Input Triggers
Output Logging
Output Processing
Output Analysis & Actions
Involves
Command Repetitions
Interactivity & User Interfaces
File Organization & Processing
Data Parsing, Sorting, Sieving, ...
- 25. 25© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Powerful Commands to Note
Script Generation: vi
Redirections & Pipes
Processing: sort, uniq
RE: sed, cut, grep, find
Sub-language: awk
- 26. 26© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
What all have we learnt?
W's of Shell Scripting
Shell Commands: Bash & Linux
Bash: The Language
Scripting: Programming the Shell
Regular Expressions
Automation & Testing