Skip to content

Commit 520011d

Browse files
authored
Create getopts.md
1 parent a7f8843 commit 520011d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

getopts.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
An example of how to use ```getopts``` shell built-in command to process shell arguments.
2+
The use of ```getopts``` is actually well-known with old-timers.
3+
However, new gens are usually cycled to ```agrc``` and ```argv```.
4+
Save some time and use the built-in. :)
5+
6+
```bash
7+
while getopts df: flag
8+
do
9+
case $flag in
10+
11+
d)
12+
echo debugging on
13+
;;
14+
f)
15+
file=$OPTARG
16+
echo filename is $file
17+
;;
18+
?)
19+
exit
20+
;;
21+
esac
22+
done
23+
shift $(( OPTIND - 1 )) # shift past the last flag or argument
24+
25+
echo parameters are $*
26+
```

0 commit comments

Comments
 (0)