SlideShare a Scribd company logo
Becoming A Bash Ninja New York PHP March 24, 2009 Brian Dailey [email_address] http://realm3.com/
Becoming A Bash Ninja Agenda Bash Basics Keyboard Navigation Shell Shortcuts Piping Shell Tools GNU Screen vim phing Benchmarking (ab/siege)
About Me 1998 – Introduced to MS-DOS 5.0 Avid reader of ”help” command. Windows 95 ”Required”!?! Why? The command line is perfect! Only switched to GUI reluctantly. 2001-2004: The Dark Depths of Java First introduction to vi in 2001 Redhat 7.2, Knoppix, DamnSmallLinux 2004 – PHP 2006 – Ubuntu 6.06 Arrives, immediately embraced.
Why Learn To Use The Shell?
Advantages You're probably already using it. You'll be able to work from nearly any *nix based computer (including servers). Huge set of tools available to make your life easier. It is concise and expressive. Increased productivity.* *Productivity can be arbitrarily defined as ”gettin' 'er done, quickly.”
Disadvantages It can be intimidating. Steep learning curve.
Development Productivity Figure out what you do often. Automate it. Determine which tasks take the most of your time. Can you optimize use of this time? (Yes, that's a simplification.)
Productivity Waste!
What's Bash? Command line environment Scripting environment right at your fingertips All the magic happens right here Fine print: Debian/Ubuntu shell actually uses ”dash”, not bash.
Bash Doesn't mean you have to run Linux locally! Set up a development server and use PuTTY. Install Cygwin inside Windows. Or use ports of GNU Utilities: http://unxutils.sourceforge.net/ Use Mac Terminal. Use a Live CD.
Shell Basics: Keyboard Navigation Getting from one point to another can be difficult at first. Example: mysql> SELECT user.id, user.username, user.email, user.address, user.city, user.state, user.name, userfile.file_name, userfile.metadata, userfile.other_stuff FOM user JOIN userfiles ON user.id = userfile.user_id AND user.id BETWEEN 1 AND 100 AND user.created BETWEEN '2008-10-10' AND '2009-10-10' AND user.is_active = 1 AND user.name LIKE 'Frodo%' LIMIT 10;
Shell Basics: Keyboard Navigation ↑↓ -  Scroll through previous commands Ctrl+U - Clear all before cursor Ctrl+K - Clear all after cursor Ctrl+A - <Home> Ctrl+E - <End> Alt+B - Move cursor back one ”word” (Similar to Ctrl+ ← ) Alt+F - Move cursor forward one ”word” (Ctrl+ -> ) Alt+Bckspc - Delete previous ”word” (Also, Ctrl+W) Alt+. - Recall last command argument Tab - Autocomplete commands, files, folders, and more. Ctrl+L - Clear the screen Ctrl+_ - Undo CTRL+R - Recall previous commands Esc, T - Swap last two words These shortcuts are nearly universal (bash, MySQL) due to use of the GNU Readline library. Exhaustive list is available here:  http://onlyubuntu.blogspot.com/2007/03/bash-shell-keyboard-shortcuts-for-linux.html
Shell Basics: Keyboard Navigation Get used to using Tab for auto-completion! $ svn ci -m”Fixed issue.” fil[tab] ↓ $ svn ci -m”Fixed issue.” filename.php [Tab][Tab] shows all available matches: $ svn ci -m”Fixed issue.” f[tab][tab] filename.php frodo_baggins.php friendly_fellow.php Enable programmable bash completion for further efficiency. $ svn c[tab][tab] cat  checkout  cl  co  copy  changelist  ci  cleanup  commit  cp
Shell Basics: Argument Shortcuts !! - Run the last command (same as  ↑ +return) !my - Runs last cmd starting with ”my”, e.g., ”mysql -uroot -p db” !my:p - Find last cmd starting with ”my” and print it out (don't execute) vi !$  - Replaces !$ with last argument in previous command vi !* - Insert all arguments from previous command A very useful (and more comprehensive) list is available here: http://mail.linux.ie/pipermail/ilug/2006-May/087799.html
Shell Basics: I/O Redirect Concept can be thought of as ”pipes” and ”streams” Negates the need for an application to write to a file, only for another application to read it. Image Source: Wikipedia
Shell Basics: I/O Redirect Basics Basic example of standard output: # format an XML file and feed it out to formatted.xml $ xmllint feed.xml --format > formatted.xml # same as above, but append to existing file. $ xmllint feed.xml --format >> formatted.xml Standard input: $ mysql -uuser -p db1 < schema.sql Or both: $ mysql -uuser -p db1 < schema.sql > log.txt Can also redirect stderr (errors). Use the pipe! $ history | grep svn
Shell Basics: Piping Examples of pipe chaining... Colorize Subversion diff output: $  svn diff | colordiff | less -R Search files for child classes, do not include test files, sort them alphabetically, and paginate. $ grep -r -n -H '^class.*extends.*{$' * | grep -v test | sort | more Cat a text file containing a list of files and execute git add on each one. $ cat files.txt | xargs git add
Shell Basics: Pipe Filters less  – Interactive text pager with search capability. sort  – Sort lines (alphabetically, numerically, etc) grep  – Regular expression searches uniq  – Detect (report or omit) repeated lines sed  – Stream EDitor awk  – powerful pattern scanning and text processing language xargs  – build and execute commands from input There are a lot more of these tools!
Shell Tricks: Pipe Use sed (Stream EDitor) to filter text from git, and automatically add all untracked files. $ git status | \ # display text from ”Untracked” to end of output sed -n '/Untracked/,/$d/p' | \ # only display lines starting with ”#” followed by a tab sed -n '/#\t/p' | \   # Strip out beginning ”#” and whitespace sed 's/#\s*//' | \ # Add the files to the git repo xargs git add But how are you going to remember this command?
Shell Basics: alias Allows you to map a command to a more complicated statement. Examples: # List files in friendly format alias l='ls -l' # Colorize svn diff, use less -R to accept colors alias sdv='svn diff | colordiff | less -R' # Do not allow updates or deletes without WHERE clause. alias mysql='mysql --i-am-a-dummy' Arguments are appended to the command. Save to /etc/profile (or .bash_profile, .bashrc) to automatically load in new sessions.
Shell Basics: alias Limitations # Usage: fun [name] alias fun='echo $1 is having fun!' $ fun Brian is having fun! Brian Alias is only useful as the  beginning  of a command. Arguments  don't work well, can't do loops, etc. Answer? Functions and/or shell scripts!
Shell Tricks: Functions Look up PHP command arguments: phpargs() { # $1 is the first argument passed # get php page from manual mirror curl -s http://us3.php.net/$1 | \ # pull all text in target div sed -n '/<div class=&quot;methodsynopsis dc-description&quot;>/,/<\/div>/p' | \ # strip out HTML tags sed 's/<[^>]*>//g' | \ # strip out line feed tr -d &quot;\n&quot; # add ending line echo } Then from the command line: $ phpargs in_array bool in_array ( mixed $needle, array $haystack [, bool $strict  ] ) The only way to learn scripts is to  write  them!
Shell Trick: Finding Files Use ”find” for quick, local searches (includes all subdirectories): # Get rid of all those Windows executables! $ find /var/www -name '*.exe' -exec rm {} \; # Find all files modified in the past 24 hours. $ find /var/www -mtime -1 Use locate (or slocate) to search filesystem using a file index: # run updatedb nightly to update a file index # search for filename.php, filter out svn matches. $ locate filename.php | grep -v svn
bash: Resources Comprehensive Introduction to the shell http://www.linuxcommand.org/learning_the_shell.php Bash keyboard shortcuts http://onlyubuntu.blogspot.com/2007/03/bash-shell-keyboard-shortcuts-for-linux.html Bash arguments http://www.deadman.org/bash.html Bash programmable completion: http://www.debian-administration.org/articles/316 http://freshmeat.net/projects/bashcompletion/ IBM Bash By Tutorial Series http://www.ibm.com/developerworks/library/l-bash.html http://www.ibm.com/developerworks/library/l-bash2.html http://www.ibm.com/developerworks/library/l-bash3.html
Next Up: Shell Tools Editing text files Multitasking Automated deployment, testing, etc. Benchmarking
Shell Tools: GNU Screen
Shell Tools: GNU Screen What does it do? Command-prompt multi-tasking (multiplex) Detachable workstation If you're accidentally disconnected, easily reconnect without losing anything. Start new screen ”php”: screen -S php Reconnect later: screen -r php Customize your ”status” line so you'll always know what server you're on. Configuration in ~/.screenrc Share screen with another user. Training sessions Code reviews
Shell Tools: GNU Screen Screen allows you to run multiple shells in one terminal. Command key followed by command (usually Ctrl+A, command) 0/9 – Switches between windows n  – Switches to the next available window p – Switches to the previous available A  – Changes window session name K  – Kills a window session c  – Creates a new window [ – Then use arrows to scroll up and down terminal, select and copy text. d – Detach window M – Monitor window for activity ” –  List current windows and names ? – Help
GNU screen: Resources GNU Screen http://magazine.redhat.com/2007/09/27/a-guide-to-gnu-screen/ Screen Configuration Examples http://softpanorama.org/Utilities/Screen/screenrc_examples.shtml
Shell Tools: Vim Most of the usual IDE tools are here: Syntax highlighting Code Completion Error Highlighting Debugging and Profiling (with xdebug plugin) Integration with versioning (CVS, SVN, git) Automatic creation of phpDoc ( /*** )
vim: Syntax Highlighting
vim: Code Completion
Vim: Debugging
vim Tricks Abbreviations :abbr teh the :abbr vd var_dump :abbr fn function(
vim: What Makes It Different? . (Repeat last command) Build Powerful Macros Command line still accessible Highly  customizable Some demonstrations follow...
vim: Resources Andrei Zmievski's excellent vim+PHP guide: http://gravitonic.com/talks/ Include's Andrei's .vim files, plugins, etc. Getting Started with vim and PHP: http://realm3.com/articles/getting_started_with_vim_and_php ” Why, oh WHY, do those #?@! nutheads use vi?” http://www.viemu.com/a-why-vi-vim.html A Slightly Advanced Introduction to Vim http://linuxgazette.net/152/srinivasan.html Integrating xdebug http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/ vim Shortcuts Cheatsheet http://rayninfo.co.uk/vimtips.html
phing: Project Build Tool Styled after Java's ”ant” XML configuration file Cross-platform Automated > Manual Configuration Deployment Testing
phing: Example Generate documentation: <?xml version=&quot;1.0&quot;?> <project name=&quot;myproject.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <target name=&quot;phpdoc&quot;>   <phpdoc title=&quot;Project API Documentation&quot; output=&quot;HTML:Smarty:PHP&quot; sourcecode=&quot;no&quot; destdir=&quot;${docs}&quot; quiet=&quot;true&quot;>   <fileset dir=&quot;.&quot;>   <include name=&quot;**/*.php&quot; />   </fileset>   </phpdoc> </target> </project>
phing: Example Run PHP_Codesniffer: <?xml version=&quot;1.0&quot;?> <project name=&quot;myproject.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;>   <target name=&quot;sniffcode&quot;>   <phpcodesniffer    standard=&quot;Zend&quot;    file=&quot;app_controller.php&quot;   allowedFileExtensions=&quot;php php5 ctp&quot;>   <fileset dir=&quot;.&quot;>   <include name=&quot;app_controller.php&quot; />   </fileset>   </phpcodesniffer>   </target> </project>
phing: Example Deploy via SSH: <?xml version=&quot;1.0&quot;?> <project name=&quot;realm3.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <property name=&quot;live.dir&quot; value=&quot;/path/to/myproject/&quot; /> <property name=&quot;project.tar&quot; value=&quot;myproject.tgz&quot; />   <target name=&quot;deploy&quot; depends=&quot;clean&quot;>   <echo msg=&quot;Copying gz to the server.&quot; />   <exec command=&quot;scp ${tarfile} me@myserver.com:/var/projects/myproject/&quot;  dir=&quot;.&quot; /> <echo msg=&quot;Unzipping on the server.&quot; />   <exec command=&quot;ssh me@myserver.com 'cd {$live.dir}; \ tar -xzvf ${project.tar}; rm ${project.tar}'&quot;  dir=&quot;.&quot; />   <delete file=&quot;${tarfile}&quot;/> </target> </project>
phing Now chain everything together. <?xml version=&quot;1.0&quot;?> <project name=&quot;myproject.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;>   <target name=&quot;phpdoc&quot;>[snip]</target>   <target name=&quot;sniffcode&quot;>[snip]</target>   <target name=&quot;deploy&quot;>[snip]</target>   <target name=&quot;main&quot;  depends=&quot;phpdoc,sniffcode,deploy&quot; > </target> </project> On the command line just run code sniffer: $ phing sniffcode Or to do it all: $ phing
phing: Why use it? Standardize documentation (all developers generating similar documentation) Standardize and automate deployment Run pre-deployment checks Prevent mistakes. Configure files with ”live” settings
phing: Resources User Guide http://phing.info/docs/guide/current/ Shell Scripts no More! http://nsslive.net/2009/02/16/shell-scripts-no-more/ Phing: Building With PHP http://www.slideshare.net/hozn/phing-building-with-php
Shell Tools: Benchmarking Get a quick and dirty idea of how your code might perform under a heavy load. ab -  Apache HTTP server benchmarking tool siege -  Siege is an http regression testing and benchmarking utility. It was designed to let web developers measure the performance of their code under duress, to see how it will stand up to load on the internet. Siege supports basic authentication, cookies, HTTP and HTTPS protocols. It allows the user hit a web server with a configurable number of concurrent simulated users. Those users place the webserver &quot;under siege.&quot;  According to Paul M. Jones, siege is more accurate. http://paul-m-jones.com/?p=413
Shell Tools: siege $ seige  http://www.example.com ** SIEGE 2.66 ** Preparing 15 concurrent users for battle. The server is now under siege... HTTP/1.1 200  0.75 secs:  2912 bytes ==> / [...snip...] Lifting the server siege...  done. Transactions:  32 hits Availability:  78.05 % Elapsed time:  24.30 secs Data transferred:  0.09 MB Response time:  2.55 secs Transaction rate:  1.32 trans/sec Throughput:  0.00 MB/sec Concurrency:  3.35 Successful transactions:  32 Failed transactions:  9 Longest transaction:  8.51 Shortest transaction:  0.56
Again, Why learn all of these tools? Automate repetitive tasks. Make complex tasks simpler. Ensure best practices are followed. Don't re-invent the wheel.
Questions?
Contact & Credits [realm3.com] brian /at/ realm3.com twitter.com/brian_dailey (917) 512-3594 Credit to: Andrew Yochum http://plexpod.com Gennady Feldman http://www.gena01.com

More Related Content

PPTX
Easiest way to start with Shell scripting
Akshay Siwal
 
PDF
Shell scripting
Manav Prasad
 
PPT
Unix And Shell Scripting
Jaibeer Malik
 
PPTX
Unix shell scripting
Pavan Devarakonda
 
PPT
Unix Shell Scripting Basics
Dr.Ravi
 
PPTX
Unix shell scripting basics
Manav Prasad
 
PDF
Introduction to shell scripting
Corrado Santoro
 
Easiest way to start with Shell scripting
Akshay Siwal
 
Shell scripting
Manav Prasad
 
Unix And Shell Scripting
Jaibeer Malik
 
Unix shell scripting
Pavan Devarakonda
 
Unix Shell Scripting Basics
Dr.Ravi
 
Unix shell scripting basics
Manav Prasad
 
Introduction to shell scripting
Corrado Santoro
 

What's hot (20)

PPT
Unix Shell Scripting Basics
Sudharsan S
 
PPT
Shell scripting - By Vu Duy Tu from eXo Platform SEA
Thuy_Dang
 
PDF
Shell scripting
Ashrith Mekala
 
PPT
Shell Scripting
Gaurav Shinde
 
ODP
OpenGurukul : Language : Shell Scripting
Open Gurukul
 
PDF
Linux system admin
Mohammed Zainul Abiddin
 
PPTX
Unix - Shell Scripts
ananthimurugesan
 
ODP
DevChatt 2010 - *nix Cmd Line Kung Foo
brian_dailey
 
PPT
Chap06
Dr.Ravi
 
PDF
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Zyxware Technologies
 
PPT
Bash shell
xylas121
 
PDF
Shell scripting
Geeks Anonymes
 
PPTX
Bash Shell Scripting
Raghu nath
 
PPT
Unix Basics
Dr.Ravi
 
PDF
Quick start bash script
Simon Su
 
PDF
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Aaron Zauner
 
PPTX
Shell & Shell Script
Amit Ghosh
 
PDF
COSCUP2012: How to write a bash script like the python?
Lloyd Huang
 
PDF
BASH Guide Summary
Ohgyun Ahn
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
Unix Shell Scripting Basics
Sudharsan S
 
Shell scripting - By Vu Duy Tu from eXo Platform SEA
Thuy_Dang
 
Shell scripting
Ashrith Mekala
 
Shell Scripting
Gaurav Shinde
 
OpenGurukul : Language : Shell Scripting
Open Gurukul
 
Linux system admin
Mohammed Zainul Abiddin
 
Unix - Shell Scripts
ananthimurugesan
 
DevChatt 2010 - *nix Cmd Line Kung Foo
brian_dailey
 
Chap06
Dr.Ravi
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Zyxware Technologies
 
Bash shell
xylas121
 
Shell scripting
Geeks Anonymes
 
Bash Shell Scripting
Raghu nath
 
Unix Basics
Dr.Ravi
 
Quick start bash script
Simon Su
 
Beautiful Bash: Let's make reading and writing bash scripts fun again!
Aaron Zauner
 
Shell & Shell Script
Amit Ghosh
 
COSCUP2012: How to write a bash script like the python?
Lloyd Huang
 
BASH Guide Summary
Ohgyun Ahn
 
Ad

Viewers also liked (20)

PPTX
Make-up
silvia_ruggeri
 
PDF
Khrsheed khawar peshawar night Part-2
Ahmed Hashmi
 
PPTX
Managing service management skills
Lex Hendriks
 
PDF
Crystallized040910
klee4vp
 
PDF
Draft A Snapshot Guide To Intellectual Property Systems
CGIAR Central Advisory Service on Intellectual Property
 
PPTX
SVH In Vogelvlucht Roc Aventus 2 3 2011
Johan Lapidaire
 
PPTX
Warandecollege 05102010
Johan Lapidaire
 
PPT
Presentatie 27 Mei Cluster Htv
Johan Lapidaire
 
PPT
The Secret To Employee Happinesss
jaltman
 
PPTX
One Unified Platform for Deploying Enterprise Class Solutions across any ente...
trw188
 
PDF
100mph, Stage 2: Strategic Digital Marketing Activation
A Better Version of You
 
PDF
Brandbook paymantix 2015
Vadim Andreev
 
PPT
Chamber Of Commerce Meeting July 2010
Surrey Beekeeper
 
PDF
clx_q4fy04
finance48
 
PPTX
عرض تقديمي1
J00D
 
PPTX
Leermeester Centraal Leermeesterdag NN 8-3-2011
Johan Lapidaire
 
PPT
jose juan
guest5653c8
 
PDF
OFE draft 9 21 mitchell baker
chefhja
 
PPTX
MAKE-UP - interattiviamoci_gruppo2
silvia_ruggeri
 
PDF
Code Qualität in agilen Teams - code.talks Hamburg 2015
Frank Sons
 
Khrsheed khawar peshawar night Part-2
Ahmed Hashmi
 
Managing service management skills
Lex Hendriks
 
Crystallized040910
klee4vp
 
Draft A Snapshot Guide To Intellectual Property Systems
CGIAR Central Advisory Service on Intellectual Property
 
SVH In Vogelvlucht Roc Aventus 2 3 2011
Johan Lapidaire
 
Warandecollege 05102010
Johan Lapidaire
 
Presentatie 27 Mei Cluster Htv
Johan Lapidaire
 
The Secret To Employee Happinesss
jaltman
 
One Unified Platform for Deploying Enterprise Class Solutions across any ente...
trw188
 
100mph, Stage 2: Strategic Digital Marketing Activation
A Better Version of You
 
Brandbook paymantix 2015
Vadim Andreev
 
Chamber Of Commerce Meeting July 2010
Surrey Beekeeper
 
clx_q4fy04
finance48
 
عرض تقديمي1
J00D
 
Leermeester Centraal Leermeesterdag NN 8-3-2011
Johan Lapidaire
 
jose juan
guest5653c8
 
OFE draft 9 21 mitchell baker
chefhja
 
MAKE-UP - interattiviamoci_gruppo2
silvia_ruggeri
 
Code Qualität in agilen Teams - code.talks Hamburg 2015
Frank Sons
 
Ad

Similar to NYPHP March 2009 Presentation (20)

PPT
Bioinformatica 29-09-2011-p1-introduction
Prof. Wim Van Criekinge
 
PPTX
RHCSA EX200 - Summary
Nugroho Gito
 
ODP
Linuxs1
rajikaa
 
PDF
Command line for the beginner - Using the command line in developing for the...
Jim Birch
 
PPT
Linux
Rathan Raj
 
ODP
Vim and Python
majmcdonald
 
ODP
Nithi
sharmibalu
 
PPTX
Power shell training
David Brabant
 
PPT
Shell programming
Moayad Moawiah
 
PPTX
SHELL PROGRAMMING
jinal thakrar
 
DOCX
lec4.docx
ismailaboshatra
 
PPT
Linux presentation
Ajaigururaj R
 
PPT
Power point on linux commands,appache,php,mysql,html,css,web 2.0
venkatakrishnan k
 
PDF
Aucklug slides - desktop tips and tricks
Glen Ogilvie
 
PPT
No-script PowerShell v2
Concentrated Technology
 
PDF
May The Nodejs Be With You
Dalibor Gogic
 
PDF
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
 
ODP
Linuxppt
poornima sugumaran
 
PPT
Linux
sravan kumar
 
DOCX
50 Most Frequently Used UNIX Linux Commands -hmftj
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Bioinformatica 29-09-2011-p1-introduction
Prof. Wim Van Criekinge
 
RHCSA EX200 - Summary
Nugroho Gito
 
Linuxs1
rajikaa
 
Command line for the beginner - Using the command line in developing for the...
Jim Birch
 
Linux
Rathan Raj
 
Vim and Python
majmcdonald
 
Nithi
sharmibalu
 
Power shell training
David Brabant
 
Shell programming
Moayad Moawiah
 
SHELL PROGRAMMING
jinal thakrar
 
lec4.docx
ismailaboshatra
 
Linux presentation
Ajaigururaj R
 
Power point on linux commands,appache,php,mysql,html,css,web 2.0
venkatakrishnan k
 
Aucklug slides - desktop tips and tricks
Glen Ogilvie
 
No-script PowerShell v2
Concentrated Technology
 
May The Nodejs Be With You
Dalibor Gogic
 
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy
 
50 Most Frequently Used UNIX Linux Commands -hmftj
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 

Recently uploaded (20)

PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Software Development Methodologies in 2025
KodekX
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 

NYPHP March 2009 Presentation

  • 1. Becoming A Bash Ninja New York PHP March 24, 2009 Brian Dailey [email_address] http://realm3.com/
  • 2. Becoming A Bash Ninja Agenda Bash Basics Keyboard Navigation Shell Shortcuts Piping Shell Tools GNU Screen vim phing Benchmarking (ab/siege)
  • 3. About Me 1998 – Introduced to MS-DOS 5.0 Avid reader of ”help” command. Windows 95 ”Required”!?! Why? The command line is perfect! Only switched to GUI reluctantly. 2001-2004: The Dark Depths of Java First introduction to vi in 2001 Redhat 7.2, Knoppix, DamnSmallLinux 2004 – PHP 2006 – Ubuntu 6.06 Arrives, immediately embraced.
  • 4. Why Learn To Use The Shell?
  • 5. Advantages You're probably already using it. You'll be able to work from nearly any *nix based computer (including servers). Huge set of tools available to make your life easier. It is concise and expressive. Increased productivity.* *Productivity can be arbitrarily defined as ”gettin' 'er done, quickly.”
  • 6. Disadvantages It can be intimidating. Steep learning curve.
  • 7. Development Productivity Figure out what you do often. Automate it. Determine which tasks take the most of your time. Can you optimize use of this time? (Yes, that's a simplification.)
  • 9. What's Bash? Command line environment Scripting environment right at your fingertips All the magic happens right here Fine print: Debian/Ubuntu shell actually uses ”dash”, not bash.
  • 10. Bash Doesn't mean you have to run Linux locally! Set up a development server and use PuTTY. Install Cygwin inside Windows. Or use ports of GNU Utilities: http://unxutils.sourceforge.net/ Use Mac Terminal. Use a Live CD.
  • 11. Shell Basics: Keyboard Navigation Getting from one point to another can be difficult at first. Example: mysql> SELECT user.id, user.username, user.email, user.address, user.city, user.state, user.name, userfile.file_name, userfile.metadata, userfile.other_stuff FOM user JOIN userfiles ON user.id = userfile.user_id AND user.id BETWEEN 1 AND 100 AND user.created BETWEEN '2008-10-10' AND '2009-10-10' AND user.is_active = 1 AND user.name LIKE 'Frodo%' LIMIT 10;
  • 12. Shell Basics: Keyboard Navigation ↑↓ - Scroll through previous commands Ctrl+U - Clear all before cursor Ctrl+K - Clear all after cursor Ctrl+A - <Home> Ctrl+E - <End> Alt+B - Move cursor back one ”word” (Similar to Ctrl+ ← ) Alt+F - Move cursor forward one ”word” (Ctrl+ -> ) Alt+Bckspc - Delete previous ”word” (Also, Ctrl+W) Alt+. - Recall last command argument Tab - Autocomplete commands, files, folders, and more. Ctrl+L - Clear the screen Ctrl+_ - Undo CTRL+R - Recall previous commands Esc, T - Swap last two words These shortcuts are nearly universal (bash, MySQL) due to use of the GNU Readline library. Exhaustive list is available here: http://onlyubuntu.blogspot.com/2007/03/bash-shell-keyboard-shortcuts-for-linux.html
  • 13. Shell Basics: Keyboard Navigation Get used to using Tab for auto-completion! $ svn ci -m”Fixed issue.” fil[tab] ↓ $ svn ci -m”Fixed issue.” filename.php [Tab][Tab] shows all available matches: $ svn ci -m”Fixed issue.” f[tab][tab] filename.php frodo_baggins.php friendly_fellow.php Enable programmable bash completion for further efficiency. $ svn c[tab][tab] cat checkout cl co copy changelist ci cleanup commit cp
  • 14. Shell Basics: Argument Shortcuts !! - Run the last command (same as ↑ +return) !my - Runs last cmd starting with ”my”, e.g., ”mysql -uroot -p db” !my:p - Find last cmd starting with ”my” and print it out (don't execute) vi !$ - Replaces !$ with last argument in previous command vi !* - Insert all arguments from previous command A very useful (and more comprehensive) list is available here: http://mail.linux.ie/pipermail/ilug/2006-May/087799.html
  • 15. Shell Basics: I/O Redirect Concept can be thought of as ”pipes” and ”streams” Negates the need for an application to write to a file, only for another application to read it. Image Source: Wikipedia
  • 16. Shell Basics: I/O Redirect Basics Basic example of standard output: # format an XML file and feed it out to formatted.xml $ xmllint feed.xml --format > formatted.xml # same as above, but append to existing file. $ xmllint feed.xml --format >> formatted.xml Standard input: $ mysql -uuser -p db1 < schema.sql Or both: $ mysql -uuser -p db1 < schema.sql > log.txt Can also redirect stderr (errors). Use the pipe! $ history | grep svn
  • 17. Shell Basics: Piping Examples of pipe chaining... Colorize Subversion diff output: $ svn diff | colordiff | less -R Search files for child classes, do not include test files, sort them alphabetically, and paginate. $ grep -r -n -H '^class.*extends.*{$' * | grep -v test | sort | more Cat a text file containing a list of files and execute git add on each one. $ cat files.txt | xargs git add
  • 18. Shell Basics: Pipe Filters less – Interactive text pager with search capability. sort – Sort lines (alphabetically, numerically, etc) grep – Regular expression searches uniq – Detect (report or omit) repeated lines sed – Stream EDitor awk – powerful pattern scanning and text processing language xargs – build and execute commands from input There are a lot more of these tools!
  • 19. Shell Tricks: Pipe Use sed (Stream EDitor) to filter text from git, and automatically add all untracked files. $ git status | \ # display text from ”Untracked” to end of output sed -n '/Untracked/,/$d/p' | \ # only display lines starting with ”#” followed by a tab sed -n '/#\t/p' | \ # Strip out beginning ”#” and whitespace sed 's/#\s*//' | \ # Add the files to the git repo xargs git add But how are you going to remember this command?
  • 20. Shell Basics: alias Allows you to map a command to a more complicated statement. Examples: # List files in friendly format alias l='ls -l' # Colorize svn diff, use less -R to accept colors alias sdv='svn diff | colordiff | less -R' # Do not allow updates or deletes without WHERE clause. alias mysql='mysql --i-am-a-dummy' Arguments are appended to the command. Save to /etc/profile (or .bash_profile, .bashrc) to automatically load in new sessions.
  • 21. Shell Basics: alias Limitations # Usage: fun [name] alias fun='echo $1 is having fun!' $ fun Brian is having fun! Brian Alias is only useful as the beginning of a command. Arguments don't work well, can't do loops, etc. Answer? Functions and/or shell scripts!
  • 22. Shell Tricks: Functions Look up PHP command arguments: phpargs() { # $1 is the first argument passed # get php page from manual mirror curl -s http://us3.php.net/$1 | \ # pull all text in target div sed -n '/<div class=&quot;methodsynopsis dc-description&quot;>/,/<\/div>/p' | \ # strip out HTML tags sed 's/<[^>]*>//g' | \ # strip out line feed tr -d &quot;\n&quot; # add ending line echo } Then from the command line: $ phpargs in_array bool in_array ( mixed $needle, array $haystack [, bool $strict ] ) The only way to learn scripts is to write them!
  • 23. Shell Trick: Finding Files Use ”find” for quick, local searches (includes all subdirectories): # Get rid of all those Windows executables! $ find /var/www -name '*.exe' -exec rm {} \; # Find all files modified in the past 24 hours. $ find /var/www -mtime -1 Use locate (or slocate) to search filesystem using a file index: # run updatedb nightly to update a file index # search for filename.php, filter out svn matches. $ locate filename.php | grep -v svn
  • 24. bash: Resources Comprehensive Introduction to the shell http://www.linuxcommand.org/learning_the_shell.php Bash keyboard shortcuts http://onlyubuntu.blogspot.com/2007/03/bash-shell-keyboard-shortcuts-for-linux.html Bash arguments http://www.deadman.org/bash.html Bash programmable completion: http://www.debian-administration.org/articles/316 http://freshmeat.net/projects/bashcompletion/ IBM Bash By Tutorial Series http://www.ibm.com/developerworks/library/l-bash.html http://www.ibm.com/developerworks/library/l-bash2.html http://www.ibm.com/developerworks/library/l-bash3.html
  • 25. Next Up: Shell Tools Editing text files Multitasking Automated deployment, testing, etc. Benchmarking
  • 27. Shell Tools: GNU Screen What does it do? Command-prompt multi-tasking (multiplex) Detachable workstation If you're accidentally disconnected, easily reconnect without losing anything. Start new screen ”php”: screen -S php Reconnect later: screen -r php Customize your ”status” line so you'll always know what server you're on. Configuration in ~/.screenrc Share screen with another user. Training sessions Code reviews
  • 28. Shell Tools: GNU Screen Screen allows you to run multiple shells in one terminal. Command key followed by command (usually Ctrl+A, command) 0/9 – Switches between windows n – Switches to the next available window p – Switches to the previous available A – Changes window session name K – Kills a window session c – Creates a new window [ – Then use arrows to scroll up and down terminal, select and copy text. d – Detach window M – Monitor window for activity ” – List current windows and names ? – Help
  • 29. GNU screen: Resources GNU Screen http://magazine.redhat.com/2007/09/27/a-guide-to-gnu-screen/ Screen Configuration Examples http://softpanorama.org/Utilities/Screen/screenrc_examples.shtml
  • 30. Shell Tools: Vim Most of the usual IDE tools are here: Syntax highlighting Code Completion Error Highlighting Debugging and Profiling (with xdebug plugin) Integration with versioning (CVS, SVN, git) Automatic creation of phpDoc ( /*** )
  • 34. vim Tricks Abbreviations :abbr teh the :abbr vd var_dump :abbr fn function(
  • 35. vim: What Makes It Different? . (Repeat last command) Build Powerful Macros Command line still accessible Highly  customizable Some demonstrations follow...
  • 36. vim: Resources Andrei Zmievski's excellent vim+PHP guide: http://gravitonic.com/talks/ Include's Andrei's .vim files, plugins, etc. Getting Started with vim and PHP: http://realm3.com/articles/getting_started_with_vim_and_php ” Why, oh WHY, do those #?@! nutheads use vi?” http://www.viemu.com/a-why-vi-vim.html A Slightly Advanced Introduction to Vim http://linuxgazette.net/152/srinivasan.html Integrating xdebug http://tech.blog.box.net/2007/06/20/how-to-debug-php-with-vim-and-xdebug-on-linux/ vim Shortcuts Cheatsheet http://rayninfo.co.uk/vimtips.html
  • 37. phing: Project Build Tool Styled after Java's ”ant” XML configuration file Cross-platform Automated > Manual Configuration Deployment Testing
  • 38. phing: Example Generate documentation: <?xml version=&quot;1.0&quot;?> <project name=&quot;myproject.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <target name=&quot;phpdoc&quot;> <phpdoc title=&quot;Project API Documentation&quot; output=&quot;HTML:Smarty:PHP&quot; sourcecode=&quot;no&quot; destdir=&quot;${docs}&quot; quiet=&quot;true&quot;> <fileset dir=&quot;.&quot;> <include name=&quot;**/*.php&quot; /> </fileset> </phpdoc> </target> </project>
  • 39. phing: Example Run PHP_Codesniffer: <?xml version=&quot;1.0&quot;?> <project name=&quot;myproject.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <target name=&quot;sniffcode&quot;> <phpcodesniffer standard=&quot;Zend&quot; file=&quot;app_controller.php&quot; allowedFileExtensions=&quot;php php5 ctp&quot;> <fileset dir=&quot;.&quot;> <include name=&quot;app_controller.php&quot; /> </fileset> </phpcodesniffer> </target> </project>
  • 40. phing: Example Deploy via SSH: <?xml version=&quot;1.0&quot;?> <project name=&quot;realm3.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <property name=&quot;live.dir&quot; value=&quot;/path/to/myproject/&quot; /> <property name=&quot;project.tar&quot; value=&quot;myproject.tgz&quot; /> <target name=&quot;deploy&quot; depends=&quot;clean&quot;> <echo msg=&quot;Copying gz to the server.&quot; /> <exec command=&quot;scp ${tarfile} [email protected]:/var/projects/myproject/&quot; dir=&quot;.&quot; /> <echo msg=&quot;Unzipping on the server.&quot; /> <exec command=&quot;ssh [email protected] 'cd {$live.dir}; \ tar -xzvf ${project.tar}; rm ${project.tar}'&quot; dir=&quot;.&quot; /> <delete file=&quot;${tarfile}&quot;/> </target> </project>
  • 41. phing Now chain everything together. <?xml version=&quot;1.0&quot;?> <project name=&quot;myproject.com&quot; default=&quot;main&quot; basedir=&quot;.&quot;> <target name=&quot;phpdoc&quot;>[snip]</target> <target name=&quot;sniffcode&quot;>[snip]</target> <target name=&quot;deploy&quot;>[snip]</target> <target name=&quot;main&quot; depends=&quot;phpdoc,sniffcode,deploy&quot; > </target> </project> On the command line just run code sniffer: $ phing sniffcode Or to do it all: $ phing
  • 42. phing: Why use it? Standardize documentation (all developers generating similar documentation) Standardize and automate deployment Run pre-deployment checks Prevent mistakes. Configure files with ”live” settings
  • 43. phing: Resources User Guide http://phing.info/docs/guide/current/ Shell Scripts no More! http://nsslive.net/2009/02/16/shell-scripts-no-more/ Phing: Building With PHP http://www.slideshare.net/hozn/phing-building-with-php
  • 44. Shell Tools: Benchmarking Get a quick and dirty idea of how your code might perform under a heavy load. ab - Apache HTTP server benchmarking tool siege - Siege is an http regression testing and benchmarking utility. It was designed to let web developers measure the performance of their code under duress, to see how it will stand up to load on the internet. Siege supports basic authentication, cookies, HTTP and HTTPS protocols. It allows the user hit a web server with a configurable number of concurrent simulated users. Those users place the webserver &quot;under siege.&quot; According to Paul M. Jones, siege is more accurate. http://paul-m-jones.com/?p=413
  • 45. Shell Tools: siege $ seige http://www.example.com ** SIEGE 2.66 ** Preparing 15 concurrent users for battle. The server is now under siege... HTTP/1.1 200 0.75 secs: 2912 bytes ==> / [...snip...] Lifting the server siege... done. Transactions: 32 hits Availability: 78.05 % Elapsed time: 24.30 secs Data transferred: 0.09 MB Response time: 2.55 secs Transaction rate: 1.32 trans/sec Throughput: 0.00 MB/sec Concurrency: 3.35 Successful transactions: 32 Failed transactions: 9 Longest transaction: 8.51 Shortest transaction: 0.56
  • 46. Again, Why learn all of these tools? Automate repetitive tasks. Make complex tasks simpler. Ensure best practices are followed. Don't re-invent the wheel.
  • 48. Contact & Credits [realm3.com] brian /at/ realm3.com twitter.com/brian_dailey (917) 512-3594 Credit to: Andrew Yochum http://plexpod.com Gennady Feldman http://www.gena01.com