Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

NYPHP March 2009 Presentation


Published on

March 24, 2009 presentation at NYPHP.

Published in: Technology
  • Be the first to comment

NYPHP March 2009 Presentation

  1. 1. Becoming A Bash Ninja <ul><li>New York PHP </li></ul><ul><li>March 24, 2009 </li></ul><ul><li>Brian Dailey </li></ul><ul><li>[email_address] </li></ul><ul><li> </li></ul>
  2. 2. Becoming A Bash Ninja <ul><li>Agenda </li></ul><ul><li>Bash Basics </li></ul><ul><ul><li>Keyboard Navigation </li></ul></ul><ul><ul><li>Shell Shortcuts </li></ul></ul><ul><ul><li>Piping </li></ul></ul><ul><li>Shell Tools </li></ul><ul><ul><li>GNU Screen </li></ul></ul><ul><ul><li>vim </li></ul></ul><ul><ul><li>phing </li></ul></ul><ul><ul><li>Benchmarking (ab/siege) </li></ul></ul>
  3. 3. About Me <ul><li>1998 – Introduced to MS-DOS 5.0 </li></ul><ul><ul><li>Avid reader of ”help” command. </li></ul></ul><ul><ul><li>Windows 95 ”Required”!?! Why? The command line is perfect! </li></ul></ul><ul><ul><li>Only switched to GUI reluctantly. </li></ul></ul><ul><li>2001-2004: The Dark Depths of Java </li></ul><ul><ul><li>First introduction to vi in 2001 </li></ul></ul><ul><ul><li>Redhat 7.2, Knoppix, DamnSmallLinux </li></ul></ul><ul><li>2004 – PHP </li></ul><ul><li>2006 – Ubuntu 6.06 Arrives, immediately embraced. </li></ul>
  4. 4. Why Learn To Use The Shell?
  5. 5. Advantages <ul><li>You're probably already using it. </li></ul><ul><li>You'll be able to work from nearly any *nix based computer (including servers). </li></ul><ul><li>Huge set of tools available to make your life easier. </li></ul><ul><li>It is concise and expressive. </li></ul><ul><li>Increased productivity.* </li></ul><ul><li>*Productivity can be arbitrarily defined as ”gettin' 'er done, quickly.” </li></ul>
  6. 6. Disadvantages <ul><li>It can be intimidating. </li></ul><ul><li>Steep learning curve. </li></ul>
  7. 7. Development Productivity <ul><li>Figure out what you do often. </li></ul><ul><ul><li>Automate it. </li></ul></ul><ul><li>Determine which tasks take the most of your time. </li></ul><ul><ul><li>Can you optimize use of this time? </li></ul></ul><ul><li>(Yes, that's a simplification.) </li></ul>
  8. 8. Productivity Waste!
  9. 9. What's Bash? <ul><li>Command line environment </li></ul><ul><li>Scripting environment right at your fingertips </li></ul><ul><li>All the magic happens right here </li></ul><ul><li>Fine print: Debian/Ubuntu shell actually uses ”dash”, not bash. </li></ul>
  10. 10. Bash <ul><li>Doesn't mean you have to run Linux locally! </li></ul><ul><li>Set up a development server and use PuTTY. </li></ul><ul><li>Install Cygwin inside Windows. </li></ul><ul><ul><li>Or use ports of GNU Utilities: </li></ul></ul><ul><li>Use Mac Terminal. </li></ul><ul><li>Use a Live CD. </li></ul>
  11. 11. Shell Basics: Keyboard Navigation <ul><li>Getting from one point to another can be difficult at first. Example: </li></ul><ul><li>mysql> SELECT, user.username,, user.address,, user.state,, userfile.file_name, userfile.metadata, userfile.other_stuff FOM user JOIN userfiles ON = userfile.user_id AND BETWEEN 1 AND 100 AND user.created BETWEEN '2008-10-10' AND '2009-10-10' AND user.is_active = 1 AND LIKE 'Frodo%' LIMIT 10; </li></ul>
  12. 12. Shell Basics: Keyboard Navigation <ul><li>↑↓ - 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 </li></ul><ul><li>These shortcuts are nearly universal (bash, MySQL) due to use of the GNU Readline library. Exhaustive list is available here: </li></ul>
  13. 13. Shell Basics: Keyboard Navigation <ul><li>Get used to using Tab for auto-completion! $ svn ci -m”Fixed issue.” fil[tab] ↓ $ svn ci -m”Fixed issue.” filename.php </li></ul><ul><li>[Tab][Tab] shows all available matches: $ svn ci -m”Fixed issue.” f[tab][tab] filename.php frodo_baggins.php friendly_fellow.php </li></ul><ul><li>Enable programmable bash completion for further efficiency. $ svn c[tab][tab] cat checkout cl co copy changelist ci cleanup commit cp </li></ul>
  14. 14. Shell Basics: Argument Shortcuts <ul><li>!! - 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 </li></ul><ul><li>A very useful (and more comprehensive) list is available here: </li></ul>
  15. 15. Shell Basics: I/O Redirect <ul><li>Concept can be thought of as ”pipes” and ”streams” </li></ul><ul><li>Negates the need for an application to write to a file, only for another application to read it. </li></ul><ul><ul><li>Image Source: Wikipedia </li></ul></ul>
  16. 16. Shell Basics: I/O Redirect Basics <ul><li>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 </li></ul><ul><li>Standard input: $ mysql -uuser -p db1 < schema.sql </li></ul><ul><li>Or both: $ mysql -uuser -p db1 < schema.sql > log.txt </li></ul><ul><li>Can also redirect stderr (errors). </li></ul><ul><li>Use the pipe! $ history | grep svn </li></ul>
  17. 17. Shell Basics: Piping <ul><li>Examples of pipe chaining... </li></ul><ul><li>Colorize Subversion diff output: $ svn diff | colordiff | less -R </li></ul><ul><li>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 </li></ul><ul><li>Cat a text file containing a list of files and execute git add on each one. $ cat files.txt | xargs git add </li></ul>
  18. 18. Shell Basics: Pipe Filters <ul><li>less – Interactive text pager with search capability. </li></ul><ul><li>sort – Sort lines (alphabetically, numerically, etc) </li></ul><ul><li>grep – Regular expression searches </li></ul><ul><li>uniq – Detect (report or omit) repeated lines </li></ul><ul><li>sed – Stream EDitor </li></ul><ul><li>awk – powerful pattern scanning and text processing language </li></ul><ul><li>xargs – build and execute commands from input </li></ul><ul><li>There are a lot more of these tools! </li></ul>
  19. 19. Shell Tricks: Pipe <ul><li>Use sed (Stream EDitor) to filter text from git, and automatically add all untracked files. </li></ul><ul><li>$ 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 '/# /p' | # Strip out beginning ”#” and whitespace sed 's/#s*//' | # Add the files to the git repo xargs git add </li></ul><ul><li>But how are you going to remember this command? </li></ul>
  20. 20. Shell Basics: alias <ul><li>Allows you to map a command to a more complicated statement. </li></ul><ul><li>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' </li></ul><ul><li>Arguments are appended to the command. </li></ul><ul><li>Save to /etc/profile (or .bash_profile, .bashrc) to automatically load in new sessions. </li></ul>
  21. 21. Shell Basics: alias Limitations <ul><li># Usage: fun [name] alias fun='echo $1 is having fun!' $ fun Brian is having fun! Brian </li></ul><ul><li>Alias is only useful as the beginning of a command. </li></ul><ul><li>Arguments don't work well, can't do loops, etc. </li></ul><ul><li>Answer? Functions and/or shell scripts! </li></ul>
  22. 22. Shell Tricks: Functions <ul><li>Look up PHP command arguments: phpargs() { # $1 is the first argument passed # get php page from manual mirror curl -s$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; &quot; # add ending line echo } </li></ul><ul><li>Then from the command line: $ phpargs in_array bool in_array ( mixed $needle, array $haystack [, bool $strict ] ) </li></ul><ul><li>The only way to learn scripts is to write them! </li></ul>
  23. 23. Shell Trick: Finding Files <ul><li>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 </li></ul><ul><li>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 </li></ul>
  24. 24. bash: Resources <ul><li>Comprehensive Introduction to the shell </li></ul><ul><li>Bash keyboard shortcuts </li></ul><ul><li>Bash arguments </li></ul><ul><li>Bash programmable completion: </li></ul><ul><li>IBM Bash By Tutorial Series </li></ul>
  25. 25. Next Up: Shell Tools <ul><li>Editing text files </li></ul><ul><li>Multitasking </li></ul><ul><li>Automated deployment, testing, etc. </li></ul><ul><li>Benchmarking </li></ul>
  26. 26. Shell Tools: GNU Screen
  27. 27. Shell Tools: GNU Screen <ul><li>What does it do? </li></ul><ul><li>Command-prompt multi-tasking (multiplex) </li></ul><ul><li>Detachable workstation </li></ul><ul><ul><li>If you're accidentally disconnected, easily reconnect without losing anything. </li></ul></ul><ul><ul><li>Start new screen ”php”: screen -S php </li></ul></ul><ul><ul><li>Reconnect later: screen -r php </li></ul></ul><ul><li>Customize your ”status” line so you'll always know what server you're on. </li></ul><ul><ul><li>Configuration in ~/.screenrc </li></ul></ul><ul><li>Share screen with another user. </li></ul><ul><ul><li>Training sessions </li></ul></ul><ul><ul><li>Code reviews </li></ul></ul>
  28. 28. Shell Tools: GNU Screen <ul><li>Screen allows you to run multiple shells in one terminal. </li></ul><ul><li>Command key followed by command (usually Ctrl+A, command) </li></ul><ul><li>0/9 – Switches between windows </li></ul><ul><li>n – Switches to the next available window </li></ul><ul><li>p – Switches to the previous available </li></ul><ul><li>A – Changes window session name </li></ul><ul><li>K – Kills a window session </li></ul><ul><li>c – Creates a new window </li></ul><ul><li>[ – Then use arrows to scroll up and down terminal, select and copy text. </li></ul><ul><li>d – Detach window </li></ul><ul><li>M – Monitor window for activity </li></ul><ul><li>” – List current windows and names </li></ul><ul><li>? – Help </li></ul>
  29. 29. GNU screen: Resources <ul><li>GNU Screen </li></ul><ul><li>Screen Configuration Examples </li></ul>
  30. 30. Shell Tools: Vim <ul><li>Most of the usual IDE tools are here: </li></ul><ul><ul><li>Syntax highlighting </li></ul></ul><ul><ul><li>Code Completion </li></ul></ul><ul><ul><li>Error Highlighting </li></ul></ul><ul><ul><li>Debugging and Profiling (with xdebug plugin) </li></ul></ul><ul><ul><li>Integration with versioning (CVS, SVN, git) </li></ul></ul><ul><ul><li>Automatic creation of phpDoc ( /*** ) </li></ul></ul>
  31. 31. vim: Syntax Highlighting
  32. 32. vim: Code Completion
  33. 33. Vim: Debugging
  34. 34. vim Tricks <ul><li>Abbreviations :abbr teh the :abbr vd var_dump :abbr fn function( </li></ul>
  35. 35. vim: What Makes It Different? <ul><li>. (Repeat last command) </li></ul><ul><li>Build Powerful Macros </li></ul><ul><li>Command line still accessible </li></ul><ul><li>Highly  customizable </li></ul><ul><li>Some demonstrations follow... </li></ul>
  36. 36. vim: Resources <ul><li>Andrei Zmievski's excellent vim+PHP guide: Include's Andrei's .vim files, plugins, etc. </li></ul><ul><li>Getting Started with vim and PHP: </li></ul><ul><li>” Why, oh WHY, do those #?@! nutheads use vi?” </li></ul><ul><li>A Slightly Advanced Introduction to Vim </li></ul><ul><li>Integrating xdebug </li></ul><ul><li>vim Shortcuts Cheatsheet </li></ul>
  37. 37. phing: Project Build Tool <ul><li>Styled after Java's ”ant” </li></ul><ul><ul><li>XML configuration file </li></ul></ul><ul><ul><li>Cross-platform </li></ul></ul><ul><li>Automated > Manual </li></ul><ul><ul><li>Configuration </li></ul></ul><ul><ul><li>Deployment </li></ul></ul><ul><ul><li>Testing </li></ul></ul>
  38. 38. phing: Example <ul><li>Generate documentation: <?xml version=&quot;1.0&quot;?> <project name=&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> </li></ul>
  39. 39. phing: Example <ul><li>Run PHP_Codesniffer: <?xml version=&quot;1.0&quot;?> <project name=&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> </li></ul>
  40. 40. phing: Example <ul><li>Deploy via SSH: <?xml version=&quot;1.0&quot;?> <project name=&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}; dir=&quot;.&quot; /> <echo msg=&quot;Unzipping on the server.&quot; /> <exec command=&quot;ssh 'cd {$live.dir}; tar -xzvf ${project.tar}; rm ${project.tar}'&quot; dir=&quot;.&quot; /> <delete file=&quot;${tarfile}&quot;/> </target> </project> </li></ul>
  41. 41. phing <ul><li>Now chain everything together. <?xml version=&quot;1.0&quot;?> <project name=&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> </li></ul><ul><li>On the command line just run code sniffer: $ phing sniffcode </li></ul><ul><li>Or to do it all: $ phing </li></ul>
  42. 42. phing: Why use it? <ul><li>Standardize documentation (all developers generating similar documentation) </li></ul><ul><li>Standardize and automate deployment </li></ul><ul><ul><li>Run pre-deployment checks </li></ul></ul><ul><ul><li>Prevent mistakes. </li></ul></ul><ul><ul><li>Configure files with ”live” settings </li></ul></ul>
  43. 43. phing: Resources <ul><li>User Guide </li></ul><ul><li>Shell Scripts no More! </li></ul><ul><li>Phing: Building With PHP </li></ul>
  44. 44. Shell Tools: Benchmarking <ul><li>Get a quick and dirty idea of how your code might perform under a heavy load. </li></ul><ul><li>ab - Apache HTTP server benchmarking tool </li></ul><ul><li>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; </li></ul><ul><li>According to Paul M. Jones, siege is more accurate. </li></ul>
  45. 45. Shell Tools: siege <ul><li>$ seige ** 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 </li></ul>
  46. 46. Again, Why learn all of these tools? <ul><li>Automate repetitive tasks. </li></ul><ul><li>Make complex tasks simpler. </li></ul><ul><li>Ensure best practices are followed. </li></ul><ul><li>Don't re-invent the wheel. </li></ul>
  47. 47. Questions?
  48. 48. Contact & Credits <ul><li>[] </li></ul><ul><li>brian /at/ </li></ul><ul><li> </li></ul><ul><li>(917) 512-3594 </li></ul><ul><li>Credit to: Andrew Yochum Gennady Feldman </li></ul>