Shpigford
Shpigford

Reputation: 25328

How to run a series of commands with a single command in the command line?

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"

Upvotes: 2

Views: 4754

Answers (7)

mouviciel
mouviciel

Reputation: 67831

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"

Upvotes: 5

drrlvn
drrlvn

Reputation: 8437

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.

Upvotes: 2

Drahkar
Drahkar

Reputation: 1671

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

Upvotes: 0

mkk
mkk

Reputation: 7693

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 :)

Upvotes: 0

ConfusedAboutCPP
ConfusedAboutCPP

Reputation: 603

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.

Upvotes: 0

cha0site
cha0site

Reputation: 10717

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

Upvotes: 1

Daan Pape
Daan Pape

Reputation: 1140

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"

Upvotes: 0

Related Questions