如何在命令行中使用单个命令运行一系列命令?

发布于 2024-12-27 15:56:43 字数 271 浏览 0 评论 0原文

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

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

如何将其包装到单个命令中?

我需要能够自定义提交消息。因此该命令可能类似于:

deploy -m "Some message"

I typically run the following commands to deploy a particular app:

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

How can I wrap that up into a single command?

I'd need to be able to customize the commit message. So the command might look something like:

deploy -m "Some message"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

金橙橙 2025-01-03 15:56:43

有两种可能性:

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

  • 函数,在您的 .bash_profile 中定义:

    部署() {
        指南针编译-e生产--force &&
        git 添加 . &&
        git commit -m "$@" && git commit -m "$@" &&
        git 推送 &&
        git推送生产大师
    }
    

如果没有参数,您将有第三个选项,即别名:

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

There are two possibilities:

  • a script, as others answered

  • a function, defined in your .bash_profile:

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

Without arguments, you'd have a third option, namely an alias:

alias deploy="compass compile -e production --force &&
              git add . &&
              git commit -m 'Dumb message' &&
              git push &&
              git push production master"
江南月 2025-01-03 15:56:43

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

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

将其放入您的 .bashrc 中,然后就可以开始了。

You could create a function that does what you want, and pass the commit message as argument:

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

Put that in your .bashrc and you're good to go.

从此见与不见 2025-01-03 15:56:43

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

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

将其保存到 myscript.sh,chmod +x 它,然后执行类似 ./myscript 的操作。 sh“一些消息”

You can make a shell script. Something that looks like this (note no input validation etc):

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

Save that to myscript.sh, chmod +x it, then do something like ./myscript.sh "Some message".

丑丑阿 2025-01-03 15:56:43

您可以为此编写一个 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”

You can write a shell script for this

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

Save this to 'deploy' and do a chmod 7xx on it. Now you can use it as ./deploy "Some message"

春夜浅 2025-01-03 15:56:43

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

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

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

you could write these commands into a file named deploy.sh .

Then make it executable and run as sh deploy.sh

You could even add it to your path by exporting the path where you save the script.

风铃鹿 2025-01-03 15:56:43

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

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

cd ../ && touch abc

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

这只是为了让您了解这样的事情,对于这个特定的场景(和 99% 的其他人)请看看其他答案:)

everyone mentions about writing a script and this is probably the best way of doing it.

However you might someday want to use another way - merge commands with &&, for example:

cd ../ && touch abc

will create a file "abc" in a parent directory :)

It is just to let you know about such thing, for this particular scenario (and 99% of the others) please take a look at other answers :)

甚是思念 2025-01-03 15:56:43

我会努力使该命令不仅仅适用于当前目录。最通用的方法之一是在 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

I would go through the effort of making the command work for more than just the current directory. One of the most versitle ways of doing this is to use getopt in a BASH script. Make sure you have getopt installed, create deploy.sh then chmod 755 deploy.sh and then do something like this:

#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文