Last active
August 29, 2015 14:18
-
-
Save joshenders/3bbd2e4d86d0f9553ca0 to your computer and use it in GitHub Desktop.
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
jenders@jenders-mba tmp :) $ cat ./foo.sh | |
#!/bin/bash | |
echo "\$@: $@" | |
flags=() | |
for flag in "$@"; do | |
if [[ "${flag}" =~ ^- ]]; then | |
flags=("${flags[@]}" "${flag}") | |
fi | |
done | |
echo "flags: ${flags[@]}" | |
echo "flag count: ${#flags[@]}" | |
operands=() | |
for operand in "$@"; do | |
if [[ ! "${operand}" =~ ^- ]]; then | |
operands=("${operands[@]}" "${operand}") | |
fi | |
done | |
echo "operands: ${operands[@]}" | |
echo "operand count: ${#operands[@]}" | |
jenders@jenders-mba tmp :) $ ./foo.sh "barrack obama" george w bush --flag | |
$@: barrack obama george w bush --flag | |
flags: --flag | |
flag count: 1 | |
operands: barrack obama george w bush | |
operand count: 4 | |
jenders@jenders-mba tmp :) $ bash -x ./foo.sh "barrack obama" george w bush --flag | |
+ echo '$@: barrack obama' george w bush --flag | |
$@: barrack obama george w bush --flag | |
+ flags=() | |
+ for flag in '"$@"' | |
+ [[ barrack obama =~ ^- ]] | |
+ for flag in '"$@"' | |
+ [[ george =~ ^- ]] | |
+ for flag in '"$@"' | |
+ [[ w =~ ^- ]] | |
+ for flag in '"$@"' | |
+ [[ bush =~ ^- ]] | |
+ for flag in '"$@"' | |
+ [[ --flag =~ ^- ]] | |
+ flags=("${flags[@]}" "${flag}") | |
+ echo 'flags: --flag' | |
flags: --flag | |
+ echo 'flag count: 1' | |
flag count: 1 | |
+ operands=() | |
+ for operand in '"$@"' | |
+ [[ ! barrack obama =~ ^- ]] | |
+ operands=("${operands[@]}" "${operand}") | |
+ for operand in '"$@"' | |
+ [[ ! george =~ ^- ]] | |
+ operands=("${operands[@]}" "${operand}") | |
+ for operand in '"$@"' | |
+ [[ ! w =~ ^- ]] | |
+ operands=("${operands[@]}" "${operand}") | |
+ for operand in '"$@"' | |
+ [[ ! bush =~ ^- ]] | |
+ operands=("${operands[@]}" "${operand}") | |
+ for operand in '"$@"' | |
+ [[ ! --flag =~ ^- ]] | |
+ echo 'operands: barrack obama' george w bush | |
operands: barrack obama george w bush | |
+ echo 'operand count: 4' | |
operand count: 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment