Antediluvian Unix: A Guide to Unix Fundamentals

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

6 comments

Comments 1 - 6 of 6 previous next Post a comment

  • + rjbs Ricardo Signes 2 years ago
    Thanks, Jon! You’re still a-ok in my book.

  • + jboutelle Jonathan Boutelle 2 years ago
    This is our fault.

    Sorry people: slideshare servers had a glitch a month ago (when this was uploaded). For a few days, the ppts that were uploaded didn’t get properly archived.

    The problem is fixed now: if you reupload it should be available to all.

    We apologize for this!
    -jon boutelle (head geek, slideshare)

  • + rjbs Ricardo Signes 2 years ago
    The link is generated by Slideshare and points to their servers. Contact them.

  • + guestf7ecdb guestf7ecdb 2 years ago
    have you managed to rpair the download link?



    I0m sorry to bother you, but I need this thing urgently... ’^^

  • + rjbs Ricardo Signes 2 years ago
    Weird! Contact slideshare’s support. I have no idea why that isn’t working.

  • + gueste81481 gueste81481 2 years ago
    Great slide... can’t seem to download it though.... :(

Post a comment
Embed Video
Edit your comment Cancel

11 Favorites & 1 Group

Antediluvian Unix: A Guide to Unix Fundamentals - Presentation Transcript

  1. Antediluvian Unix a guide to unix fundamentals revision 1
  2. Why is UNIX so hard?
  3. Why is UNIX so hard? - UNIX is made of tiny little pieces, all alike.
  4. Why is UNIX so hard? - UNIX is made of tiny little pieces, all alike. - They can be put together in many ways.
  5. Why is UNIX so hard? - UNIX is made of tiny little pieces, all alike. - They can be put together in many ways. - The pieces have manuals, but UNIX doesn’t.
  6. Why is UNIX so hard? - UNIX is made of tiny little pieces, all alike. - They can be put together in many ways. - The pieces have manuals, but UNIX doesn’t. - The Mysteries of UNIX were handed down in an oral tradition.
  7. Why is UNIX so hard? - UNIX is made of tiny little pieces, all alike. - They can be put together in many ways. - The pieces have manuals, but UNIX doesn’t. - The Mysteries of UNIX were handed down in an oral tradition. - Then the GNU/Linux flood happened.
  8. The Deluge
  9. The Deluge - Users adopted Linux as their first unix, learning everything from HOWTOs.
  10. The Deluge - Users adopted Linux as their first unix, learning everything from HOWTOs. - The GNU system combined toolkit pieces.
  11. The Deluge - Users adopted Linux as their first unix, learning everything from HOWTOs. - The GNU system combined toolkit pieces. - Distributions with X11 out of the box further obfuscated the shell.
  12. We’re going to talk about antediluvian UNIX.
  13. ...but we’re going to use modern tools.
  14. ...but we’re going to use modern tools. Because being a fundamentalist does not mean being a relic.
  15. UNIX at Rest the unix filesystem
  16. The Time Before UNIX
  17. The Time Before UNIX - Every file had a structure: a list of similar records.
  18. The Age of UNIX
  19. The Age of UNIX - Forcing a record structure on every file was a hassle.
  20. The Age of UNIX - Forcing a record structure on every file was a hassle. - Plain text was easier to read and edit without special tools.
  21. The Age of UNIX - Forcing a record structure on every file was a hassle. - Plain text was easier to read and edit without special tools. - So records were stored in the natural plain text way: columns and lines.
  22. The Age of UNIX - Forcing a record structure on every file was a hassle. - Plain text was easier to read and edit without special tools. - So records were stored in the natural plain text way: columns and lines. - Every file is just a bytestream.
  23. The Flat File 127.0.0.1 localhost 66.92.232.136 cheshirecat 66.92.232.135 redking 192.168.200.1 airport
  24. The Flat File 127.0.0.1 localhost 66.92.232.136 cheshirecat 66.92.232.135 redking 192.168.200.1 airport FS = space RS = newline
  25. The Flat File root:x:0:0::/root:/bin/sh rjbs:x:101:100:RJBS:/home/rjbs:/bin/zsh
  26. The Flat File root:x:0:0::/root:/bin/sh rjbs:x:101:100:RJBS:/home/rjbs:/bin/zsh FS = colon RS = newline
  27. Special Files
  28. Special Files - Files that contain data are plain files.
  29. Special Files - Files that contain data are plain files. - Other (special) files perform system magic.
  30. Special Files - Files that contain data are plain files. - Other (special) files perform system magic. - directories
  31. Special Files - Files that contain data are plain files. - Other (special) files perform system magic. - directories - symbolic links
  32. Special Files - Files that contain data are plain files. - Other (special) files perform system magic. - directories - symbolic links - named pipes
  33. Special Files - Files that contain data are plain files. - Other (special) files perform system magic. - directories - symbolic links - named pipes - sockets
  34. Special Files - Files that contain data are plain files. - Other (special) files perform system magic. - directories - symbolic links - named pipes - sockets - devices
  35. Permissions
  36. Permissions - Every file has associated permissions.
  37. Permissions - Every file has associated permissions. - Permissions govern reading, writing, and execution.
  38. Permissions - Every file has associated permissions. - Permissions govern reading, writing, and execution. - And then there are the special permissions: s?id and sticky.
  39. Permissions - Every file has associated permissions. - Permissions govern reading, writing, and execution. - And then there are the special permissions: s?id and sticky. ----------
  40. Magic Numbers
  41. Magic Numbers - UNIX supports many executable file types.
  42. Magic Numbers - UNIX supports many executable file types. - Roughly 65,535, at the minimum.
  43. Magic Numbers - UNIX supports many executable file types. - Roughly 65,535, at the minimum. - Magic numbers identify the file type.
  44. Magic Numbers - UNIX supports many executable file types. - Roughly 65,535, at the minimum. - Magic numbers identify the file type. - They’re the first two bytes of the file.
  45. The Magic Magic Number
  46. The Magic Magic Number - One magic number, 0x2321, is common in modern unices.
  47. The Magic Magic Number - One magic number, 0x2321, is common in modern unices. - In ASCII, it’s #!
  48. The Magic Magic Number - One magic number, 0x2321, is common in modern unices. - In ASCII, it’s #! - In English, it’s “shebang!”
  49. The Magic Magic Number - One magic number, 0x2321, is common in modern unices. - In ASCII, it’s #! - In English, it’s “shebang!” - The kernel will run the program named after #! with the given args, plus the filename.
  50. #! in Action
  51. #! in Action ~/bin/hello #!/bin/sh echo Good morning, $USER!
  52. #! in Action ~/bin/hello #!/bin/sh echo Good morning, $USER! knave!rjbs% hello
  53. #! in Action ~/bin/hello #!/bin/sh echo Good morning, $USER! knave!rjbs% hello execve( ”/bin/sh”, “/home/rjbs/hello” );
  54. #! in Action
  55. #! in Action ~/bin/hi_all #!/usr/bin/awk -f BEGIN { FS=”:”; } {} ($7 != “”) && ($5 !=””) { {print “Hello, “ $1 “!”;} END { print “Buh-bye!” }
  56. #! in Action ~/bin/hi_all #!/usr/bin/awk -f BEGIN { FS=”:”; } {} ($7 != “”) && ($5 !=””) { {print “Hello, “ $1 “!”;} END { print “Buh-bye!” }
  57. #! in Action ~/bin/hi_all #!/usr/bin/awk -f BEGIN { FS=”:”; } {} ($7 != “”) && ($5 !=””) { {print “Hello, “ $1 “!”;} END { print “Buh-bye!” }
  58. #! in Action ~/bin/hi_all #!/usr/bin/awk -f BEGIN { FS=”:”; } {} ($7 != “”) && ($5 !=””) { {print “Hello, “ $1 “!”;} END { print “Buh-bye!” } knave!rjbs% hi_all /etc/passwd
  59. Hello, sync! Hello, shutdown! Hello, halt! Hello, operator! Hello, gdm! Hello, samael! Hello, rjbs! Hello, calliope! Hello, solios! Hello, photon! Buh-bye!
  60. UNIX in Motion unix program execution
  61. The Process Tree
  62. The Process Tree - Every running process is the child of the process that ran it (except init).
  63. The Process Tree - Every running process is the child of the process that ran it (except init). - A child can’t live without its parent.
  64. The Process Tree - Every running process is the child of the process that ran it (except init). - A child can’t live without its parent. - A child starts life with a copy of its parent’s environment.
  65. Environment
  66. Environment - An environment is a set of data communicated from parent to child.
  67. Environment - An environment is a set of data communicated from parent to child. - Parents teach children, but children can never teach parents.
  68. Environment - An environment is a set of data communicated from parent to child. - Parents teach children, but children can never teach parents. - This is important.
  69. Environment - An environment is a set of data communicated from parent to child. - Parents teach children, but children can never teach parents. - This is important. - Really important.
  70. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rjbs LESS=-M
  71. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rjbs LESS=-M knave!rjbs% IRCNICK=rsignes irssi
  72. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org LESS=-M knave!rjbs% IRCNICK=rsignes irssi
  73. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rsignes LESS=-M knave!rjbs% IRCNICK=rsignes irssi
  74. init -+ |-zsh -irssi MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rsignes LESS=-M knave!rjbs% IRCNICK=rsignes irssi
  75. init -+ |-zsh -irssi MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rsignes LESS=-M
  76. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rsignes LESS=-M
  77. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org LESS=-M
  78. init -+ |-zsh MAIL=/var/spoołmaiłrjbs MAILCHECK=60 IRCSERVER=us.slashnet.org IRCNICK=rjbs LESS=-M
  79. Signals
  80. Signals - Processes mind their own business and try to run to completion.
  81. Signals - Processes mind their own business and try to run to completion. - But they can be sent signals to make them do unusual things.
  82. Signals - Processes mind their own business and try to run to completion. - But they can be sent signals to make them do unusual things. - Mostly, “stop running.”
  83. Some Common Signals
  84. Some Common Signals SIGHUP
  85. Some Common Signals SIGINT SIGHUP
  86. Some Common Signals SIGQUIT SIGINT SIGHUP
  87. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP
  88. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP
  89. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT
  90. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS
  91. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS SIGKILL
  92. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS SIGKILL SIGSEGV
  93. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS SIGKILL SIGSEGV SIGTERM
  94. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS SIGKILL SIGSEGV SIGTERM SIGSTOP
  95. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS SIGKILL SIGSEGV SIGTERM SIGSTOP SIGCONT
  96. Some Common Signals SIGQUIT SIGINT SIGILL SIGHUP SIGTRAP SIGABRT SIGBUS SIGKILL SIGSEGV SIGTERM SIGSTOP SIGCONT SIGPWR
  97. The Time Before UNIX
  98. The Time Before UNIX - Your computer ran one program.
  99. The Time Before UNIX - Your computer ran one program. - When that program was done, it ran the next.
  100. The Time Before UNIX - Your computer ran one program. - When that program was done, it ran the next. - Lather, rinse, repeat.
  101. The Time Before UNIX - Your computer ran one program. - When that program was done, it ran the next. - Lather, rinse, repeat. - Batch processing.
  102. The Age of UNIX
  103. The Age of UNIX - Many programs run at once.
  104. The Age of UNIX - Many programs run at once. - Interactive programs became possible.
  105. The Age of UNIX - Many programs run at once. - Interactive programs became possible. - ...but batch processing is not dead.
  106. Job Control
  107. Job Control - A system for handling batch processing in UNIX.
  108. Job Control - A system for handling batch processing in UNIX. - Jobs can be
  109. Job Control - A system for handling batch processing in UNIX. - Jobs can be - paused
  110. Job Control - A system for handling batch processing in UNIX. - Jobs can be - paused - run in the background
  111. Job Control - A system for handling batch processing in UNIX. - Jobs can be - paused - run in the background - brought to the foreground
  112. Job Control knave!rjbs%
  113. Job Control knave!rjbs% jobs [1]+ Running make bzImage& [2] Running backup& [3]- Suspended wget -rnp http... [4] Running pdflatex thesis.tex knave!rjbs%
  114. Job Control knave!rjbs% jobs [1]+ Running make bzImage& [2] Running backup& [3]- Suspended wget -rnp http... [4] Running pdflatex thesis.tex knave!rjbs% mutt
  115. Job Control knave!rjbs% jobs [1]+ Running make bzImage& [2] Running backup& [3]- Suspended wget -rnp http... [4] Running pdflatex thesis.tex knave!rjbs% mutt [2] Done backup& [4] Done pdflatex thesis.tex
  116. Job Control in Action ~/bin/waiter #!/bin/sh for i in 1 2 3 4 5 6 7 8 9 10 do sleep $i done How long does this take to run?
  117. Job Control in Action ~/bin/waiter #!/bin/sh for i in 1 2 3 4 5 6 7 8 9 10 do (sleep $i)& done wait How long does this take to run?
  118. Job Control
  119. Job Control - Ctrl-Z
  120. Job Control - Ctrl-Z - bg and fg
  121. Job Control - Ctrl-Z - bg and fg -&
  122. Job Control - Ctrl-Z - bg and fg -& - jobs
  123. Job Control - Ctrl-Z - bg and fg -& - jobs - wait
  124. Bytestreams
  125. Bytestreams - Files, you remember, are just bytestreams.
  126. Bytestreams - Files, you remember, are just bytestreams. - A bytestream is list of bytes, in order.
  127. Bytestreams - Files, you remember, are just bytestreams. - A bytestream is list of bytes, in order. - But it’s a stream: it flows
  128. Bytestreams - Files, you remember, are just bytestreams. - A bytestream is list of bytes, in order. - But it’s a stream: it flows - in and out of programs
  129. Bytestreams - Files, you remember, are just bytestreams. - A bytestream is list of bytes, in order. - But it’s a stream: it flows - in and out of programs - through pipes
  130. Bytestreams
  131. Bytestreams - Programs read and write from bytestreams all the time.
  132. Bytestreams - Programs read and write from bytestreams all the time. - But a UNIX program’s favorite bytestreams are the standard IO streams.
  133. Bytestreams - Programs read and write from bytestreams all the time. - But a UNIX program’s favorite bytestreams are the standard IO streams. - stdin - standard input
  134. Bytestreams - Programs read and write from bytestreams all the time. - But a UNIX program’s favorite bytestreams are the standard IO streams. - stdin - standard input - stdout - standard output (1)
  135. Bytestreams - Programs read and write from bytestreams all the time. - But a UNIX program’s favorite bytestreams are the standard IO streams. - stdin - standard input - stdout - standard output (1) - stderr - standard err (2)
  136. Standard I/O
  137. Standard I/O - The standard I/O streams are usually connected to the terminal.
  138. Standard I/O - The standard I/O streams are usually connected to the terminal. - But you can redirect them.
  139. Standard I/O - The standard I/O streams are usually connected to the terminal. - But you can redirect them. - Understanding I/O redirection is fundamental to using UNIX effectively.
  140. Redirection in Action ~/bin/savalias #!/bin/sh alias > .bash_tmp_1 cat .bash_alias .bash_tmp1 \\ > .bash_tmp_2 sort < .bash_tmp_2 > .bash_tmp_1 uniq < .bash_tmp_1> .bash_tmp_2 mv .bash_tmp_2 .bash_alias rm .bash_tmp_1
  141. Redirection in Action ~/bin/savalias #!/bin/sh alias \\ | cat - .bash_alias \\ | sort | uniq > .bash_alias
  142. Redirection in Action ~/bin/connected #!/bin/sh netstat -n \\ | grep ESTABLISHED \\ | cut -c 45-65 \\ | awk ‘BEGIN{FS=”:”}{print $1}’ \\ | sort -n \\ | uniq
  143. Redirection
  144. Redirection -> - >> -< -| - `` or $()
  145. Arguments
  146. Arguments - Programs are passed a list of arguments, given on the command line.
  147. Arguments - Programs are passed a list of arguments, given on the command line. - These are usually switches that change how the program will work.
  148. Arguments - Programs are passed a list of arguments, given on the command line. - These are usually switches that change how the program will work. - In lieu of stdio, arguments may name files on which to operate.
  149. Return Values
  150. Return Values - When a job finishes, its status is reported by a single integer.
  151. Return Values - When a job finishes, its status is reported by a single integer. - This “return value” indicates success, failure, or other information.
  152. environment stderr return stdin Process value signals arguments stdout other processes
  153. The UNIX Toolkit elementary programs
  154. The Toolkit
  155. The Toolkit - Little utilities.
  156. The Toolkit - Little utilities. - Found almost everywhere.
  157. The Toolkit - Little utilities. - Found almost everywhere. - Work the same almost everywhere.
  158. Some Toolkit Programs
  159. Some Toolkit Programs cd
  160. Some Toolkit Programs ls cd
  161. Some Toolkit Programs cp ls cd
  162. Some Toolkit Programs cp ls mv cd
  163. Some Toolkit Programs cp ls mv cd rm
  164. Some Toolkit Programs cp ls mv cd mkdir rm
  165. Some Toolkit Programs cp ls mv cd mkdir chmod rm
  166. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm
  167. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm sort
  168. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm uniq sort
  169. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq sort
  170. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort
  171. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort basename
  172. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut basename
  173. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr basename
  174. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr grep basename
  175. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr grep basename seq
  176. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr grep basename yes seq
  177. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr grep basename true yes seq
  178. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr grep basename true yes false seq
  179. Some Toolkit Programs cp ls mv cd mkdir chmod chown rm cat uniq test sort cut tr grep basename true yes false seq
  180. The Toolkit in Action /sbin/armageddon #!/bin/sh cat /etc/passwd
  181. The Toolkit in Action /sbin/armageddon #!/bin/sh cat /etc/passwd \\ | grep :100:
  182. The Toolkit in Action /sbin/armageddon #!/bin/sh cat /etc/passwd \\ | grep :100: \\ | cut -d: -f6
  183. The Toolkit in Action /sbin/armageddon #!/bin/sh rm -R $(cat /etc/passwd \\ | grep :100: \\ | cut -d: -f6)
  184. The Toolkit in Action /sbin/armageddon #!/bin/sh yes | rm -R $(cat /etc/passwd \\ | grep :100: \\ | cut -d: -f6) \\ echo Users obliterated!
  185. The Shell
  186. The Shell - Just another piece of the toolkit.
  187. The Shell - Just another piece of the toolkit. - But the first among equals.
  188. The Shell - Just another piece of the toolkit. - But the first among equals. - Provides flow control for processes.
  189. The Shell - Just another piece of the toolkit. - But the first among equals. - Provides flow control for processes. - Also, built-in utilities.
  190. The Shell - Just another piece of the toolkit. - But the first among equals. - Provides flow control for processes. - Also, built-in utilities. - ...but most of these are extraneous.
  191. Flow Control
  192. Flow Control - Controls:
  193. Flow Control - Controls: - whether a process will run
  194. Flow Control - Controls: - whether a process will run - when a process will run
  195. Flow Control - Controls: - whether a process will run - when a process will run - process iteration over a list of data
  196. Flow Control #!/bin/sh if [ $UID = 0 ]; then echo You’re root! else echo You’re a luser! fi
  197. Flow Control #!/bin/sh if [ $UID = 0 ]; then echo You’re root! else echo You’re a luser! fi So, how does sh compare $UID and zero?
  198. environment stderr return stdin Process value signals arguments stdout other processes
  199. if #!/bin/sh if test $UID = 0; then echo You’re root! else echo You’re a luser! fi
  200. if #!/bin/sh if cp $@ $HOME; then echo Everything’s fine! else echo WE’RE ALL GONNA DIE! fi
  201. Flow Control #!/bin/sh if [ $UID = 0 ]; then echo You’re root! else echo You’re a luser! fi
  202. for ~/bin/makeall #!/bin/sh for dir in ~/code/*; do cd $dir; make done
  203. for and if ~/bin/makeall #!/bin/sh for dir in ~/code/*; do \\ if [ -d $dir -a -r $dir/Makefile ]; then cd $dir; make fi done
  204. for, if, and job control ~/bin/makeall #!/bin/sh for dir in ~/code/*; do \\ if [ -d $dir -a -r $dir/Makefile ]; then (cd $dir; make)& fi done wait
  205. while ~/bin/unique #!/bin/sh echo $$ > ~/unique_pid while [ $$ = $(cat ~/unique_pid) ] do sleep 1 done echo Replaced by $(cat ~/unique_pid)
  206. until ~/bin/unique #!/bin/sh echo $$ > ~/unique_pid until [ $$ != $(cat ~/unique_pid) ] do sleep 1 done echo Replaced by $(cat ~/unique_pid)
  207. while and if ~/bin/rmcont #!/bin/sh if [ -r ~/unique_pid ]; then kill $(cat ~/unique_pid) rm ~/unique_pid fi echo $$ > ~/unique_pid while true do rm -R $1/* > /dev/null done
  208. select ~/bin/chat #!/bin/sh select user in $(who|cut -f1 -d” “|uniq) do if [ $user ]; then talk $user; fi break done
  209. select ~/bin/chat #!/bin/sh select user in $(who|cut -f1 -d” “|uniq) do if [ $user ]; then talk $user; fi break done 1) rjbs 2) calliope #?
  210. case ~/bin/unarc #!/bin/sh case “$1” in *.tgz|*.tar.gz) tar zxvf $1;; *.tar.bz2) tar jxvf $1;; *.gz) gunzip $1;; *.tar) tar xvf $1;; *.zip) unzip $1;; *.shar.gz) cat $1 | gunzip | unshar > \\ $(basename $1 .shar.gz) ;; *) echo Unknown archive type! esac
  211. Redefining Your UNIX customizing the interactive experience
  212. Why script?
  213. Why script? - Any shell command can be entered directly at the command line.
  214. Why script? - Any shell command can be entered directly at the command line. - So why script?
  215. Why script? - Any shell command can be entered directly at the command line. - So why script? - Scripting lets you remember useful recipes.
  216. Why script? - Any shell command can be entered directly at the command line. - So why script? - Scripting lets you remember useful recipes. - And abstract them.
  217. My Favorite Scripts
  218. My Favorite Scripts - ...are incredibly simple.
  219. My Favorite Scripts - ...are incredibly simple. - They just eliminate some keystrokes.
  220. My Favorite Scripts - ...are incredibly simple. - They just eliminate some keystrokes. ~/bin/sx #!/bin/sh screen -x $@
  221. My Favorite Scripts - ...are incredibly simple. - They just eliminate some keystrokes. ~/bin/sx #!/bin/sh screen -x $@ ~/bin/n2pm #!/bin/sh module-starter --author=”Ricardo Signes \\ --email=”rjbs@cpan.org” $@
  222. Functions
  223. Functions - These don’t need to be scripts.
  224. Functions - These don’t need to be scripts. - You can do this with functions.
  225. Functions - These don’t need to be scripts. - You can do this with functions. knave!rjbs% sx() { screen -x $@ }
  226. Functions - These don’t need to be scripts. - You can do this with functions. knave!rjbs% sx() { screen -x $@ } knave!rjbs% cs() { cp $2 $2.bak; cp $1 $2 }
  227. Functions - These don’t need to be scripts. - You can do this with functions. knave!rjbs% sx() { screen -x $@ } knave!rjbs% cs() { cp $2 $2.bak; cp $1 $2 } knave!rjbs% ch() { awk ‘FNR==1{for(i=1;i<=NF;i++)print $i}{next}’ $@ }
  228. Aliases
  229. Aliases - Aliases can do much of what functions can.
  230. Aliases - Aliases can do much of what functions can. - Quoting makes them hard to write.
  231. Aliases - Aliases can do much of what functions can. - Quoting makes them hard to write. - Single-lining makes them harder.
  232. Aliases - Aliases can do much of what functions can. - Quoting makes them hard to write. - Single-lining makes them harder. - Those can be worked around, but why bother?
  233. Aliases
  234. Aliases - There are three reasons to bother:
  235. Aliases - There are three reasons to bother: - better recursion protection
  236. Aliases - There are three reasons to bother: - better recursion protection - non-command aliases (the space hack)
  237. Aliases - There are three reasons to bother: - better recursion protection - non-command aliases (the space hack) - higher precedence
  238. Aliases - There are three reasons to bother: - better recursion protection - non-command aliases (the space hack) - higher precedence - I still don’t bother.
  239. Functions
  240. Functions - More reasons to use functions:
  241. Functions - More reasons to use functions: - they’re already in memory
  242. Functions - More reasons to use functions: - they’re already in memory - they run in the current process
  243. Functions - More reasons to use functions: - they’re already in memory - they run in the current process - they produce scope
  244. Scope
  245. Scope - Scope is the place where something is visible.
  246. Scope - Scope is the place where something is visible. - Using scope wisely makes life easy.
  247. Scope - Scope is the place where something is visible. - Using scope wisely makes life easy. - The file hierarchy is a kind of scope.
  248. Scope - Scope is the place where something is visible. - Using scope wisely makes life easy. - The file hierarchy is a kind of scope. - So is the process environment.
  249. Scope Functions can alter the running shell. (It’s in scope.) cvsroot() { if [ “$1” != “” ]; then if [ -f $CVSROOT_DIR/$1 ]; then CVSROOT=$(cat $CVSROOT_DIR/$1) else echo cvsroot: $CVSROOT_DIR/$1 is invalid fi else echo cvsroot: currently $CVSROOT fi }
  250. Scope Function variables can be made local (scope-limited). mown() { local filename = $1 local owner = $(ls -l $1|cut -c21-29) mail $owner }
  251. Scope Nested functions are scoped to the enclosing function. #!/bin/sh hypotenuses() { pythagoras() { echo $(dc -e “$1 2 ^ $2 2 ^ v f”) } for $side in $*; do pythagoras $side; done } The pythagoras() function is not visible outside of hypotenuses().
  252. Scope Functions are scoped to their enclosing script. #!/bin/sh hypotenuse() { echo $(dc -e “$1 2 ^ $2 2 ^ v f”) } for side do hypotenuse $(dc -e “$side 2 / f”) The special-purpose hypotenuse() function will go away when the script is done running.
  253. Scope This is how I load my functions. for func in ~/.bash/functions/*; do . $i done (it’s in my profile)
  254. Scripts
  255. Scripts - When are scripts useful?
  256. Scripts - When are scripts useful? - programs not run under a login
  257. Scripts - When are scripts useful? - programs not run under a login - programs too large to leave in memory
  258. Scripts - When are scripts useful? - programs not run under a login - programs too large to leave in memory - rarely-used programs
  259. Scripts - When are scripts useful? - programs not run under a login - programs too large to leave in memory - rarely-used programs - In almost no case do I write sh scripts.
  260. Scripts
  261. Scripts - ...but I write plenty of other scripts.
  262. Scripts - ...but I write plenty of other scripts. - Shell scripts lose utility because I’m always in the shell.
  263. Scripts - ...but I write plenty of other scripts. - Shell scripts lose utility because I’m always in the shell. - But I’m not limited to shell scripts! I have the shebang!
  264. #!/usr/bin/whatever
  265. #!/usr/bin/whatever - Almost any interpreted data file can be run.
  266. #!/usr/bin/whatever - Almost any interpreted data file can be run. - It needs a shebang and +x permissions.
  267. #!/usr/bin/whatever - Almost any interpreted data file can be run. - It needs a shebang and +x permissions. - The key is knowing the right tool.
  268. #! in Action ~/bin/hi_all #!/usr/bin/awk -f BEGIN { FS=”:”; } {} ($7 != “”) && ($5 !=””) { {print “Hello, “ $1 “!”;} END { print “Buh-bye!” }
  269. #!/usr/bin/whatever ~/bin/hi_all #!/usr/bin/awk -f BEGIN { FS=”:” } ($7 != “”) && ($5 !=””) {print “Hello, “ $1 “!”;} {next} END { print “Buh-bye!” }
  270. #!/usr/bin/whatever ~/bin/backup #!/usr/bin/make -f RSYNC_SW=”--delete -ave ssh” default: web gnupg logs mirrors: ifarchive minicpan all: default mirrors web: rsync ${RSYNC_SW} ${HOME}/public_htmł cheshire:~/public_htmł gnupg: rsync ${RSYNC_SW} cheshire:~/.gnupg/ ${HOME}/backup/gnupg/ logs: rsync ${RSYNC_SW} cheshire:/var/www/ ${HOME}/backup/www/ rsync ${RSYNC_SW} cheshire:~/cvs/ ${HOME}/backup/cvs/ rsync ${RSYNC_SW} cheshire:~/log/ ${HOME}/backup/log/ ifarchive: rsync ${RSYNC_SW} cheshire:~/ifarchive/ ${HOME}/mirrors/ifarchive/
  271. #!/usr/bin/whatever ~/bin/bestsongs #!/usr/bin/mpg123 -z -@ /usr/bin/music/outkast/ms.jackson.mp3 /usr/bin/music/devøshout.mp3 /usr/bin/music/talking_heads/television_man.mp3 ...
  272. #!/usr/bin/whatever ~/bin/bestsongs #!/usr/bin/mpg123 -z -@ /usr/bin/music/outkast/ms.jackson.mp3 /usr/bin/music/devøshout.mp3 /usr/bin/music/talking_heads/television_man.mp3 ... addsong() { if [ “$1” != “” -a -r “$1” ]; then echo $1 >> ~/bin/bestsongs; else echo can’t add file }
  273. #!/usr/bin/whatever #!/usr/bin/perl #!/usr/bin/python #!/usr/bin/ruby #!/usr/bin/clisp
  274. More Slides things that deserve more time
  275. Globbing
  276. Globbing - * and ?
  277. Globbing - * and ? - [ABC]
  278. Globbing - * and ? - [ABC] - {xyz}
  279. Globbing - * and ? - [ABC] - {xyz} - zsh globbing
  280. Environment Variables
  281. Environment Variables - shell vars v. environment vars
  282. Environment Variables - shell vars v. environment vars - $x and ${x}
  283. Environment Variables - shell vars v. environment vars - $x and ${x} - ${x:-default}
  284. Environment Variables - shell vars v. environment vars - $x and ${x} - ${x:-default} - ${x:=default}
  285. Environment Variables - shell vars v. environment vars - $x and ${x} - ${x:-default} - ${x:=default} - ${x:?epitaph}
  286. Environment Variables - shell vars v. environment vars - $x and ${x} - ${x:-default} - ${x:=default} - ${x:?epitaph} - ${x:+iftrue}
  287. Environment Variables - shell vars v. environment vars - $x and ${x} - ${x:-default} - ${x:=default} - ${x:?epitaph} - ${x:+iftrue} - ${x:offset:length}
  288. STDIO
  289. STDIO - `` and $()
  290. STDIO - `` and $() - >&
  291. STDIO - `` and $() - >& - << and <<-
  292. STDIO - `` and $() - >& - << and <<- - command blocks { }
  293. STDIO - `` and $() - >& - << and <<- - command blocks { } - the read builtin
  294. Magic Nonsense
  295. Magic Nonsense - (job&) # job into void
  296. Magic Nonsense - (job&) # job into void - $((2+2))
  297. Magic Nonsense - (job&) # job into void - $((2+2)) - “” versus ‘’
  298. Magic Nonsense - (job&) # job into void - $((2+2)) - “” versus ‘’ - ugh! ‘abc’\\’’def’
  299. Magic Nonsense - (job&) # job into void - $((2+2)) - “” versus ‘’ - ugh! ‘abc’\\’’def’ - eval
  300. Magic Nonsense - (job&) # job into void - $((2+2)) - “” versus ‘’ - ugh! ‘abc’\\’’def’ - eval - source
  301. Precedence Hackery - command - builtin - enable

+ Ricardo SignesRicardo Signes, 3 years ago

custom

10847 views, 11 favs, 4 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 10847
    • 10839 on SlideShare
    • 8 from embeds
  • Comments 6
  • Favorites 11
  • Downloads 834
Most viewed embeds
  • 3 views on http://document-server.cono
  • 3 views on http://192.168.10.100
  • 1 views on http://localhost
  • 1 views on http://orange.wiki

more

All embeds
  • 3 views on http://document-server.cono
  • 3 views on http://192.168.10.100
  • 1 views on http://localhost
  • 1 views on http://orange.wiki

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories

Groups / Events