You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-Bash-Scripting.md
+17-20Lines changed: 17 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,7 @@ To determine whether a command succeeded or failed, you can check the `$?` varia
72
72
Traditionally, a value of `0` indicates success, and a non-zero value indicates failure.
73
73
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.
74
74
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`:
76
76
```
77
77
$ g++ no-such-file.cpp
78
78
g++: error: no-such-file.cpp: No such file or directory
@@ -84,7 +84,7 @@ $ echo $?
84
84
85
85
Bash also provides variables holding the command-line arguments passed to the script.
86
86
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]
88
88
The variables `$1` through `$9` contain the first through ninth command line arguments, respectively.
89
89
To get the 10th argument, you have to write `${10}`, and likewise for higher argument numbers.
90
90
@@ -135,14 +135,12 @@ Here's an example of how to write `if` statements in Bash:
135
135
136
136
# Emit the appropriate greeting for various people
137
137
138
-
if [[ $1="Jeff" ]];then
138
+
if [[ "$1"="Jeff" ]];then
139
139
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
145
141
echo"FREEDOM!"
142
+
elif [[ "$1"==*.txt ]];then
143
+
echo"You’re a text file, $1"
146
144
else
147
145
echo"Who in blazes are you?"
148
146
fi
@@ -163,8 +161,7 @@ Comparing Strings:
163
161
-`!=` means either:
164
162
- String inequality, if both operands are strings, or
165
163
- 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.
168
165
-`-n`: The string is not empty (e.g., `[[ -n "$var" ]]`).
169
166
-`-z`: The string is empty (length is zero).
170
167
@@ -204,7 +201,7 @@ if [[ -f "$1" ]]; then
204
201
elif [[ -d"$1" ]];then
205
202
ls "$1"
206
203
else
207
-
echo"I don't know what to do with $1!";
204
+
echo"I don't know what to do with $1!"
208
205
fi
209
206
```
210
207
@@ -365,7 +362,7 @@ If you need a counting for loop (C-style loop), you can get one of those with `(
365
362
#!/bin/bash
366
363
367
364
for(( i=1; i <9; i++));do
368
-
echo$i;
365
+
echo$i
369
366
done
370
367
```
371
368
@@ -467,6 +464,8 @@ You can scope variables to functions with the `local` builtin; run `help local`
467
464
468
465
### Tips
469
466
467
+
Bash contains a help system for its built-in commands: `help pushd` tells you information about the `pushd` command.
468
+
470
469
To write a literal `\` , `` ` ``, `$`, `"`, `'`, or `#`, escape it with `\`; for instance, `"\$"` gives a literal `$`.
471
470
472
471
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
507
506
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.
508
507
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`!
509
508
510
-
Bash contains a help system for its built-in commands: `help pushd` tells you information about the `pushd` command.
511
-
512
509
### Customizing Bash
513
510
514
511
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.
574
571
## Questions
575
572
Name: `______________________________`
576
573
577
-
1.What does the `let` builtin do?
574
+
1.In your own words, what is the difference between `g++ $file` and `g++ "$file"`?
578
575
\vspace{10em}
579
576
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}
582
579
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]
584
581
\newpage
585
582
586
583
## Quick Reference
@@ -597,8 +594,7 @@ Comparing Strings:
597
594
-`!=` means either:
598
595
- String inequality, if both operands are strings, or
599
596
- 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.
602
598
-`-n`: The string is not empty (e.g., `[[ -n "$var" ]]`).
603
599
-`-z`: The string is empty (length is zero).
604
600
@@ -698,3 +694,4 @@ In this book, we'll use `[[ ]]` because it has fewer gotchas.
698
694
[^javascript2]: If you know some JavaScript you might be familiar with the problem of too-permissive operators:
699
695
in JS, `"4" + 1 == "41"`, but `"4" - 1 == 3`.
700
696
[^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