Skip to content

bash-utilities/array-splice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Array Splice

Removes elements from Bash array

Byte size of Array Splice Open Issues Open Pull Requests Latest commits Build Status



Requirements

This project is built and tested on devices with Bash version 4.4 or greater. Please check that your system has this available prior to opening new Issues, eg...

bash --version | awk '/version/ { print; exit 0; }'
#> GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)

Quick Start

This repository encourages the use of Git Submodules to track dependencies

Bash Variables

_module_name='array-splice'
_module_https_url="https://github.com/bash-utilities/array-splice.git"
_module_base_dir='modules'
_module_path="${_module_base_dir}/${_module_name}"

Bash Submodule Commands

cd "<your-git-project-path>"

mkdir -vp "${_module_base_dir}"

git submodule add --name "${_module_name}"\
                  -b main\
                  "${_module_https_url}"\
                  "${_module_path}"

Your ReadMe File

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

Commit and Push

git add .gitmodules
git add "${_module_path}"


## Add any changed files too


git commit -F- <<'EOF'
:heavy_plus_sign: Adds `bash-utilities/array-splice#1` submodule



**Additions**


- `.gitmodules`, tracks submodules AKA Git within Git _fanciness_

- `README.md`, updates installation and updating guidance

- `modules/array-splice`, submodule removes elements from Bash array
EOF


git push origin gh-pages

πŸŽ‰ Excellent πŸŽ‰ your project is now ready to begin unitizing code from this repository!


Usage

Write a script that makes use of array_splice function...

#!/usr/bin/env bash


## Find directory that this script resides in
__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 array_splice '<list_ref>' '<item>' '<offset>'
source "${__DIR__}/modules/array-splice/array-splice.sh"


list=(
    --beginning 'foo'
    --middle 'bar'
    --end 'spam'
)

printf '${list[*]} -> ( %s )\n' "${list[*]}"

echo "#> array_splice --target 'list' --element '--middle' --offset 1"
array_splice --target 'list' --element '--middle' --offset 1

printf '${list[*]} -> ( %s )\n' "${list[*]}"
## Expected output
#> ${list[*]} -> ( --beginning foo --end spam )

Alternatively this project maybe utilized interactively by sourcing within a .profile, or similar, configuration file, eg...

mkdir -vp ~/git/hub/bash-utilities
cd ~/git/hub/bash-utilities

git clone [email protected]:bash-utilities/array-splice.git

tee -a "${HOME}/.profile" 1>/dev/null <<'EOF'
# Add array_splice to interactive shell
source ~/git/hub/bash-utilities/array-splice
EOF

Use --help flag to list available parameters, and assigned values, that array_splice function responds to...

array_splice --help

API

Removes element from array plus/minus offset


## Parameters


-t    --target <ArrayReferance>

    {Required} - Target array reference to remove element(s) from


-d    --deleted <ArrayReferance>

    {Optional} - Array reference to append removed element(s) to


-o    --offset <number> 0

    {Optional} - Index offset, may be negative or positive default: 1


-e    --element <number|string> "Some Value"

    {Required} - Element , if "--index" or "--regexp" is not defined, value to remove from target array


-i    --index <number> 0

    {Required} - If "--element" or "--regexp" is not defined, indexed value to remove from target array


-r    --regexp <RegExp> "^--[[:print:]].*"

    {Required} - If "--element" or "--index" is not defined, regular expression to remove from target array


-v    --verbose 0

    {Optional} - Prints parsed options and results if flag is present


-h    --help 1

    {Optional} - Prints this message if flag is present

Notes

This repository may not be feature complete and/or fully functional, Pull Requests that add features or fix bugs are certainly welcomed.


Contributing

Options for contributing to array-splice and bash-utilities


Forking

Start making a Fork of this repository to an account that you have write permissions for.

cd ~/git/hub/bash-utilities/array-splice

git remote add fork [email protected]:<NAME>/array-splice.git
  • Commit your changes and push to your fork, eg. to fix an issue...
cd ~/git/hub/bash-utilities/array-splice


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 set fork as the default remote, eg. git push -u fork main however, this will also default the fork remote for pulling from too! Meaning that pulling updates from origin 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.


Sponsor

Thanks for even considering it!

Via Liberapay you may sponsor__shields_io__liberapay on a repeating basis.

Regardless of if you're able to financially support projects such as array-splice 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.


Attribution


License

Removes elements from Bash array
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.