Last active
February 22, 2019 15:56
-
-
Save benwei/9169dba050272967b3405353c1807e81 to your computer and use it in GitHub Desktop.
Use bash to implement like C's argc and argv
This file contains 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
#!/usr/bin/env bash | |
# Copyright (C) 2019 by Ben Wei <ben@juluos.org> | |
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, | |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF | |
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
###### | |
# sample code of blog | |
## tested with bash 4.3.48 x86_64-pc-linux-gnu ubuntu-16.04.4 LTS | |
verbose=0 | |
clog() | |
{ | |
if [ $verbose -gt 0 ]; then | |
echo $@ | |
fi | |
} | |
syntax() | |
{ | |
cat << EOF >/dev/stdout | |
syntax: [options] <message 1> [message 2] [message 3] | |
options -v verbose of clog | |
EOF | |
exit 1 | |
} | |
while [ 1 ]; do | |
if [[ "$1" =~ "-".* ]]; then | |
if [ "$1" == "-v" ]; then | |
verbose=1 | |
fi | |
shift | |
else | |
break | |
fi | |
done | |
if [ -z "$1" ]; then | |
syntax | |
fi | |
arguments=("$1" "$2" "$3") | |
argc=${#arguments[@]} | |
if [ "$argc" -eq 0 ]; then | |
syntax | |
fi | |
for ((i=0; i<${argc} ; i++)); do | |
if [ "x${arguments[$i]}" = "x" ]; then continue; fi | |
clog "input argument[$i]: ${arguments[$i]}" | |
done | |
exit 0 |
This file contains 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
# origin result | |
bash bash_arguments_example.sh -v "a b" "ff 11" | |
input argument[0]: a b | |
input argument[1]: ff 11 | |
# with debuging option | |
$ bash -x bash_arguments_example.sh -v "a b" "ff 11" | |
+ verbose=0 | |
+ '[' 1 ']' | |
+ [[ -v =~ -.* ]] | |
+ '[' -v == -v ']' | |
+ verbose=1 | |
+ shift | |
+ '[' 1 ']' | |
+ [[ a b =~ -.* ]] | |
+ break | |
+ '[' -z 'a b' ']' | |
+ arguments=("$1" "$2" "$3") | |
+ argc=3 | |
+ '[' 3 -eq 0 ']' | |
+ (( i=0 )) | |
+ (( i<3 )) | |
+ '[' 'xa b' = x ']' | |
+ clog 'input argument[0]: a b' | |
+ '[' 1 -gt 0 ']' | |
+ echo input 'argument[0]:' a b | |
input argument[0]: a b | |
+ (( i++ )) | |
+ (( i<3 )) | |
+ '[' 'xff 11' = x ']' | |
+ clog 'input argument[1]: ff 11' | |
+ '[' 1 -gt 0 ']' | |
+ echo input 'argument[1]:' ff 11 | |
input argument[1]: ff 11 | |
+ (( i++ )) | |
+ (( i<3 )) | |
+ '[' x = x ']' | |
+ continue | |
+ (( i++ )) | |
+ (( i<3 )) | |
+ exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment