SlideShare a Scribd company logo
Shell Scripting

                     An Introduction to 
                    Linux Shell Scripting


                             Vern Ceder
                    Canterbury School, Fort Wayne



Intro to Shell Scripting
Vern Ceder
Canterbury School
This talk ISN'T for

    # Experienced shell scripters
    # People who own T­shirts saying 
      “Go away or I will replace you 
      with a very small shell script”
    # Anyone with a favorite shell 
      hack they MUST share... ;)


Intro to Shell Scripting
Vern Ceder
Canterbury School
This talk is for you if...

    # You can open a terminal
    # You can type
    # You're interested in how to 
      combine those two things to make 
      your life easier 



Intro to Shell Scripting
Vern Ceder
Canterbury School
What's Coming

    # Why use the shell?
    # Basic shell commands
    # Combining shell commands
    # Creating simple scripts
    # Automation with cron


Intro to Shell Scripting
Vern Ceder
Canterbury School
Do you...




Intro to Shell Scripting
Vern Ceder
Canterbury School
$ Need to manage computers not on your 
       desk?
                        
     $ Need to perform complex operations, 
       on lots of files?
     $ Need to repeat the same operations 
       on a lot of machines?
     $ Wish that some of this routine stuff 
       could just happen?




Intro to Shell Scripting
Vern Ceder
Canterbury School
What's a “shell”?

    # A program that uses (text) 
      commands to talk to the OS
    # DOS (e.g.)
    # sh, bash, csh, ksh, zsh, etc...
    # This talk will assume bash


Intro to Shell Scripting
Vern Ceder
Canterbury School
Basic Shell Commands

# Files – ls, cp, mv, rm, mkdir
# Access – chmod, chown
# Disks – df, mount 
# Processes – ps, kill, fg, bg
# Network – ifconfig, ping, tracepath


Intro to Shell Scripting
Vern Ceder
Canterbury School
Take ls, for example
    # ls                   list files
    # ls ­l                list more info
    # ls ­a                list ALL
    # ls ­t                list by date
    # ls ­R                list Recursive
    # ls ­latR             or combine



Intro to Shell Scripting
Vern Ceder
Canterbury School
More file commands
    # cp a b               copy files
    # mv a b               move (rename)
    # rm a                 delete 
    # rm ­rf a             remove EVERYTHING 
                           (danger, danger)




Intro to Shell Scripting
Vern Ceder
Canterbury School
Access commands
    # chown user:group a     change file owner
    # chmod ugo+rwx a        change permissions




Intro to Shell Scripting
Vern Ceder
Canterbury School
disk commands
    # df                      get free space
    # du /folder              get amount used
    # mount                   show/change disks 
                              mounted




Intro to Shell Scripting
Vern Ceder
Canterbury School
process commands
    # ps ax                  show processes
    # kill processid         kill a process
    # fg                     foreground
    # bg                     background




Intro to Shell Scripting
Vern Ceder
Canterbury School
network commands
    # ifconfig               show interfaces
    # ping                   ping a host
    # tracepath              follow a packet




Intro to Shell Scripting
Vern Ceder
Canterbury School
So you can...

    # Manage files
    # Monitor disks
    # Control user access
    # Manage/debug network connections



Intro to Shell Scripting
Vern Ceder
Canterbury School
DANGER! 

       You can also...
       Completely destroy your system
       Don't experiment/practice as 
       root!




Intro to Shell Scripting
Vern Ceder
Canterbury School
But how do you know 
             what does what?
    # Man – the best way to find out 
      what a command does
    # Apropos – the way to find the 
      right command
    # tldp.org – man pages and howto's
    # Google ;) 

Intro to Shell Scripting
Vern Ceder
Canterbury School
So?

       How is this better than using a 
       file manager?
    # Powerful – you can do ANYTHING
    # Remote – you can do it from 
      ANYWHERE
    # “Combinable” (that's next)

Intro to Shell Scripting
Vern Ceder
Canterbury School
The “unix idea”

    # Small, specific utilities
    # Text input/output
    # “pipes” to connect one utility 
      to another




Intro to Shell Scripting
Vern Ceder
Canterbury School
Commands to pull 
                it all together
    # cat                  dump file
    # grep                 find a pattern
    # cut                  cut out a field
    # sort                 sort
    # uniq                 remove dupes
    # head, tail           get part of file
    # more, less           page through file

Intro to Shell Scripting
Vern Ceder
Canterbury School
Introducing the “pipe”

    # | connects the output of one 
      command to the input of another
    # > (and >>) puts the output into 
      a file
    # < puts a file to input



Intro to Shell Scripting
Vern Ceder
Canterbury School
For example...

       Find out who is hogging the home 
       disk...
    # du 
    # sort 
    # tail 
    # du ­cks * | sort ­rn | head

Intro to Shell Scripting
Vern Ceder
Canterbury School
Another example...
       Who's tried to log into your box?
    # cat 
    # grep 
    # cut 
    # sort 
    # uniq 

Intro to Shell Scripting
Vern Ceder
Canterbury School
cat /var/log/auth.log | grep 
   "Failed password" | grep ­v 
   "invalid" | cut ­d " " ­f 9,11 | 
   sort | uniq




Intro to Shell Scripting
Vern Ceder
Canterbury School
Scripts

    # To avoid retyping (and sometimes 
      refiguring out)
    # To handle more complex chores




Intro to Shell Scripting
Vern Ceder
Canterbury School
Creating a simple script

    # Text file (text editor)
    # The “shebang” line
    # Shell commands
    # Make it executable



Intro to Shell Scripting
Vern Ceder
Canterbury School
“space hog” as script

       #!/bin/bash


       du ­cks $1 | sort ­rn | head




Intro to Shell Scripting
Vern Ceder
Canterbury School
“login finder” as script

   Login finder as a script
   cat $1 | grep "Failed password"  
   | grep ­v "invalid" | 
   cut ­d " " ­f 9,11 |grep $2 | 
   sort | uniq


Intro to Shell Scripting
Vern Ceder
Canterbury School
What I didn't cover
                     (a very partial list)

    # find                      # editors 
    # tar                       # Sed & awk
    # environment               # cron & crontab
      vars                      # loops 
    # echo                      # if 
    # sleep                     # test ( [ ] )
Intro to Shell Scripting
Vern Ceder
Canterbury School
But what about automation?

       What if you need to repeat the 
       same script every month? Every 
       week? Every day? Every hour? 
       Every minute?




Intro to Shell Scripting
Vern Ceder
Canterbury School
Automating with cron

# cron lets you run scripts at any 
  time, as any user




Intro to Shell Scripting
Vern Ceder
Canterbury School
Print Resources
# Classic Shell Scripting, Robbins & Beebe, 
  O'Reilly
# Think Linux, Jon Lasser, QUE
# UNIX Hints and Hacks, Kirk Waingrow, QUE
# Linux in a Nutshell, Ellen Siever, et al. 
  O'Reilly
# Many books on Linux/UNIX automation and 
  administration

Intro to Shell Scripting
Vern Ceder
Canterbury School
Web Resources
# See http://tech.canterburyschool.org/tech/shell 
# Google “linux shell scripting” or the like




Intro to Shell Scripting
Vern Ceder
Canterbury School
Video Resources
# http://video.google.com 
   Search for “linux shell scripting tutorial”




Intro to Shell Scripting
Vern Ceder
Canterbury School

More Related Content

PDF
Lesson 2 Understanding Linux File System
Sadia Bashir
 
PPT
Shell Scripting
Gaurav Shinde
 
PPT
Bash shell
xylas121
 
PDF
Linux systems - Linux Commands and Shell Scripting
Emertxe Information Technologies Pvt Ltd
 
PDF
Shell scripting
Manav Prasad
 
PPT
Unix/Linux Basic Commands and Shell Script
sbmguys
 
PPT
Linux presentation
Nikhil Jain
 
PPTX
Linux commands
penetration Tester
 
Lesson 2 Understanding Linux File System
Sadia Bashir
 
Shell Scripting
Gaurav Shinde
 
Bash shell
xylas121
 
Linux systems - Linux Commands and Shell Scripting
Emertxe Information Technologies Pvt Ltd
 
Shell scripting
Manav Prasad
 
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Linux presentation
Nikhil Jain
 
Linux commands
penetration Tester
 

What's hot (20)

PPT
Shell and its types in LINUX
SHUBHA CHATURVEDI
 
PPT
Basic 50 linus command
MAGNA COLLEGE OF ENGINEERING
 
PDF
An Introduction To Linux
Ishan A B Ambanwela
 
PDF
Shell scripting
Geeks Anonymes
 
PDF
Linux Presentation
nishantsri
 
PPTX
Linux basics part 1
Lilesh Pathe
 
PPSX
User Administration in Linux
SAMUEL OJO
 
PDF
Linux Tutorial For Beginners | Linux Administration Tutorial | Linux Commands...
Edureka!
 
PPTX
Shell scripting
simha.dev.lin
 
PPTX
Introduction 2 linux
Papu Kumar
 
PPTX
Vi editor
Ramakrishna kapa
 
PPT
Linux file system
Burhan Abbasi
 
PDF
Linux Basic Commands
Hanan Nmr
 
PDF
Linux basic commands with examples
abclearnn
 
PDF
makefiles tutorial
vsubhashini
 
PDF
Shell scripting
Ashrith Mekala
 
PPTX
Bash Shell Scripting
Raghu nath
 
ODP
Introduction to Shell script
Bhavesh Padharia
 
PDF
Linux File System
Anil Kumar Pugalia
 
PPT
Unix File System
student(MCA)
 
Shell and its types in LINUX
SHUBHA CHATURVEDI
 
Basic 50 linus command
MAGNA COLLEGE OF ENGINEERING
 
An Introduction To Linux
Ishan A B Ambanwela
 
Shell scripting
Geeks Anonymes
 
Linux Presentation
nishantsri
 
Linux basics part 1
Lilesh Pathe
 
User Administration in Linux
SAMUEL OJO
 
Linux Tutorial For Beginners | Linux Administration Tutorial | Linux Commands...
Edureka!
 
Shell scripting
simha.dev.lin
 
Introduction 2 linux
Papu Kumar
 
Vi editor
Ramakrishna kapa
 
Linux file system
Burhan Abbasi
 
Linux Basic Commands
Hanan Nmr
 
Linux basic commands with examples
abclearnn
 
makefiles tutorial
vsubhashini
 
Shell scripting
Ashrith Mekala
 
Bash Shell Scripting
Raghu nath
 
Introduction to Shell script
Bhavesh Padharia
 
Linux File System
Anil Kumar Pugalia
 
Unix File System
student(MCA)
 
Ad

Viewers also liked (8)

PDF
Quick start bash script
Simon Su
 
PDF
Unix Shell Scripting
Mustafa Qasim
 
ODP
OpenGurukul : Language : Shell Scripting
Open Gurukul
 
PPT
Basic command ppt
Rohit Kumar
 
PDF
Let us c(by yashwant kanetkar) chapter 2 solution
rohit kumar
 
PPT
Unix Shell Scripting Basics
Dr.Ravi
 
PPT
Linux command ppt
kalyanineve
 
PPTX
Linux ppt
lincy21
 
Quick start bash script
Simon Su
 
Unix Shell Scripting
Mustafa Qasim
 
OpenGurukul : Language : Shell Scripting
Open Gurukul
 
Basic command ppt
Rohit Kumar
 
Let us c(by yashwant kanetkar) chapter 2 solution
rohit kumar
 
Unix Shell Scripting Basics
Dr.Ravi
 
Linux command ppt
kalyanineve
 
Linux ppt
lincy21
 
Ad

Similar to Intro to Linux Shell Scripting (20)

PDF
(Ebook) linux shell scripting tutorial
jayaramprabhu
 
PDF
21bUc8YeDzZpE
Aniruddh Tyagi
 
PDF
21bUc8YeDzZpE
aniruddh Tyagi
 
PDF
21bUc8YeDzZpE
aniruddh Tyagi
 
PDF
Shell Scripting crash course.pdf
harikrishnapolaki
 
PDF
Shell tutorial
Vu Duy Tu
 
PDF
60761 linux
Ritika Ahlawat
 
PDF
Shell Script Linux
Wellington Oliveira
 
PDF
Complete Guide for Linux shell programming
sudhir singh yadav
 
PDF
3.1.d manual bash script guide lsstv 2.0r11
Acácio Oliveira
 
PPT
Shell_Scripting.ppt
KiranMantri
 
PDF
Slides
abhishekvirmani
 
PDF
2023comp90024_linux2.pdf
LevLafayette1
 
PPT
Using Unix
Dr.Ravi
 
PPTX
Shell & Shell Script
Amit Ghosh
 
PPTX
Shell & Shell Script
Amit Ghosh
 
PPT
Shell Scripting in Linux
Anu Chaudhry
 
PDF
Bash shell programming in linux
Norberto Angulo
 
(Ebook) linux shell scripting tutorial
jayaramprabhu
 
21bUc8YeDzZpE
Aniruddh Tyagi
 
21bUc8YeDzZpE
aniruddh Tyagi
 
21bUc8YeDzZpE
aniruddh Tyagi
 
Shell Scripting crash course.pdf
harikrishnapolaki
 
Shell tutorial
Vu Duy Tu
 
60761 linux
Ritika Ahlawat
 
Shell Script Linux
Wellington Oliveira
 
Complete Guide for Linux shell programming
sudhir singh yadav
 
3.1.d manual bash script guide lsstv 2.0r11
Acácio Oliveira
 
Shell_Scripting.ppt
KiranMantri
 
2023comp90024_linux2.pdf
LevLafayette1
 
Using Unix
Dr.Ravi
 
Shell & Shell Script
Amit Ghosh
 
Shell & Shell Script
Amit Ghosh
 
Shell Scripting in Linux
Anu Chaudhry
 
Bash shell programming in linux
Norberto Angulo
 

Intro to Linux Shell Scripting

  • 1. Shell Scripting An Introduction to  Linux Shell Scripting Vern Ceder Canterbury School, Fort Wayne Intro to Shell Scripting Vern Ceder Canterbury School
  • 2. This talk ISN'T for # Experienced shell scripters # People who own T­shirts saying  “Go away or I will replace you  with a very small shell script” # Anyone with a favorite shell  hack they MUST share... ;) Intro to Shell Scripting Vern Ceder Canterbury School
  • 3. This talk is for you if... # You can open a terminal # You can type # You're interested in how to  combine those two things to make  your life easier  Intro to Shell Scripting Vern Ceder Canterbury School
  • 4. What's Coming # Why use the shell? # Basic shell commands # Combining shell commands # Creating simple scripts # Automation with cron Intro to Shell Scripting Vern Ceder Canterbury School
  • 6. $ Need to manage computers not on your  desk?   $ Need to perform complex operations,  on lots of files? $ Need to repeat the same operations  on a lot of machines? $ Wish that some of this routine stuff  could just happen? Intro to Shell Scripting Vern Ceder Canterbury School
  • 7. What's a “shell”? # A program that uses (text)  commands to talk to the OS # DOS (e.g.) # sh, bash, csh, ksh, zsh, etc... # This talk will assume bash Intro to Shell Scripting Vern Ceder Canterbury School
  • 8. Basic Shell Commands # Files – ls, cp, mv, rm, mkdir # Access – chmod, chown # Disks – df, mount  # Processes – ps, kill, fg, bg # Network – ifconfig, ping, tracepath Intro to Shell Scripting Vern Ceder Canterbury School
  • 9. Take ls, for example # ls    list files # ls ­l list more info # ls ­a list ALL # ls ­t list by date # ls ­R list Recursive # ls ­latR or combine Intro to Shell Scripting Vern Ceder Canterbury School
  • 10. More file commands # cp a b   copy files # mv a b move (rename) # rm a delete  # rm ­rf a remove EVERYTHING  (danger, danger) Intro to Shell Scripting Vern Ceder Canterbury School
  • 11. Access commands # chown user:group a   change file owner # chmod ugo+rwx a change permissions Intro to Shell Scripting Vern Ceder Canterbury School
  • 12. disk commands # df    get free space # du /folder get amount used # mount show/change disks  mounted Intro to Shell Scripting Vern Ceder Canterbury School
  • 13. process commands # ps ax   show processes # kill processid kill a process # fg  foreground # bg  background Intro to Shell Scripting Vern Ceder Canterbury School
  • 14. network commands # ifconfig   show interfaces # ping ping a host # tracepath  follow a packet Intro to Shell Scripting Vern Ceder Canterbury School
  • 15. So you can... # Manage files # Monitor disks # Control user access # Manage/debug network connections Intro to Shell Scripting Vern Ceder Canterbury School
  • 16. DANGER!  You can also... Completely destroy your system Don't experiment/practice as  root! Intro to Shell Scripting Vern Ceder Canterbury School
  • 17. But how do you know  what does what? # Man – the best way to find out  what a command does # Apropos – the way to find the  right command # tldp.org – man pages and howto's # Google ;)  Intro to Shell Scripting Vern Ceder Canterbury School
  • 18. So? How is this better than using a  file manager? # Powerful – you can do ANYTHING # Remote – you can do it from  ANYWHERE # “Combinable” (that's next) Intro to Shell Scripting Vern Ceder Canterbury School
  • 19. The “unix idea” # Small, specific utilities # Text input/output # “pipes” to connect one utility  to another Intro to Shell Scripting Vern Ceder Canterbury School
  • 20. Commands to pull  it all together # cat    dump file # grep  find a pattern # cut cut out a field # sort sort # uniq remove dupes # head, tail get part of file # more, less page through file Intro to Shell Scripting Vern Ceder Canterbury School
  • 21. Introducing the “pipe” # | connects the output of one  command to the input of another # > (and >>) puts the output into  a file # < puts a file to input Intro to Shell Scripting Vern Ceder Canterbury School
  • 22. For example... Find out who is hogging the home  disk... # du  # sort  # tail  # du ­cks * | sort ­rn | head Intro to Shell Scripting Vern Ceder Canterbury School
  • 23. Another example... Who's tried to log into your box? # cat  # grep  # cut  # sort  # uniq  Intro to Shell Scripting Vern Ceder Canterbury School
  • 24. cat /var/log/auth.log | grep  "Failed password" | grep ­v  "invalid" | cut ­d " " ­f 9,11 |  sort | uniq Intro to Shell Scripting Vern Ceder Canterbury School
  • 25. Scripts # To avoid retyping (and sometimes  refiguring out) # To handle more complex chores Intro to Shell Scripting Vern Ceder Canterbury School
  • 26. Creating a simple script # Text file (text editor) # The “shebang” line # Shell commands # Make it executable Intro to Shell Scripting Vern Ceder Canterbury School
  • 27. “space hog” as script #!/bin/bash du ­cks $1 | sort ­rn | head Intro to Shell Scripting Vern Ceder Canterbury School
  • 28. “login finder” as script Login finder as a script cat $1 | grep "Failed password"   | grep ­v "invalid" |  cut ­d " " ­f 9,11 |grep $2 |  sort | uniq Intro to Shell Scripting Vern Ceder Canterbury School
  • 29. What I didn't cover (a very partial list) # find # editors  # tar # Sed & awk # environment  # cron & crontab vars # loops  # echo  # if  # sleep  # test ( [ ] ) Intro to Shell Scripting Vern Ceder Canterbury School
  • 30. But what about automation? What if you need to repeat the  same script every month? Every  week? Every day? Every hour?  Every minute? Intro to Shell Scripting Vern Ceder Canterbury School
  • 31. Automating with cron # cron lets you run scripts at any  time, as any user Intro to Shell Scripting Vern Ceder Canterbury School
  • 32. Print Resources # Classic Shell Scripting, Robbins & Beebe,  O'Reilly # Think Linux, Jon Lasser, QUE # UNIX Hints and Hacks, Kirk Waingrow, QUE # Linux in a Nutshell, Ellen Siever, et al.  O'Reilly # Many books on Linux/UNIX automation and  administration Intro to Shell Scripting Vern Ceder Canterbury School
  • 34. Video Resources # http://video.google.com  Search for “linux shell scripting tutorial” Intro to Shell Scripting Vern Ceder Canterbury School