mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-10-09 22:41:13 +00:00
Website snapshot
This commit is contained in:
parent
ee0ab66d73
commit
50ec3688a5
281 changed files with 21066 additions and 0 deletions
32
content/blog/bashflags.md
Normal file
32
content/blog/bashflags.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
---
|
||||
title: "Bash Flags"
|
||||
date: 2019-08-06T16:55:47-04:00
|
||||
draft: false
|
||||
---
|
||||
|
||||
I was creating a bash script and was looking around for a solution for parsing command line arguments. [This StackOverflow post](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) has a variety of different solutions available. I want to describe my favorite of these posts.
|
||||
|
||||
[Inanc Gumus](https://stackoverflow.com/users/115363/inanc-gumus) proposed the following:
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
while [[ "$#" -gt 0 ]]; do case $1 in
|
||||
-d|--deploy) deploy="$2"; shift;;
|
||||
-u|--uglify) uglify=1;;
|
||||
*) echo "Unknown parameter passed: $1"; exit 1;;
|
||||
esac; shift; done
|
||||
|
||||
echo "Should deploy? $deploy"
|
||||
echo "Should uglify? $uglify"
|
||||
```
|
||||
|
||||
Let me quickly describe what it does. While the number of arguments left to process is greater than zero....
|
||||
|
||||
- Check to see if the argument matches any of the flags
|
||||
- If it does...
|
||||
- If the flag requires an additional argument grab it. Then discard an argument.
|
||||
- If it doesn't. Error out.
|
||||
- Then get rid of an argument.
|
||||
|
||||
At the end of the while loop, you would've evaluated all the arguments!
|
Loading…
Add table
Add a link
Reference in a new issue