SlideShare una empresa de Scribd logo
1 de 33
Shell Programming

Andrew Vandever
Scale 12x
What is a shell?

The command interpreter used to pass
commands to an operating system; so called
because it is the part of the operating system
that interfaces with the outside world.
What is a CLI?

A Command Line Interface is one in which you
type commands instead of choosing them
from a menu
Putting it together...
The Unix Philosophy
●

Small components do one thing well

●

Chain these components together

●

Portability over efficiency

●

High leverage through reuse

●

Avoid captive user interfaces
Shell scripts are used by...
●

System V init/config

●

Docker and similar

●

Crond

●

...and more
Pick a shell
●

bash is default on most linux distros

●

zsh is a popular modern alternative

●

dash had a run on ubuntu, but bash is back

●

(t)csh is a shell with c-style syntax
On most systems:
Anatomy of a command
program opt

opt-arg

pos-arg

Reality check: These are all arguments. The
program decides what they mean.
How to get help
●

--help
How to get help
●

man
Usage statements
●

Single-letter options: -abc

●

Full-word options: --some-option

●

Placeholders: <this> or THIS or this

●

Optional: [SOMEARG]

●

X and Y are optional, but Y needs X: [X [Y]]
Pro shell manipulation
●

Scroll through history with arrows

●

<ctrl>-r – search backwards through history

●

<ctrl>-<arrow> to move by word

●

cmd | less to view output by page (q to quit)

●

<tab> completion
Useful builtins
Managing files
●

ls – list files, or files inside a directory

●

cd – change into a directory

●

mkdir – make a new directory

●

mv – rename and/or move a file

●

rm – remove files

●

rmdir – remove a directory
How paths work
Shell config
●
●

●

source – exec the lines from a file (also “.”)
~/.bashrc, /etc/bashrc – sourced by interactive
shell instances
~/.profile, /etc/profile – sourced by login shell
instances
Environment variables
●

Inherited by processes launched from shell

●

Set with export

●

Often set in /etc/profile, ~/.profile

●

See with “env”

●

Examples: HOME, LANG, EDITOR, PATH
Other config variables
●
●

●

DO NOT set in profile!
DO put in /etc/bashrc, ~/.bashrc or your shell's
equivalent
Examples: PS1, PROMPT_COMMAND
rc file honorable mentions
●

aliases

●

functions

●

per-shell env variable overrides

●

other shell config (e.g. shopt)
Making scripts
●

Must set read and execute permissions:

●

Should have “shebang” magic on first line:

●

Easier to call if in PATH:
Variables

●

Spaces: No.

●

UPPERCASE is best practice, not required

●

Curlies not required, but work in more cases

●

Passed by value
Special variables
●

${?} - exit status of the last command

●

${$} - this shell's PID

●

${!} - PID of last backgrounded job

●

Positionals:
–

${0} – this script's name

–

${n} – nth arg to this script

–

${@} – all args

–

${#} – number of args
Other expansions
●

$((${num} * 5)) – math

●

$(cat /tmp/somefile) – command sub

●

A{0..3} – list expansion

●

~someuser – tilde expansion

●

>(head) – process substitution

●

*.txt – path expansion
Command interaction
●

Simple: cmd1; cmd2

●

Conditional: cmd1 && cmd2

●

Else: cmd1 || cmd2

●

Grouping: (cmd1; cmd2) &> /tmp/outfile
cmd1 || (cmd2 && exit 1)
Parens actually launch a subshell
Command interaction
●

●

Pass output to next command:
cmd1 | cmd2
Pass output as command line args:
cmd1 | xargs cmd2
Escaping
●

'Single quotes prevent parsing'

●

“Double quotes do too, except for $, `, , !”

●

 escapes the next character, even newline
I/O handling
●

Default channels:
–

0 (STDIN)

–

1 (STDOUT)

–

2 (STDERR)

●

STDIN to a file: cmd < file

●

STDOUT to a file: cmd 1> file

●

STDIN to STDOUT: cmd 1>&2
I/O handling
●

●

STDOUT of cmd1 to STDIN of cmd2:
cmd1 | cmd2
STDERR of cmd1 to STDIN of cmd2:
lolno stopit
You usually wouldn't want to pass stderr
ok, fine...
cmd1 2>&1 > /tmp/logfile | cmd2
For
If
Other flow control stuff
●

While

●

Until

●

case
What else should I learn?
●

●

Other bash features: arrays, functions, aliases,
job control
CLI text manipulation: grep, sed, awk, head,
tail, sort, tr, wc, etcetera

●

Text-based editors: vi/emacs

●

Shell multiplexing/sharing: screen/tmux
More resources
●

Guides at tldp.org:
–

Bash Guide for Beginners

–

Advanced Bash Scripting Guide

–

GNU/Linux Command-Line Tools Summary

●

Vimtutor

●

http://lmgtfy.com/?q=tmux+tutorial

●

python, ruby, perl, etc.

Más contenido relacionado

La actualidad más candente

Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Mastering unix
Mastering unixMastering unix
Mastering unix
Raghu nath
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
Kalkey
 
Unit 10 investigating and managing
Unit 10 investigating and managingUnit 10 investigating and managing
Unit 10 investigating and managing
root_fibo
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
Rahul Pola
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Sudharsan S
 

La actualidad más candente (20)

Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 
Quick start bash script
Quick start   bash scriptQuick start   bash script
Quick start bash script
 
Shell programming
Shell programmingShell programming
Shell programming
 
Mastering unix
Mastering unixMastering unix
Mastering unix
 
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
 
Devel::hdb debugger talk
Devel::hdb debugger talkDevel::hdb debugger talk
Devel::hdb debugger talk
 
Shell programming 1.ppt
Shell programming  1.pptShell programming  1.ppt
Shell programming 1.ppt
 
Unix - Shell Scripts
Unix - Shell ScriptsUnix - Shell Scripts
Unix - Shell Scripts
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
Unit 10 investigating and managing
Unit 10 investigating and managingUnit 10 investigating and managing
Unit 10 investigating and managing
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Chap06
Chap06Chap06
Chap06
 
Introduction to Shell script
Introduction to Shell scriptIntroduction to Shell script
Introduction to Shell script
 
Linux shell env
Linux shell envLinux shell env
Linux shell env
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Linux shell scripting
Linux shell scriptingLinux shell scripting
Linux shell scripting
 
Basics of shell programming
Basics of shell programmingBasics of shell programming
Basics of shell programming
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
 

Similar a Shellprogramming

2.1.using the shell
2.1.using the shell2.1.using the shell
2.1.using the shell
donv214
 
Compiler design notes phases of compiler
Compiler design notes phases of compilerCompiler design notes phases of compiler
Compiler design notes phases of compiler
ovidlivi91
 
8.1.intro unix
8.1.intro unix8.1.intro unix
8.1.intro unix
southees
 
Linux: A Getting Started Presentation
Linux: A Getting Started PresentationLinux: A Getting Started Presentation
Linux: A Getting Started Presentation
Nap Ramirez
 
Using Unix
Using UnixUsing Unix
Using Unix
Dr.Ravi
 

Similar a Shellprogramming (20)

Tips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development EfficiencyTips and Tricks for Increased Development Efficiency
Tips and Tricks for Increased Development Efficiency
 
Fundamental of Shell Programming
Fundamental of Shell ProgrammingFundamental of Shell Programming
Fundamental of Shell Programming
 
Linux shell
Linux shellLinux shell
Linux shell
 
Adhocr T-dose 2012
Adhocr T-dose 2012Adhocr T-dose 2012
Adhocr T-dose 2012
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
IntroCommandLine.ppt
IntroCommandLine.pptIntroCommandLine.ppt
IntroCommandLine.ppt
 
2.1.using the shell
2.1.using the shell2.1.using the shell
2.1.using the shell
 
Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
Compiler design notes phases of compiler
Compiler design notes phases of compilerCompiler design notes phases of compiler
Compiler design notes phases of compiler
 
Using R on High Performance Computers
Using R on High Performance ComputersUsing R on High Performance Computers
Using R on High Performance Computers
 
8.1.intro unix
8.1.intro unix8.1.intro unix
8.1.intro unix
 
linux cmds.pptx
linux cmds.pptxlinux cmds.pptx
linux cmds.pptx
 
Linux: A Getting Started Presentation
Linux: A Getting Started PresentationLinux: A Getting Started Presentation
Linux: A Getting Started Presentation
 
Terminals and Shells
Terminals and ShellsTerminals and Shells
Terminals and Shells
 
Mac OSX Terminal 101
Mac OSX Terminal 101Mac OSX Terminal 101
Mac OSX Terminal 101
 
Using Unix
Using UnixUsing Unix
Using Unix
 
50 Most Frequently Used UNIX Linux Commands -hmftj
50 Most Frequently Used UNIX  Linux Commands -hmftj50 Most Frequently Used UNIX  Linux Commands -hmftj
50 Most Frequently Used UNIX Linux Commands -hmftj
 
Shell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdfShell Programming_Module2_Part2.pptx.pdf
Shell Programming_Module2_Part2.pptx.pdf
 
Introduction to ansible
Introduction to ansibleIntroduction to ansible
Introduction to ansible
 
2018-Summer-Tutorial-Intro-to-Linux.pdf
2018-Summer-Tutorial-Intro-to-Linux.pdf2018-Summer-Tutorial-Intro-to-Linux.pdf
2018-Summer-Tutorial-Intro-to-Linux.pdf
 

Shellprogramming

  • 2. What is a shell? The command interpreter used to pass commands to an operating system; so called because it is the part of the operating system that interfaces with the outside world.
  • 3. What is a CLI? A Command Line Interface is one in which you type commands instead of choosing them from a menu
  • 5. The Unix Philosophy ● Small components do one thing well ● Chain these components together ● Portability over efficiency ● High leverage through reuse ● Avoid captive user interfaces
  • 6. Shell scripts are used by... ● System V init/config ● Docker and similar ● Crond ● ...and more
  • 7. Pick a shell ● bash is default on most linux distros ● zsh is a popular modern alternative ● dash had a run on ubuntu, but bash is back ● (t)csh is a shell with c-style syntax On most systems:
  • 8. Anatomy of a command program opt opt-arg pos-arg Reality check: These are all arguments. The program decides what they mean.
  • 9. How to get help ● --help
  • 10. How to get help ● man
  • 11. Usage statements ● Single-letter options: -abc ● Full-word options: --some-option ● Placeholders: <this> or THIS or this ● Optional: [SOMEARG] ● X and Y are optional, but Y needs X: [X [Y]]
  • 12. Pro shell manipulation ● Scroll through history with arrows ● <ctrl>-r – search backwards through history ● <ctrl>-<arrow> to move by word ● cmd | less to view output by page (q to quit) ● <tab> completion
  • 14. Managing files ● ls – list files, or files inside a directory ● cd – change into a directory ● mkdir – make a new directory ● mv – rename and/or move a file ● rm – remove files ● rmdir – remove a directory
  • 16. Shell config ● ● ● source – exec the lines from a file (also “.”) ~/.bashrc, /etc/bashrc – sourced by interactive shell instances ~/.profile, /etc/profile – sourced by login shell instances
  • 17. Environment variables ● Inherited by processes launched from shell ● Set with export ● Often set in /etc/profile, ~/.profile ● See with “env” ● Examples: HOME, LANG, EDITOR, PATH
  • 18. Other config variables ● ● ● DO NOT set in profile! DO put in /etc/bashrc, ~/.bashrc or your shell's equivalent Examples: PS1, PROMPT_COMMAND
  • 19. rc file honorable mentions ● aliases ● functions ● per-shell env variable overrides ● other shell config (e.g. shopt)
  • 20. Making scripts ● Must set read and execute permissions: ● Should have “shebang” magic on first line: ● Easier to call if in PATH:
  • 21. Variables ● Spaces: No. ● UPPERCASE is best practice, not required ● Curlies not required, but work in more cases ● Passed by value
  • 22. Special variables ● ${?} - exit status of the last command ● ${$} - this shell's PID ● ${!} - PID of last backgrounded job ● Positionals: – ${0} – this script's name – ${n} – nth arg to this script – ${@} – all args – ${#} – number of args
  • 23. Other expansions ● $((${num} * 5)) – math ● $(cat /tmp/somefile) – command sub ● A{0..3} – list expansion ● ~someuser – tilde expansion ● >(head) – process substitution ● *.txt – path expansion
  • 24. Command interaction ● Simple: cmd1; cmd2 ● Conditional: cmd1 && cmd2 ● Else: cmd1 || cmd2 ● Grouping: (cmd1; cmd2) &> /tmp/outfile cmd1 || (cmd2 && exit 1) Parens actually launch a subshell
  • 25. Command interaction ● ● Pass output to next command: cmd1 | cmd2 Pass output as command line args: cmd1 | xargs cmd2
  • 26. Escaping ● 'Single quotes prevent parsing' ● “Double quotes do too, except for $, `, , !” ● escapes the next character, even newline
  • 27. I/O handling ● Default channels: – 0 (STDIN) – 1 (STDOUT) – 2 (STDERR) ● STDIN to a file: cmd < file ● STDOUT to a file: cmd 1> file ● STDIN to STDOUT: cmd 1>&2
  • 28. I/O handling ● ● STDOUT of cmd1 to STDIN of cmd2: cmd1 | cmd2 STDERR of cmd1 to STDIN of cmd2: lolno stopit You usually wouldn't want to pass stderr ok, fine... cmd1 2>&1 > /tmp/logfile | cmd2
  • 29. For
  • 30. If
  • 31. Other flow control stuff ● While ● Until ● case
  • 32. What else should I learn? ● ● Other bash features: arrays, functions, aliases, job control CLI text manipulation: grep, sed, awk, head, tail, sort, tr, wc, etcetera ● Text-based editors: vi/emacs ● Shell multiplexing/sharing: screen/tmux
  • 33. More resources ● Guides at tldp.org: – Bash Guide for Beginners – Advanced Bash Scripting Guide – GNU/Linux Command-Line Tools Summary ● Vimtutor ● http://lmgtfy.com/?q=tmux+tutorial ● python, ruby, perl, etc.