website/content/blog/quickbashargcount.md

30 lines
549 B
Markdown
Raw Normal View History

2020-12-15 09:30:48 -05:00
---
2023-03-10 12:41:04 -05:00
date: 2020-12-15 14:25:11
2020-12-15 09:30:48 -05:00
draft: false
2023-01-05 14:04:45 -05:00
medium_enabled: true
2023-03-10 12:41:04 -05:00
medium_post_id: ac38c9d9d896
tags:
- Bash
title: 'Quick Bash: Check Argument Count'
2020-12-15 09:30:48 -05:00
---
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
```