Last active
December 25, 2015 05:09
-
-
Save buzztaiki/6922499 to your computer and use it in GitHub Desktop.
bashのgetoptsの使い方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| usage() { | |
| cat <<EOF | |
| usage: $0 [options] FILE... | |
| -h show this help | |
| -n specify number | |
| EOF | |
| } | |
| while getopts "hn:" opt; do | |
| case $opt in | |
| h) | |
| usage | |
| exit 0 | |
| ;; | |
| n) | |
| NUM="$OPTARG" | |
| ;; | |
| *) | |
| usage 1>&2 | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| if [ $# -eq 0 ]; then | |
| usage 1>&2 | |
| exit 1 | |
| fi | |
| echo num: $NUM | |
| for f in "$@"; do | |
| if [ -e "$f" ]; then | |
| echo exists $f | |
| else | |
| echo not exists $f | |
| fi | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment