mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-25 09:36:31 -05:00
New Post
This commit is contained in:
parent
409c456e52
commit
705bec5366
1 changed files with 27 additions and 0 deletions
27
content/blog/quickbashargcount.md
Normal file
27
content/blog/quickbashargcount.md
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
---
|
||||||
|
title: "Quick Bash: Check Argument Count"
|
||||||
|
date: 2020-12-15T09:25:11-05:00
|
||||||
|
draft: false
|
||||||
|
tags: ["bash"]
|
||||||
|
---
|
||||||
|
|
||||||
|
I've been writing more bash scripts recently and I noticed that I often check for the number of arguments before validating them in my scripts. I'll share that small snippet here for my future self.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -o errexit
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
show_usage() {
|
||||||
|
echo "Usage: script [arg1]"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check argument count
|
||||||
|
if [ "$#" -ne 1 ]; then
|
||||||
|
show_usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
```
|
Loading…
Reference in a new issue