0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Linux VMで開発の際のaliasやブランチ設定の追加を自動化してみた

Posted at

最近VMで開発することが多くなっているのだが、その際のエイリアスなど毎回同じものを追加しているので自動化してみた。

以下のことをしたい

  • gitコマンドのエイリアス追加
  • ブランチ表示

そして上記のコマンドがすでにファイルに記載されていたら再度追加するのを防いでおきたい。
それは grep -qxF "${stmt}" filename でできるらしい(以下参照)。

シェルスクリプトは以下

alias.sh
#! /bin/sh

# useful aliases when development in VM

HOMEDIR="/home/vagrant"

touch $HOMEDIR/.bash_aliases

stmt='alias gst="git status"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gp="git push"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gcmsg="git commit -S -m"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gup="git pull"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias ga="git add"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gco="git checkout"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gcb="git checkout -b"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gm="git merge"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases
stmt='alias gcm="git checkout main"'
grep -qxF "${stmt}" $HOMEDIR/.bash_aliases || echo "${stmt}" >> $HOMEDIR/.bash_aliases

stmt="
if [ -f $HOMEDIR/.bash_aliases ]; then
    $HOMEDIR/.bash_aliases
fi"

grep -qxF "${stmt}" $HOMEDIR/.bashrc || echo "${stmt}" >> $HOMEDIR/.bashrc

stmt="
git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}"

grep -qxF "${stmt}" $HOMEDIR/.bashrc || echo "${stmt}" >> $HOMEDIR/.bashrc

stmt='export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \$(git_branch)\$ "'
grep -qxF "${stmt}" $HOMEDIR/.bashrc || echo "${stmt}" >> $HOMEDIR/.bashrc



VagrantでVMを立ち上げているので、このシェルスクリプトを走らせるように追加しておく。

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
    config.vm.define "vm-build" do |v|
    end
  
    config.vm.box = "bento/ubuntu-18.04"
  
    config.vm.synced_folder "./", "/home/vagrant/myfolder"
  
    config.vm.provision "shell", inline: <<-SHELL
      apt-get update
      apt-get install -y nasm make xorriso binutils gcc automake g++
      sudo ./alias.sh
    SHELL
  end

これでvagrant upでgit周辺のコマンドも追加できる!
相対パスが使えなかったので/home/vagrantを指定しないといけないのはめんどうだが

あとはssh, gpg鍵の自動化もできたらいいのだが、、、

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?