Bash array from string with quote parsing
This project is written and tested for devices with Bash version 4.4
(or greater)
This repository encourages the use of Git Submodules to track dependencies
Bash Variables
_module_name='to-array'
_module_https_url="https://github.com/bash-utilities/to-array.git"
_module_base_dir='modules'
_module_path="${_module_base_dir}/${_module_name}"
Bash Submodule Commands
cd "<your-git-project-path>"
git checkout gh-pages
mkdir -vp "${_module_base_dir}"
git submodule add -b main\
--name "${_module_name}"\
"${_module_https_url}"\
"${_module_path}"
Suggested additions for your ReadMe.md
file so everyone has a good time with submodules
Clone with the following to avoid incomplete downloads
git clone --recurse-submodules <url-for-your-project>
Update/upgrade submodules via
git submodule update --init --merge --recursive
git add .gitmodules
git add "${_module_path}"
## Add any changed files too
git commit -F- <<'EOF'
:heavy_plus_sign: Adds `bash-utilities/to-array#1` submodule
**Additions**
- `.gitmodules`, tracks submodules AKA Git within Git _fanciness_
- `README.md`, updates installation and updating guidance
- `modules/to-array`, submodule provides Bash function that splits string to array
EOF
git push origin gh-pages
π Excellent π your project is now ready to begin unitizing code from this repository!
Write a script that sources and utilizes to_array
function...
example.sh
#!/usr/bin/env bash
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
__SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"
## Provides to_array -t '<array_reference>' -i '<string>'
source "${__DIR__}/modules/to-array/to-array.sh"
__usage__() {
cat <<EOF
Converts list of arguments to array(s) and prints results
EOF
}
_arguments=( "${@}" )
for _index in "${!_arguments[@]}"; do
_argument="${_arguments[${_index}]}"
case "${_argument}" in
-h|--help)
__usage__
exit 0
;;
*)
target=()
to_array -t 'target' -i "${_argument}" --strip-quotes
printf '_argument[%i] -> ( %s )\n' "${_index}" "${target[*]@Q}"
;;
esac
done
Provide executable permissions and run example.sh
script...
chmod u+x example.sh
./example.sh '"spam flavored" ham "in a can"'
#> _argument[0] -> ( 'spam flavored' 'ham' 'in a can' )
This repository may not be feature complete and/or fully functional, Pull Requests that add features or fix bugs are certainly welcomed.
To list available parameters use --help
option...
cd "<your-git-project-path>"
modules/to-array/to-array.sh --help
Options for contributing to to-array and bash-utilities
Start making a Fork of this repository to an account that you have write permissions for.
- Add remote for fork URL. The URL syntax is
git@github.com:<NAME>/<REPO>.git
...
cd ~/git/hub/bash-utilities/to-array
git remote add fork git@github.com:<NAME>/to-array.git
- Commit your changes and push to your fork, eg. to fix an issue...
cd ~/git/hub/bash-utilities/to-array
git commit -F- <<'EOF'
:bug: Fixes #42 Issue
**Edits**
- `<SCRIPT-NAME>` script, fixes some bug reported in issue
EOF
git push fork main
Note, the
-u
option may be used to setfork
as the default remote, eg.git push -u fork main
however, this will also default thefork
remote for pulling from too! Meaning that pulling updates fromorigin
must be done explicitly, eg.git pull origin main
- Then on GitHub submit a Pull Request through the Web-UI, the URL syntax is
https://github.com/<NAME>/<REPO>/pull/new/<BRANCH>
Note; to decrease the chances of your Pull Request needing modifications before being accepted, please check the dot-github repository for detailed contributing guidelines.
Thanks for even considering it!
Via Liberapay you may on a repeating basis.
Regardless of if you're able to financially support projects such as to-array that bash-utilities maintains, please consider sharing projects that are useful with others, because one of the goals of maintaining Open Source repositories is to provide value to the community.
Bash array from string with quote parsing
Copyright (C) 2020 S0AndS0
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For further details review full length version of AGPL-3.0 License.