I have bash function directory located at /git/function
This is structure:
/git/function/
├── delete
│ ├── delete.dir
│ └── delete.file
├── main.sh
├── get
│├── get.dir.sh
│└── get.file.sh
How to use main.sh
usage: get + space + TAB(to autocomplete from /git/function/get/ directory)
output:
dir file
Next: when I select select get file it runs script from /git/function/get/get.file.sh
#!/bin/bash
# Directory where the functions are located
functiondir="/git/function"
# List of supported commands:
commands=(
"get"
"delete"
)
# Function to execute the 'get' command
get() {
echo "Running $functiondir/get/get.$1.sh"
}
# Function to execute the 'delete' command
delete() {
echo "Running $functiondir/delete/delete.$1.sh"
}
# Autocompletion function for command names
_completion() {
# Find all files in the specified function directory with the given command name
local function_files=$(find "$functiondir/$1" -maxdepth 1 -type f -name "$1.*.sh" -printf '%f\n' | sed "s/^$1\.//;s/\.sh$//")
# Generate autocompletion options based on the found files
COMPREPLY=($(compgen -W "$function_files" -- "${COMP_WORDS[COMP_CWORD]}"))
}
# Set autocompletion for 'get' and 'delete' commands using the _completion function
complete -F _completion -o filenames "${commands[@]}"
I have couple questions:
- main.sh is sourced from .bashrc
- I want to use . instead of space for command completion,
- I need dynamically generate content from sub-dir
/git/function/without adding additional functions to main.sh - the fist part of the command for example: if I type get.TAB (I should check if
sub-direxist and if true then generate content of this directory/git/function/getas autocomplete) - if I type delete.TAB (I should check if
sub-direxist and if true then generate content of this directory/git/function/deleteas autocomplete)
If you know better solution to dynamically load custom functions I will appreciate if you can share some tips
delete.diras a command word, how does the shell find this command? Are the directories in the$PATH? Would it be an option to create an alias for each of them?