logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Shell-literacy.gmi (6260B)


  1. Shell literacy is one of the most important skills you ought to possess as a programmer. The Unix shell is one of the most powerful ideas ever put to code, and should be second nature to you as a programmer. No other tool is nearly as effective at commanding your computer to perform complex tasks quickly — or at storing them as scripts you can use later.
  2. In my workflow, I use Vim as my editor, and Unix as my “IDE”. I don’t trick out my vimrc to add a bunch of IDE-like features — the most substantial plugin I use on a daily basis is Ctrl+P, and that just makes it easier to open files. Being Vim literate is a valuable skill, but an important detail is knowing when to drop it. My daily workflow involves several open terminals, generally one with Vim, another to run builds or daemons, and a third which just keeps a shell handy for anything I might ask of it.
  3. => https://git.sr.ht/~sircmpwn/dotfiles/tree/master/.vimrc My .vimrc
  4. The shell I keep open allows me to perform complex tasks and answer complex questions as I work. I find interesting things with git grep, perform bulk find-and-replace with sed, answer questions with awk, and perform more intricate tasks on-demand with ad-hoc shell commands and pipelines. I have the freedom to creatively solve problems without being constrained to the rails laid by IDE designers.
  5. => https://git-scm.com/docs/git-grep git grep
  6. => gemini://drewdevault.com/cgi-bin/man.sh/1/sed sed
  7. => gemini://drewdevault.com/cgi-bin/man.sh/1/awk awk
  8. Here’s an example of a problem I encountered recently: I had a bunch of changes in a git repository. I wanted to restore deleted files without dropping the rest of my changes, but there were hundreds of these. How can I efficiently address this problem?
  9. Well, I start by getting a grasp of the scale of the issue with git status, which shows hundreds of deleted files that need to be restored. This scale is beyond the practical limit of manual intervention, so I switch to git status -s to get a more pipeline-friendly output.
  10. ```
  11. $ git status -s
  12. D main/a52dec/APKBUILD
  13. D main/a52dec/a52dec-0.7.4-build.patch
  14. D main/a52dec/automake.patch
  15. D main/a52dec/fix-globals-test-x86-pie.patch
  16. D main/aaudit/APKBUILD
  17. D main/aaudit/aaudit
  18. D main/aaudit/aaudit-common.lua
  19. D main/aaudit/aaudit-repo
  20. D main/aaudit/aaudit-server.json
  21. D main/aaudit/aaudit-server.lua
  22. ...
  23. ```
  24. I can work with this. I add grep '^ D' to filter out any entries which were not deleted, and pipe it through awk '{ print $2 }' to extract just the filenames. I’ll often run the incomplete pipeline just to check my work and catch my bearings:
  25. ```
  26. $ git status -s | grep '^ D' | awk '{ print $2 }'
  27. main/a52dec/APKBUILD
  28. main/a52dec/a52dec-0.7.4-build.patch
  29. main/a52dec/automake.patch
  30. main/a52dec/fix-globals-test-x86-pie.patch
  31. main/aaudit/APKBUILD
  32. main/aaudit/aaudit
  33. main/aaudit/aaudit-common.lua
  34. main/aaudit/aaudit-repo
  35. main/aaudit/aaudit-server.json
  36. main/aaudit/aaudit-server.lua
  37. ...
  38. ```
  39. Very good — we have produced a list of files which we need to address. Note that, in retrospect, I could have dropped the grep and just used awk to the same effect:
  40. ```
  41. $ git status -s | awk '/^ D/ { print $2 }'
  42. main/a52dec/APKBUILD
  43. main/a52dec/a52dec-0.7.4-build.patch
  44. main/a52dec/automake.patch
  45. main/a52dec/fix-globals-test-x86-pie.patch
  46. main/aaudit/APKBUILD
  47. main/aaudit/aaudit
  48. main/aaudit/aaudit-common.lua
  49. main/aaudit/aaudit-repo
  50. main/aaudit/aaudit-server.json
  51. main/aaudit/aaudit-server.lua
  52. ...
  53. ```
  54. However, we’re just writing an ad-hoc command here to solve a specific, temporary problem — finesse is not important. This command isn’t going to be subjected to a code review. Often my thinking in these situations is to solve one problem at a time: “filter the list” and “reword the list”. Anyway, the last step is to actually use this list of files to address the issue, with the help of xargs.
  55. => gemini://drewdevault.com/cgi-bin/man.sh/1/xargs xargs
  56. ```
  57. $ git status -s | awk '/^ D/ { print $2 }' | xargs git checkout --
  58. ```
  59. Let’s look at some more examples of interesting ad-hoc shell pipelines. Naturally, I wrote a shell pipeline to find some:
  60. ```
  61. $ history | cut -d' ' -f2- | awk -F'|' '{ print NF-1 " " $0 }' | sort -n | tail
  62. ```
  63. Here’s the breakdown:
  64. * history prints a list of my historical shell commands.
  65. * cut -d' ' -f2- removes the first field from each line, using space as a delimiter. history numbers every command, and this removes the number.
  66. * awk -F'|' '{ print NF-1 " " $0 } tells awk to use | as the field delimiter for each line, and print each line prefixed with the number of fields. This prints every line of my history, prefixed with the number of times the pipe operator appears in that line.
  67. * sort -n numerically sorts this list.
  68. * tail prints the last 10 items.
  69. This command, written in the moment, finds, characterizes, filters, and sorts my shell history by command complexity. Here are a couple of the cool shell commands I found:
  70. Play the 50 newest videos in a directory with mpv:
  71. ```
  72. $ ls -tc | head -n50 | tr '\n' '\0' | xargs -0 mpv
  73. ```
  74. I use this command all the time. If I want to watch a video later, I will touch the file so it appears at the top of this list. Another command transmits a tarball of a patched version of Celeste to a friend using netcat, minus the (large) game assets, with a progress display via pv:
  75. ```
  76. $ find . ! -path './Content/*' | xargs tar -cv | pv | zstd | nc 204:fbf5:... 12345
  77. ```
  78. => pv http://www.ivarch.com/programs/pv.shtml
  79. And on my friend’s end:
  80. ```
  81. $ nc -vll :: 12345 | zstdcat | pv | tar -xv
  82. ```
  83. tar, by the way, is an under-rated tool for moving multiple files through a pipeline. It can read and write tarballs to stdin and stdout!
  84. I hope that this has given you a tantalizing taste of the power of the Unix shell. If you want to learn more about the shell, I can recommend shellhaters.org as a great jumping-off point into various shell-related parts of the POSIX specification. Don’t be afraid of the spec — it’s concise, comprehensive, comprehensible, and full of examples. I would also definitely recommend taking some time to learn awk in particular.
  85. => http://shellhaters.org/ Shell haters
  86. => https://ferd.ca/awk-in-20-minutes.html A brief awk tutorial