workshops/slides/shells/shells.md

145 lines
3.6 KiB
Markdown
Raw Permalink Normal View History

2023-06-15 22:28:08 +00:00
# Origin
- Used as a 'shell' around a bare-ass kernel.
- People once considered a basic shell user-friendly.
2023-06-16 00:25:05 +00:00
- Once worked as slow printing.
2023-06-15 22:28:08 +00:00
## Use Cases
2023-06-16 00:25:05 +00:00
- It's crazy-fast to write.
2023-06-15 22:28:08 +00:00
- Try deleting a file in Python.
2023-06-16 00:25:05 +00:00
- It's awful for any structured data.
2023-06-15 22:28:08 +00:00
# Bangs!
Don't say `#!/bin/bash` if you don't mean it!
2023-06-16 00:25:05 +00:00
| Bang! | Apraisal |
|:------------------------|:--------------|
| `#!/bin/bash` | reasonable |
| `#!/bin/sh` | sensible |
| `#!/bin/dash` | bad idea |
| `#!/bin/zsh` | risky |
| `#!/bin/ksh` | foolish |
| `#!/usr/bin/env bash` | pathetic |
| `#!/bin/fish` | madness |
| `#!/bin/elvish` | genius |
2023-06-15 22:28:08 +00:00
Protip: for a faster machine, make `sh` a link to `/bin/dash`.
`ln -s /bin/dash /bin/dash`
# Deviant Shells
## `ksh` (korn shell)
2023-06-16 00:25:05 +00:00
- made before `bash`
- `vi` mode by default (means you're a hacker)
- Can be posix compliant with effort
2023-06-15 22:28:08 +00:00
## `elvish`
- pure style
- built-in browser
- Ctrl+i to search for commands in $PATH
- Ctrl+n to search for command arguments
- Ctrl+l to return to previous locations
2023-06-16 00:25:05 +00:00
## zsh
- `for x in 1..10`
2023-06-19 16:38:44 +00:00
- great autocompletion
2023-06-16 00:25:05 +00:00
# Bash Advantages
2023-06-15 22:28:08 +00:00
- Equality check with "==".
* Dash cannot do `if [ $x == 3 ]; then echo y; fi`
- `$RANDOM`
- Dash cannot use `&>/dev/null`
- No history
2023-06-16 00:25:05 +00:00
- `$PROMPT_COMMAND`
## Lists
```
myList+=(*)
echo "${myList[0]}"
```
# Functions
```
function wotsa(){
def="$(curl -s dict://dict.org/define:$1: | \
grep -vP '^\d\d\d ')"
if [ "$def" = "" ]; then
echo no definition
else
echo "$def" | mdcat -p
fi
}
```
2023-06-15 22:28:08 +00:00
# Protips
```
"\C- ": shell-expand-line
"\C-x": glob-expand-word
```
2023-06-16 00:25:05 +00:00
- `set -e`
- `set -x`
2023-06-15 22:28:08 +00:00
- `set -o vi`
- Ctrl+d
2023-06-16 00:25:05 +00:00
- `type $whatever`
2023-06-15 22:28:08 +00:00
- `.inputrc`
2023-06-16 00:25:05 +00:00
- `PS1=foo`
2023-06-19 16:38:44 +00:00
- `^$` for first argument used
- `!$` for last argument used
2023-06-15 22:28:08 +00:00
# Fun with Bash
- wifi_qr.sh
- clean.sh
- theme.sh
2023-06-16 00:25:05 +00:00
- notflix.sh
- clip.sh
- vidget.sh
2023-06-15 22:28:08 +00:00
# Zen Master Foo
Master Foo once said to a visiting programmer: "There is more Unix-nature in one line of shell script than there is in ten thousand lines of C."
The programmer, who was very proud of his mastery of C, said: "How can this be?
C is the language in which the very kernel of Unix is implemented!"
Master Foo replied: "That is so.
Nevertheless, there is more Unix-nature in one line of shell script than there is in ten thousand lines of C."
#
The programmer grew distressed. "But through the C language we experience the enlightenment of the Patriarch Ritchie! We become as one with the operating system and the machine, reaping matchless performance!"
Master Foo replied: "All that you say is true.
But there is still more Unix-nature in one line of shell script than there is in ten thousand lines of C."
The programmer scoffed at Master Foo and rose to depart.
But Master Foo nodded to his student Nubi, who wrote a line of shell script on a nearby whiteboard, and said: "Master programmer, consider this pipeline.
Implemented in pure C, would it not span ten thousand lines?"
#
The programmer muttered through his beard, contemplating what Nubi had written.
Finally he agreed that it was so.
"And how many hours would you require to implement and debug that C program?" asked Nubi.
"Many," admitted the visiting programmer. "But only a fool would spend the time to do that when so many more worthy tasks await him."
"And who better understands the Unix-nature?" Master Foo asked. "Is it he who writes the ten thousand lines, or he who,
perceiving the emptiness of the task, gains merit by not coding?"
Upon hearing this, the programmer was enlightened.