diff options
author | Jasper Ras <jaspert.ras@gmail.com> | 2025-04-18 21:01:49 +0200 |
---|---|---|
committer | Jasper Ras <jaspert.ras@gmail.com> | 2025-04-18 21:01:49 +0200 |
commit | a8a8e1f984f20c8008f3a5f57cd39b416eb73104 (patch) | |
tree | 2262794b52d82c5fd9cbc55b503b52fafbd3af8a /How to properly do options in a bash script with getopt!.md | |
parent | 5fa69499917fab7a026c90c8321dbcc22734106a (diff) | |
parent | ab409a3701bf59dd73dc1e0324376bdac8b6d74f (diff) |
vault backup: 2025-04-18 21:01:49
Diffstat (limited to 'How to properly do options in a bash script with getopt!.md')
-rw-r--r-- | How to properly do options in a bash script with getopt!.md | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/How to properly do options in a bash script with getopt!.md b/How to properly do options in a bash script with getopt!.md new file mode 100644 index 0000000..7d417c5 --- /dev/null +++ b/How to properly do options in a bash script with getopt!.md @@ -0,0 +1,31 @@ +--- +tags: + - bash +--- +``` +#!/bin/bash + +while getopts "ab:c" opt; do + case "$opt" in + a) + echo "Option -a was specified." + ;; + b) + echo "Option -b was specified with argument: $OPTARG" + ;; + c) + echo "Option -c was specified." + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done + +shift $((OPTIND - 1)) + +echo "Remaining arguments: $@" +``` + +man bash // getopt
\ No newline at end of file |