linux - 为位于我的一个文件夹中的 shell 脚本设置别名
问题描述
所以我有这个文件夹 BashScripts 有以下目录路径
/home/sadnan/BashScripts
/home/sadnan
成为我的$HOME
。这是我打算放置自定义 shell 脚本的地方。目前只有一个名为cbi.sh
. 当我从文件夹内部运行时,脚本按预期工作。现在我希望能够在全球范围内运行它。所以我将以下内容添加到我的 .bashrc 文件中
#For my personal Bash Scripts
if [ -d $HOME/BashScripts ]; then
PATH="$PATH:$HOME/BashScripts"
fi
alias cbi='. ./cbi.sh'
所以现在当我这样做echo $PATH
时会打印出以下内容
/home/sadnan/.nvm/versions/node/v12.18.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/sadnan/BashScripts
因此该文件夹似乎已添加到 PATH 中。如果我理解正确的话,应该允许我在全球范围内运行我的 shell 脚本。
然后我还为脚本设置了一个别名并命名它cbi
但是现在当我跑步cbi
或./cbi.sh
在我的终端上时,这就是我得到的
bash: ./cbi.sh: No such file or directory
我做错了什么,我没有做太多的shell脚本。但我认为这是要走的路。
我也尝试过alias cbi=`source ./cbi.sh'
,alias cbi=`sh ./cbi.sh'
但都没有奏效。
解决方案
你必须运行cbi.sh
:
# Wrong, the file is not named 'cbi'
cbi
# Wrong, the file is not in the current directory
./cbi.sh
# Wrong, same reason as above
alias cbi='. ./cbi.sh'
cbi
# Correct, this is the name of the file:
cbi.sh
推荐阅读
- r - 我如何将代码计算为百分比?
- c# - C# - 如何关闭 From 和 Application.Run
- python - 测试 python selenium web scraper 的代码时出现 AWS Lambda 错误
- c++ - 如果将结构放入结构中会发生什么?
- azure-devops - 如何在 azure 管道模板中使用 powershell/bash 脚本文件
- sas - SAS中的数字宏
- mysql - 将 MySQL 数据库与节点 JavaScript 应用程序连接起来
- core-data - SwiftUI 列表,其中包含从核心数据中获取的部分
- javascript - 将类名传递给函数参数
- python - class_weights 上的 lightGBM 分类器错误