function - 在系统中注册自定义 shell 函数 (OSX)

标签 function shell bash

今天我想编写一个函数来自动将我的博客页面(在 nanoc 中开发)部署到 github 页面 - 这是脚本:

function cmit()
{
  nanoc compile;
  git add .;
  git commit -am $1;
  git push origin source;
  cd output; git add .;
  git commit -am $1;
  git push origin master;
  cd ..;
  echo 'new version deployed! now starting nanoc locally..';
  nanoc aco;
}

示例用法:cmit "my example commit!"

我真的不知道如何在系统 (OSX) 中注册我的函数 - 在 .bashrc.bash_profile 或其他地方?请帮忙!

最佳答案

只需将它添加到您的 ~/.bashrc 文件的底部,然后您就可以像常规命令一样使用 cmit,您需要刷新当前的shell 获取更改,因此运行 source ~/.bashrc。如果您将函数保存在文件 cmit 中,只需执行 cat cmit >> ~/.bashrc 将函数附加到 ~/.bashrc 的末尾

您可以先尝试一个测试函数:

# add to ~/.bashrc first
function test() {
    echo "Some test foo!"
}

$ source ~/.bashrc

$ test
Some test foo!

关于function - 在系统中注册自定义 shell 函数 (OSX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14193207/

相关文章:

linux - getopts 空参数和默认值

linux - 如何找到文件所有者的权限并使用脚本将相同的权限分配给组和其他人

bash + awk + ​​grep ....解析并保存变量

linux - 使用 awk 输入列

python - 在python中获取执行时间函数

function - 你必须声明一个函数的类型吗?

c - 这个函数做了什么来帮助它以不同的方式接受输入以及如何执行 for 循环中的条件

bash - 使用 aria2c 并行下载多个文件

用于 tsv 平面文件的 python/bash SQL(无 sqlite)

python - 在 Python 中,使用 for 循环和乘法创建幂函数