-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGETOPTS-LONG-OPTTIONS.txt
More file actions
64 lines (47 loc) · 1.31 KB
/
GETOPTS-LONG-OPTTIONS.txt
File metadata and controls
64 lines (47 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
techpatterns.com/forums/about1138.html
https://sites.google.com/site/xiangyangsite/home/technical-tips/linux-unix/shell-programming/bash-tips/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
https://www.codebyamir.com/blog/parse-command-line-arguments-using-getopt
https://techpatterns.com/forums/about1138.html
https://linuxhint.com/bash_getopts_example/
#!/bin/bash
while getopts "h-:" opt; do
if [ "$opt" == "-" ]; then opt=$OPTARG; fi;
case $opt in
h|help)
echo "You need help I am not trained or licensed to provide."
exit 0
;;
*)
echo "Invalid option"
exit 1
;;
esac
done
=====================================================
#https://stackoverflow.com/questions/3534280/how-can-i-pass-a-file-argument-to-my-bash-script-using-a-terminal-command-in-lin
#!/bin/sh
OPTIONS=$(getopt -o hf:gb -l help,file:,foo,bar -- "$@")
if [ $? -ne 0 ]; then
echo "getopt error"
exit 1
fi
eval set -- $OPTIONS
while true; do
case "$1" in
-h|--help) HELP=1 ;;
-f|--file) FILE="$2" ; shift ;;
-g|--foo) FOO=1 ;;
-b|--bar) BAR=1 ;;
--) shift ; break ;;
*) echo "unknown option: $1" ; exit 1 ;;
esac
shift
done
if [ $# -ne 0 ]; then
echo "unknown option(s): $@"
exit 1
fi
echo "help: $HELP"
echo "file: $FILE"
echo "foo: $FOO"
echo "bar: $BAR"