SlideShare a Scribd company logo
1 of 27
© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Shell Scripting
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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 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© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
RE & File Processing
vi, sed
echo, cut
grep, find
awk
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© 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© 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© 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
27© 2010-14 SysPlay Workshops <workshop@sysplay.in>
All Rights Reserved.
Any Queries?

More Related Content

What's hot (20)

Low-level Accesses
Low-level AccessesLow-level Accesses
Low-level Accesses
 
Block Drivers
Block DriversBlock Drivers
Block Drivers
 
System Calls
System CallsSystem Calls
System Calls
 
PCI Drivers
PCI DriversPCI Drivers
PCI Drivers
 
SPI Drivers
SPI DriversSPI Drivers
SPI Drivers
 
Kernel Programming
Kernel ProgrammingKernel Programming
Kernel Programming
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
Toolchain
ToolchainToolchain
Toolchain
 
Introduction to Linux Drivers
Introduction to Linux DriversIntroduction to Linux Drivers
Introduction to Linux Drivers
 
Board Bringup
Board BringupBoard Bringup
Board Bringup
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 
Kernel Timing Management
Kernel Timing ManagementKernel Timing Management
Kernel Timing Management
 
Video Drivers
Video DriversVideo Drivers
Video Drivers
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 
Architecture Porting
Architecture PortingArchitecture Porting
Architecture Porting
 
Kernel Debugging & Profiling
Kernel Debugging & ProfilingKernel Debugging & Profiling
Kernel Debugging & Profiling
 
Character Drivers
Character DriversCharacter Drivers
Character Drivers
 
File System Modules
File System ModulesFile System Modules
File System Modules
 
Processes
ProcessesProcesses
Processes
 

Viewers also liked (11)

Mobile Hacking using Linux Drivers
Mobile Hacking using Linux DriversMobile Hacking using Linux Drivers
Mobile Hacking using Linux Drivers
 
Embedded Software Design
Embedded Software DesignEmbedded Software Design
Embedded Software Design
 
Functional Programming with LISP
Functional Programming with LISPFunctional Programming with LISP
Functional Programming with LISP
 
Embedded C
Embedded CEmbedded C
Embedded C
 
Threads
ThreadsThreads
Threads
 
Timers
TimersTimers
Timers
 
Signals
SignalsSignals
Signals
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Inter Process Communication
Inter Process CommunicationInter Process Communication
Inter Process Communication
 
References
ReferencesReferences
References
 
Interrupts
InterruptsInterrupts
Interrupts
 

Similar to Shell Scripting (20)

Linux System
Linux SystemLinux System
Linux System
 
Linux Internals Part - 1
Linux Internals Part - 1Linux Internals Part - 1
Linux Internals Part - 1
 
60761 linux
60761 linux60761 linux
60761 linux
 
Complete Guide for Linux shell programming
Complete Guide for Linux shell programmingComplete Guide for Linux shell programming
Complete Guide for Linux shell programming
 
Basics of Linux Commands, Git and Github
Basics of Linux Commands, Git and GithubBasics of Linux Commands, Git and Github
Basics of Linux Commands, Git and Github
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Operating system (remuel)
Operating system (remuel)Operating system (remuel)
Operating system (remuel)
 
Shell scripting - By Vu Duy Tu from eXo Platform SEA
Shell scripting - By Vu Duy Tu from eXo Platform SEAShell scripting - By Vu Duy Tu from eXo Platform SEA
Shell scripting - By Vu Duy Tu from eXo Platform SEA
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Linux powerpoint
Linux powerpointLinux powerpoint
Linux powerpoint
 
unix.ppt
unix.pptunix.ppt
unix.ppt
 
Shell_Scripting.ppt
Shell_Scripting.pptShell_Scripting.ppt
Shell_Scripting.ppt
 
Lamp ppt
Lamp pptLamp ppt
Lamp ppt
 
File Systems
File SystemsFile Systems
File Systems
 
Terminal basic-commands(Unix) -partI
Terminal basic-commands(Unix) -partITerminal basic-commands(Unix) -partI
Terminal basic-commands(Unix) -partI
 
Useful Linux and Unix commands handbook
Useful Linux and Unix commands handbookUseful Linux and Unix commands handbook
Useful Linux and Unix commands handbook
 
Shell Scripting crash course.pdf
Shell Scripting crash course.pdfShell Scripting crash course.pdf
Shell Scripting crash course.pdf
 
101 apend. scripting, crond, atd
101 apend. scripting, crond, atd101 apend. scripting, crond, atd
101 apend. scripting, crond, atd
 
Linux Porting
Linux PortingLinux Porting
Linux Porting
 
Lamp ppt
Lamp pptLamp ppt
Lamp ppt
 

More from Anil Kumar Pugalia (10)

Processes
ProcessesProcesses
Processes
 
System Calls
System CallsSystem Calls
System Calls
 
Playing with R L C Circuits
Playing with R L C CircuitsPlaying with R L C Circuits
Playing with R L C Circuits
 
Audio Drivers
Audio DriversAudio Drivers
Audio Drivers
 
Power of vi
Power of viPower of vi
Power of vi
 
"make" system
"make" system"make" system
"make" system
 
Hardware Design for Software Hackers
Hardware Design for Software HackersHardware Design for Software Hackers
Hardware Design for Software Hackers
 
RPM Building
RPM BuildingRPM Building
RPM Building
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Linux File System
Linux File SystemLinux File System
Linux File System
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 

Recently uploaded (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

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
  • 27. 27© 2010-14 SysPlay Workshops <workshop@sysplay.in> All Rights Reserved. Any Queries?