Created
November 7, 2012 17:44
-
-
Save killwing/4033179 to your computer and use it in GitHub Desktop.
[convertArgsStrToArray] convert args str to array
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
#!/bin/bash | |
returnVal=() | |
convertArgsStrToArray() { | |
local concat="" | |
local t="" | |
returnVal=() | |
for word in $@; do | |
local len=`expr "$word" : '.*"'` | |
[ "$len" -eq 1 ] && concat="true" | |
if [ "$concat" ]; then | |
t+=" $word" | |
else | |
word=${word#\"} | |
word=${word%\"} | |
returnVal+=("$word") | |
fi | |
if [ "$concat" -a "$len" -gt 1 ]; then | |
t=${t# } | |
t=${t#\"} | |
t=${t%\"} | |
returnVal+=("$t") | |
t="" | |
concat="" | |
fi | |
done | |
} | |
# example | |
lsArgs='-hlF soft "my code" "" "docs" "mystuff" "pic music video"' | |
convertArgsStrToArray $lsArgs | |
ls "${returnVal[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment