这里有一个简单的别名的方式侵扰程度较低的替代方案:
alias lx='PATH="./bin:$PATH"'
然后,您可以使用lx
(大号 OCALËX ecution)任何文件夹中,有一个bin
子目录执行脚本没有路径;例如,从当前目录执行脚本./bin/start-server
,运行:
lx start-server
如果要启用基于对本地可执行制表完成,以下内容添加到您的bash配置文件(测试在OSX和Linux上):
# Install the custom tab-completion function defined below.
complete -F _complete_lx -- lx
# Command-completion function for lx: tab-completes the executables in
# the ./bin subfolder.
_complete_lx() {
# Set the directory in which to look for local executables.
local localExeDir='./bin'
# Get the current command-line token.
local token=${COMP_WORDS[$COMP_CWORD]}
local tokenLen=${#token}
# Find all local executables.
read -d ' ' -ra localExes < <(find "$localExeDir" -maxdepth 1 -type f -perm -a=x -exec basename -a {} +;)
# Filter the list of local executables
# based on the current command-line token.
# Turn case-insensitive matching temporarily on, if necessary.
local nocasematchWasOff=0
shopt nocasematch >/dev/null || nocasematchWasOff=1
((nocasematchWasOff)) && shopt -s nocasematch
COMPREPLY=()
for localExe in "${localExes[@]}"; do
if [[ ${localExe:0:$((tokenLen))} == "$token" ]]; then
COMPREPLY+=("$localExe")
fi
done
# Restore state of 'nocasematch' option, if necessary.
((nocasematchWasOff)) && shopt -u nocasematch
}
想让“.runme”脚本在有人向目录中执行“cd”时自动运行? – 2011-04-08 11:34:19
是的,这是可能的吗? – 2011-04-08 11:39:50