Skip to content

Instantly share code, notes, and snippets.

@buzztaiki
Last active December 25, 2015 05:09
Show Gist options
  • Select an option

  • Save buzztaiki/6922499 to your computer and use it in GitHub Desktop.

Select an option

Save buzztaiki/6922499 to your computer and use it in GitHub Desktop.
bashのgetoptsの使い方
#!/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