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

时间:2012-01-18 12:49:54

标签: linux bash shell command-line

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

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

如何将其整理成一个命令?

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

deploy -m "Some message"

7 个答案:

答案 0 :(得分:5)

有两种可能性:

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

  • 在.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"

答案 1 :(得分:2)

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

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

将它放在.bashrc中,你就可以了。

答案 2 :(得分:1)

您可以制作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"之类的操作。

答案 3 :(得分:0)

您可以为此

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

将此保存到'deploy'并在其上执行chmod 7xx。现在您可以将它用作./deploy“Some message”

答案 4 :(得分:0)

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

然后使其成为可执行文件并以sh deploy.sh

运行

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

答案 5 :(得分:0)

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

但是有一天你可能会想用另一种方式 - 用&&amp ;,合并命令,例如:

cd ../ && touch abc

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

只是让你知道这个事情,对于这个特殊情况(以及99%的其他人),请看看其他答案:)

答案 6 :(得分:0)

我会努力使命令不仅仅是当前目录。最常用的方法之一是在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
相关问题