Skip to content

Commit dc1b21f

Browse files
Merge pull request #32 from LearnYouSomeComputer/ch4
Bash: Os's edits
2 parents 95ca56f + d609e47 commit dc1b21f

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

04-Bash-Scripting.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ To determine whether a command succeeded or failed, you can check the `$?` varia
7272
Traditionally, a value of `0` indicates success, and a non-zero value indicates failure.
7373
Some programs may use different return values to indicate different types of failures; consult the man page for a program to see how it behaves.
7474

75-
For example, if you run `g++` on a file that doesn't exist, g++ returns `1`:
75+
For example, if you run `g++` on a file that doesn't exist, `g++` returns `1`:
7676
```
7777
$ g++ no-such-file.cpp
7878
g++: error: no-such-file.cpp: No such file or directory
@@ -84,7 +84,7 @@ $ echo $?
8484

8585
Bash also provides variables holding the command-line arguments passed to the script.
8686
A command-line argument is something that you type after the command; for instance, in the command `ls /tmp`, `/tmp` is the first argument passed to `ls`.
87-
The name of the command that started the script is stored in `$0`. This is almost always just the name of the script.[^symlinks]
87+
The name of the command that started the script is stored in `$0`. This is (almost) always just the name of the script.[^symlinks]
8888
The variables `$1` through `$9` contain the first through ninth command line arguments, respectively.
8989
To get the 10th argument, you have to write `${10}`, and likewise for higher argument numbers.
9090

@@ -135,14 +135,12 @@ Here's an example of how to write `if` statements in Bash:
135135

136136
# Emit the appropriate greeting for various people
137137

138-
if [[ $1 = "Jeff" ]]; then
138+
if [[ "$1" = "Jeff" ]]; then
139139
echo "Hi, Jeff"
140-
elif [[ $1 == "Maggie" ]]; then
141-
echo "Hello, Maggie"
142-
elif [[ $1 == *.txt ]]; then
143-
echo "You’re a text file, $1"
144-
elif [ "$1" = "Stallman" ]; then
140+
elif [[ "$1" == "Stallman" ]]; then
145141
echo "FREEDOM!"
142+
elif [[ "$1" == *.txt ]]; then
143+
echo "You’re a text file, $1"
146144
else
147145
echo "Who in blazes are you?"
148146
fi
@@ -163,8 +161,7 @@ Comparing Strings:
163161
- `!=` means either:
164162
- String inequality, if both operands are strings, or
165163
- Glob fails to match, if the RHS is a glob.
166-
- `<`: The LHS sorts before the RHS.
167-
- `>`: The LHS sorts after the RHS.
164+
- `<` / `>`: The LHS lexicographically[^lexicographically] compares as less than / greater than the RHS.
168165
- `-n`: The string is not empty (e.g., `[[ -n "$var" ]]`).
169166
- `-z`: The string is empty (length is zero).
170167

@@ -204,7 +201,7 @@ if [[ -f "$1" ]]; then
204201
elif [[ -d "$1" ]]; then
205202
ls "$1"
206203
else
207-
echo "I don't know what to do with $1!";
204+
echo "I don't know what to do with $1!"
208205
fi
209206
```
210207

@@ -365,7 +362,7 @@ If you need a counting for loop (C-style loop), you can get one of those with `(
365362
#!/bin/bash
366363

367364
for (( i=1; i < 9; i++ )); do
368-
echo $i;
365+
echo $i
369366
done
370367
```
371368

@@ -467,6 +464,8 @@ You can scope variables to functions with the `local` builtin; run `help local`
467464

468465
### Tips
469466

467+
Bash contains a help system for its built-in commands: `help pushd` tells you information about the `pushd` command.
468+
470469
To write a literal `\` , `` ` ``, `$`, `"`, `'`, or `#`, escape it with `\`; for instance, `"\$"` gives a literal `$`.
471470

472471
When writing scripts, sometimes you will want to change directories --- for instance, maybe you want to write some temporary files in `/tmp`.
@@ -507,8 +506,6 @@ $ dirs
507506
Putting `set -u` at the top of your script will give you an error if you try to use a variable without setting it first.
508507
This is particularly handy if you make a typo; for example, `rm -r $delete_mee/*` will call `rm -r /*` if you haven't set `$delete_mee`!
509508

510-
Bash contains a help system for its built-in commands: `help pushd` tells you information about the `pushd` command.
511-
512509
### Customizing Bash
513510

514511
Let's say you've typed out our example script for compiling and running C++ code and you've put it in a directory called `scripts` in your home directory.
@@ -574,13 +571,13 @@ See `help variables` for a list of knobs and dials you can fiddle with.
574571
## Questions
575572
Name: `______________________________`
576573

577-
1. What does the `let` builtin do?
574+
1. In your own words, what is the difference between `g++ $file` and `g++ "$file"`?
578575
\vspace{10em}
579576

580-
2. Write a script that prints "fizz" if the first argument is divisible by 3, "buzz" if it is divisible by 5, and "fizzbuzz" if it is divisible by both 3 and 5.[^interview]
581-
\vspace{20em}
577+
2. Write a script that prints "directory" if the first argument is a directory and "file" if the first argument is a file.
578+
\vspace{15em}
582579

583-
3. Write a script that prints "directory" if the first argument is a directory and "file" if the first argument is a file.
580+
3. Write a script that prints "fizz" if the first argument is divisible by 3, "buzz" if it is divisible by 5, and "fizzbuzz" if it is divisible by both 3 and 5.[^interview]
584581
\newpage
585582

586583
## Quick Reference
@@ -597,8 +594,7 @@ Comparing Strings:
597594
- `!=` means either:
598595
- String inequality, if both operands are strings, or
599596
- Glob fails to match, if the RHS is a glob.
600-
- `<`: The LHS sorts before the RHS.
601-
- `>`: The LHS sorts after the RHS.
597+
- `<`/`>`: The LHS lexicographically compares less than / greater than the RHS.
602598
- `-n`: The string is not empty (e.g., `[[ -n "$var" ]]`).
603599
- `-z`: The string is empty (length is zero).
604600

@@ -698,3 +694,4 @@ In this book, we'll use `[[ ]]` because it has fewer gotchas.
698694
[^javascript2]: If you know some JavaScript you might be familiar with the problem of too-permissive operators:
699695
in JS, `"4" + 1 == "41"`, but `"4" - 1 == 3`.
700696
[^interview]: Also, why do so many people ask this as an interview question!?
697+
[^lexicographically]: I.e., how [`strcmp()`](http://www.cplusplus.com/reference/cstring/strcmp/) sorts strings: `LHS < RHS` if and only if `strcmp(LHS, RHS) < 0`.

0 commit comments

Comments
 (0)