将 bash 脚本转换为鱼
convert bash script to fish
我想将 Bash 脚本转换为 Fish 脚本以测试文件 .nvmrc
是否存在。
Bash 脚本:
## Auto load nvm when there's a .nvmrc file
OLD_PWD=""
promptCommand() {
if [ "$OLD_PWD" != "$PWD" ] ;
then
OLD_PWD="$PWD"
if [ -e .nvmrc ] ;
then nvm use;
fi
fi
}
export PROMPT_COMMAND=promptCommand
和 Fish 脚本(不起作用):
set OLD_PWD ""
function nvm_prompt
if [ "$OLD_PWD" != "$PWD" ]
then
OLD_PWD="$PWD"
if [ -e .nvmrc ]
then bass source ~/.nvm/nvm.sh --no-use ';' nvm use
end
end
end
首先,鱼的if
并没有使用then
这个词。就这么没了。
所以
if [ "$OLD_PWD" != "$PWD" ]
then
变成了
if [ "$OLD_PWD" != "$PWD" ]
(与其他类似 if
)
其次,
OLD_PWD="$PWD"
不是有效的 fish 脚本(它会告诉您)。使用
set -g OLD_PWD "$PWD"
第三,就目前而言,此函数当前已定义但从未定义 运行。当 PWD 更改时,您需要某种方式来执行它。而且,幸运的是,fish 有一种方法可以在变量变化时将函数定义为 运行 - function
的 --on-variable VARNAME
选项。
所以你的解决方案看起来像这样:
function nvm_prompt --on-variable PWD
if [ "$OLD_PWD" != "$PWD" ]
set -g OLD_PWD "$PWD"
if [ -e .nvmrc ]
bass source ~/.nvm/nvm.sh --no-use ';' nvm use
end
end
end
您甚至可以取消 $OLD_PWD 检查,也可以不这样做,因为当您这样做时也会触发该事件,例如cd .
(即当变量再次设置为相同的值时)。
另外,我假设这个名字的意思是当显示提示时它是 运行,而不是它实际上自己显示任何东西 - 在这种情况下你会把它贴在你的 fish_prompt
函数(尝试 funced fish_prompt
和 funcsave fish_prompt
)。
我想将 Bash 脚本转换为 Fish 脚本以测试文件 .nvmrc
是否存在。
Bash 脚本:
## Auto load nvm when there's a .nvmrc file
OLD_PWD=""
promptCommand() {
if [ "$OLD_PWD" != "$PWD" ] ;
then
OLD_PWD="$PWD"
if [ -e .nvmrc ] ;
then nvm use;
fi
fi
}
export PROMPT_COMMAND=promptCommand
和 Fish 脚本(不起作用):
set OLD_PWD ""
function nvm_prompt
if [ "$OLD_PWD" != "$PWD" ]
then
OLD_PWD="$PWD"
if [ -e .nvmrc ]
then bass source ~/.nvm/nvm.sh --no-use ';' nvm use
end
end
end
首先,鱼的if
并没有使用then
这个词。就这么没了。
所以
if [ "$OLD_PWD" != "$PWD" ]
then
变成了
if [ "$OLD_PWD" != "$PWD" ]
(与其他类似 if
)
其次,
OLD_PWD="$PWD"
不是有效的 fish 脚本(它会告诉您)。使用
set -g OLD_PWD "$PWD"
第三,就目前而言,此函数当前已定义但从未定义 运行。当 PWD 更改时,您需要某种方式来执行它。而且,幸运的是,fish 有一种方法可以在变量变化时将函数定义为 运行 - function
的 --on-variable VARNAME
选项。
所以你的解决方案看起来像这样:
function nvm_prompt --on-variable PWD
if [ "$OLD_PWD" != "$PWD" ]
set -g OLD_PWD "$PWD"
if [ -e .nvmrc ]
bass source ~/.nvm/nvm.sh --no-use ';' nvm use
end
end
end
您甚至可以取消 $OLD_PWD 检查,也可以不这样做,因为当您这样做时也会触发该事件,例如cd .
(即当变量再次设置为相同的值时)。
另外,我假设这个名字的意思是当显示提示时它是 运行,而不是它实际上自己显示任何东西 - 在这种情况下你会把它贴在你的 fish_prompt
函数(尝试 funced fish_prompt
和 funcsave fish_prompt
)。