简体   繁体   English

获取自定义 bash 脚本的可接受方式是什么?

[英]What is the acceptable way to source custom bash scripts?

I am at beginning the process of automating some documentation functionality on my MBP and would like to learn a few good ways to source custom scripts for use with bash.我正在开始在我的 MBP 上自动化一些文档功能的过程,并且想学习一些很好的方法来获取自定义脚本以用于 bash。 I have (within the last year) picked up a MBP and been learning the ropes for package management and how shell invocation is happening.我(在去年)拿起了一个 MBP 并一直在学习 package 管理以及 shell 调用是如何发生的。 I have a 5.x version of bash from brew and originally started my config.dot-file with a.bash_profile.我有一个来自 brew 的 bash 的 5.x 版本,最初使用 .bash_profile 启动了我的 config.dot 文件。 I have built a script with a function that will touch a readme.md from a newly created working directory that helps me automatically create an associated readme when creating a new directory- which looks like so:我已经使用 function 构建了一个脚本,它将触及新创建的工作目录中的 readme.md,它可以帮助我在创建新目录时自动创建关联的自述文件 - 如下所示:

function mkr() {
    touch $1/readme.md
    if [ $? -eq 0 ]
    then
        echo "Successfully created file" # stdout
        # exit 0 # success but it kills the shell?
    else
        echo "Could not create file" >&2 # out to stderr
        # exit 1 # they killed kinney
    fi
}

######CLI to invoke the above script named .mk_readme.sh
$ mkdir ~/path/to/new/dir
$ mkr !!:1

My concerns are:我的担忧是:

  1. Is there more efficient ways to handle a simple task to speed up workflow like the above?是否有更有效的方法来处理简单的任务以加快上述工作流程?
  2. When sourcing the file would it be best just to source the individual script or directory of custom scripts as needed.在获取文件时,最好只根据需要获取单个脚本或自定义脚本的目录。 My guess is the solution depends on use case.我的猜测是解决方案取决于用例。 Maybe we are needing devs to document their work and all needed scripts for them to make use of and get sourced from this aggregated directory.也许我们需要开发人员记录他们的工作和所有需要的脚本,以便他们使用并从这个聚合目录中获取资源。 Then we look at the application side of sourcing and aggregate those separately?然后我们看看采购的应用端并分别聚合它们?
  3. I am sourcing the file currently in the.bash_profile with source./.mk_readme.sh .我正在使用source./.mk_readme.sh获取当前在 .bash_profile 中的文件。 I read an article that addresses doing this through the.bashrc.我阅读了一篇通过 .bashrc 解决此问题的文章。 What is the ramifications of this and is it best practice to place a source.bashrc into the.bash_profile for this to occur, or just leave the directories from ~ that need sourcing done straight from the.bash_profile.这样做的后果是什么?最好的做法是将source.bashrc放入 .bash_profile 中以发生这种情况,或者直接从 ~ 中保留需要从 .bash_profile 进行采购的目录。
  4. Can anyone give me a quick ironing out on the best way to handle the exit status here?谁能给我一个快速解决这里处理退出状态的最佳方法的问题? I am using VSCode and it kills the inner terminal after successfully creating the readme.我正在使用 VSCode,它会在成功创建自述文件后杀死内部终端。 In my current thought process if there was a problem with calling mkr::.1 (maybe I called !!:2 by accident) I might want a warning that the file creation was unsuccessful but it doesn't clobber my currently running shell from within there.在我目前的思考过程中,如果调用 mkr::.1 时出现问题(也许我不小心调用了 !!:2),我可能想要警告文件创建不成功,但它不会破坏我当前正在运行的 shell里面。

I may have started thinking of the answer, as it may lay in what loads when on a Mac or Posix/Unix/Linux machine.我可能已经开始考虑答案,因为它可能在于 Mac 或 Posix/Unix/Linux 机器上的负载。 I am new to Mac and have dabbled in Ubuntu Debian and Fedora/RHEL/CentOS on both cloud and bare metal machines- I'm just trying to learn more about this laptop that costs more than my car does right now:) Thanks for your time ahead of it.我是 Mac 新手,在云和裸机机器上都涉足了 Ubuntu Debian 和 Fedora/RHEL/CentOS - 我只是想了解更多关于这台笔记本电脑的信息,它的成本比我现在的车还高:)谢谢你时间提前。

Feedback, per concern:反馈,每个问题:

  1. Is there more efficient ways to handle a simple task to speed up workflow like the above?是否有更有效的方法来处理简单的任务以加快上述工作流程?

The script is simple.脚本很简单。 Consider testing if the file already exists before touch , if there are any side effect from bumping the timestamp, or executing other one time commands.考虑在touch之前测试该文件是否已经存在,是否存在碰撞时间戳或执行其他一次性命令的副作用。 [ -f $1/readme.md ] && return

  1. When sourcing the file would it be best just to source the individual script...在获取文件时,最好只获取单个脚本...

Both options are reasonable, and bash will handle both in efficient way.这两个选项都是合理的,bash 将以有效的方式处理这两个选项。 Setting up a shared folder which will be included in the 'PATH' make it easier to make changes, add scripts, etc. I think it's more efficient (from management point of view), vs. sourcing individual files during bash startups.设置将包含在“PATH”中的共享文件夹可以更轻松地进行更改、添加脚本等。我认为它更有效(从管理的角度来看),而不是在 bash 启动期间采购单个文件。

  1. I am sourcing the file currently in the.bash_profile with source./.mk_readme.sh.我正在使用 source./.mk_readme.sh 获取当前位于 .bash_profile 中的文件。

While adding a single function definition to each bash startup is small, this will usually not scale well.虽然向每个 bash 启动添加单个 function 定义很小,但这通常不会很好地扩展。 On surface, this function will be used sporadically (one time per folder).从表面上看,这个 function 将被偶尔使用(每个文件夹一次)。 I suggest adding reference to the script.我建议添加对脚本的引用。 If it's only needed for interactive sessions, you can use aliases in '~/.bashrc'.如果只需要交互式会话,您可以在 '~/.bashrc' 中使用别名。 Very tempting to fall into slippery slope, where additional 3 letter commands will show up.很容易掉入滑坡,会出现额外的 3 个字母命令。

  1. Can anyone give me a quick ironing out on the best way to handle the谁能给我一个快速熨烫的最佳方法来处理

Unfortunately, I do not have experience with running bash scripts from VS.不幸的是,我没有从 VS 运行 bash 脚本的经验。 The below is based google/stack overflow:以下是基于谷歌/堆栈溢出:

* Alternative, instead of running window 'pause', use 'read', to wait for standard input.
* Modify the launcher to pass env var "EXIT_PAUSE=1", add '[ "$PAUSE" ] || trap 'echo 'echo "exit $?" ; pause' EXIT' to the top your script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM