【问题标题】:How to run a series of commands with a single command in the command line?如何在命令行中使用单个命令运行一系列命令?
【发布时间】:2012-01-18 12:49:53
【问题描述】:

我通常运行以下命令来部署特定应用:

compass compile -e production --force
git add .
git commit -m "Some message"
git push
git push production master

我怎样才能把它包装成一个命令?

我需要能够自定义提交消息。所以命令可能看起来像:

deploy -m "Some message"

【问题讨论】:

    标签: linux bash shell command-line


    【解决方案1】:

    有两种可能:

    • 一个脚本,正如其他人回答的那样

    • 一个函数,在你的 .bash_profile 中定义:

      deploy() {
          compass compile -e production --force &&
          git add . &&
          git commit -m "$@" &&
          git push &&
          git push production master
      }
      

    没有参数,你会有第三种选择,即别名:

    alias deploy="compass compile -e production --force &&
                  git add . &&
                  git commit -m 'Dumb message' &&
                  git push &&
                  git push production master"
    

    【讨论】:

    • 如果前面的任何命令失败,你真的需要中止。全部替换 ';'使用 '&&',并将 '&&' 放在每一行的末尾,但在函数定义中的最后一行。
    • '&&'后面不需要换行符。
    • 如何将参数传递给这个?我正在尝试deploy "Some Example",它正在退回error: pathspec 'Example' did not match any file(s) known to git.
    • @Shpigford,这是因为函数定义中的参数没有正确转义。请参阅我的答案,了解如何将它们传递给git commit(简而言之:在函数中使用“$@”并将其用作`deploy -m“Some Example”)。
    • @spatz:我更改了$@,并重新加载了 .bash_profile 文件,但仍然遇到同样的路径规范错误。
    【解决方案2】:

    您可以创建一个执行您想要的功能的函数,并将提交消息作为参数传递:

    function deploy() {
        compass compile -e production --force
        git add .
        git commit "$@"
        git push
        git push production master
    }
    

    把它放在你的.bashrc 中就可以了。

    【讨论】:

      【解决方案3】:

      你可以制作一个shell脚本。看起来像这样的东西(注意没有输入验证等):

      #!/bin/sh
      compass compile -e production --force
      git add .
      git commit -m $1
      git push
      git push production master
      

      将其保存到myscript.shchmod +x,然后执行./myscript.sh "Some message" 之类的操作。

      【讨论】:

        【解决方案4】:

        你可以为此编写一个shell脚本

        #!/bin/bash
        compass compile -e production --force 
        git add . 
        git commit -m $1 
        git push 
        git push production master
        

        将其保存到“部署”并对其执行 chmod 7xx。现在您可以将其用作 ./deploy "Some message"

        【讨论】:

          【解决方案5】:

          您可以将这些命令写入名为 deploy.sh 的文件中。

          然后使其可执行并作为 sh deploy.sh 运行

          您甚至可以通过导出保存脚本的路径将其添加到您的路径中。

          【讨论】:

            【解决方案6】:

            每个人都提到要编写脚本,这可能是最好的方法。

            但是,您可能有一天会想使用另一种方式 - 将命令与 && 合并,例如:

            cd ../ && touch abc
            

            将在父目录中创建一个文件“abc”:)

            这只是为了让您了解此类事情,对于这种特殊情况(以及其他 99% 的情况),请查看其他答案 :)

            【讨论】:

              【解决方案7】:

              我会努力使命令不仅仅适用于当前目录。最通用的方法之一是在 BASH 脚本中使用 getopt。确保您已安装 getopt,创建 deploy.sh 然后创建 chmod 755 deploy.sh,然后执行以下操作:

              #!/bin/bash
              
              declare -r GETOPT=/usr/bin/getopt
              declare -r ECHO='builtin echo'
              declare -r COMPASS=/path/to/compass
              declare -r GIT=/path/to/git
              
              
              sanity() {
                  # Sanity check our runtime environment to make sure all needed apps are there.
                  for bin in $GETOPT $ECHO $COMPASS $GIT
                  do
                      if [ ! -x $bin ]
                      then
                          log error "Cannot find binary $bin"
                          return 1
                      fi
                  done
              
                  return 0
              }
              
              usage() {
              $CAT <<! 
              
              ${SCRIPTNAME}:  Compile, add and commit directories
              
              Usage: ${SCRIPTNAME} -e <env> [-v]
                  -p|--path=<path to add>
                  -c|--comment="Comment to add"
                  -e|--environment=<production|staging|dev>
              
              Example:
                  $SCRIPTNAME -p /opt/test/env -c "This is the comment" -e production
              
              !
              }
              
              
              
              checkopt() {
              
              # Since getopt is used within this function, it must be called as
              
              # checkopt "$@"
                  local SHORTOPT="-hp::c::e::"
                  local LONGOPT="help,path::,comment::,environment::"
              
              eval set -- "`$GETOPT -u -o $SHORTOPT --long $LONGOPT -n $SCRIPTNAME -- $@`"
                  while true
                  do
                      case "$1" in
                      -h|--help)
                          return 1
                          ;;
                      -|--path)
                          PATH="$2"
                          shift 2
                          ;;
                      -c|--comment)
                          COMMENT=$2
                          shift 2
                          ;;
                      -e|--environment)
                          ENV="$2"
                          shift 2
                          ;;
                      --)
                          shift
                          break
                          ;;
                      *)
                          $ECHO "what is $1?"
                          ;;
                      esac
                  done
              }
              
              
              if ! sanity
              then
                  die "Sanity check failed - Cant find proper system binaries"
              fi
              
              
              if checkopt $@
              then
                  $ECHO "Running Compass Compile & Git commit sequence..."
                  $COMPASS compile -e $ENV --force
                  $GIT add $PATH
                  $GIT commit -m $COMMENT
                  $GIT push
                  $GIT push ENV master
              
              else
                  usage
                  exit 1
              fi
              
              exit 0
              

              【讨论】:

              • 您应该知道getopt have issues 的某些版本。
              • 请记住这一点。这是一篇很好的文章,解释了要远离哪些版本。
              猜你喜欢
              • 2022-12-11
              • 2020-10-13
              • 2014-06-07
              • 2022-01-24
              • 2012-04-10
              • 2020-07-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多