Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env nix-shell
- #! nix-shell -i zsh -p zsh --pure
- #
- # Remove empty and duplicate entries from a colon-separated path.
- #
- # For example: ":a:b:b:::c:a::" --> "a:b:c"
- #
- # Ensure that the script uses only built-in shell commands.
- PATH=''
- delete_duplicates() {
- if [[ ${#entries} -ne 0 ]]; then
- local i=1
- while [[ ${i} -lt ${#entries} ]]; do
- local j=
- (( j=${i} + 1 ))
- while [[ ${j} -le ${#entries} ]]; do
- if [[ "${entries[${i}]}" == "${entries[${j}]}" ]]; then
- # Delete the duplicate entry.
- entries[${j}]=()
- else
- (( j=${j} + 1 ))
- fi
- done
- (( i=${i} + 1 ))
- done
- fi
- }
- if [[ ${#} != 1 ]]; then
- echo "${0}: command line error" 1>&2
- echo "Usage: ${0} PATH" 1>&2
- exit_status=1
- else
- dirty_path="${1}"
- # Split the path at ':' characters.
- entries=(${(s/:/)dirty_path})
- delete_duplicates
- # Join the path with ':' characters.
- clean_path="${(j/:/)entries}"
- echo -n "${clean_path}"
- exit_status=0
- fi
- exit ${exit_status}
Advertisement
Add Comment
Please, Sign In to add comment