File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
9
9
10
10
For a steady stream of TILs, [ sign up for my newsletter] ( https://tinyletter.com/jbranchaud ) .
11
11
12
- _ 950 TILs and counting..._
12
+ _ 951 TILs and counting..._
13
13
14
14
---
15
15
@@ -41,6 +41,7 @@ _950 TILs and counting..._
41
41
* [ React Testing Library] ( #react-testing-library )
42
42
* [ ReasonML] ( #reasonml )
43
43
* [ Ruby] ( #ruby )
44
+ * [ Shell] ( #shell )
44
45
* [ tmux] ( #tmux )
45
46
* [ Unix] ( #unix )
46
47
* [ Vim] ( #vim )
@@ -819,6 +820,10 @@ _950 TILs and counting..._
819
820
- [ Wrap Things In An Array, Even Hashes] ( ruby/wrap-things-in-an-array-even-hashes.md )
820
821
- [ Zero Padding] ( ruby/zero-padding.md )
821
822
823
+ ### Shell
824
+
825
+ - [ Check If The First Argument Is Given] ( shell/check-if-the-first-argument-is-given.md )
826
+
822
827
### tmux
823
828
824
829
- [ Adjusting Window Pane Size] ( tmux/adjusting-window-pane-size.md )
Original file line number Diff line number Diff line change
1
+ # Check If The First Argument Is Given
2
+
3
+ In a shell script, you may want to check if an argument was given. Each
4
+ argument is referenced numerically with the ` $ ` prefix, so the first argument
5
+ is ` $1 ` . To check if the first argument is given, you can use the ` -z ` check.
6
+
7
+ ``` bash
8
+ if [ -z " $1 " ]
9
+ then
10
+ echo " The first argument is missing"
11
+ exit 1
12
+ fi
13
+ ```
14
+
15
+ The ` -z ` checks if the argument is a zero-length string (so ` "" ` or undefined
16
+ will be true). If it is missing, then we echo out a message and exit the
17
+ script. This is how I might fashion a script that requires the first argument.
18
+
19
+ [ source] ( https://stackoverflow.com/questions/6482377/check-existence-of-input-argument-in-a-bash-shell-script )
You can’t perform that action at this time.
0 commit comments