在多台计算机上保持shell配置同步

mus*_*pax 10 shell synchronization development-environment dotfiles

我是一个非常活跃的命令行用户,我到处都有shell帐户.MacBooks,Linux台式机,Linux服务器,XP上的Cygwin,你可以说出来.

我怎样才能让我的壳结构(.bashrc,.vimrc等)同步在所有这些机器使用跨所有平台上提供有限的工具?

我一直rsync在以非常有限的方式使用,涉及在我需要时手动复制一些文件,但我想要一种标准的方法来在我的所有机器上设置相同的基本shell环境.告诉我你的shell帐户管理策略.

mus*_*pax 14

我在Dropbox上有全局,每个操作系统和每个机器外壳配置的文件夹:

$ ls ~/Dropbox/shell/bash
bashbootstrap  bashrc
bashrc-Darwin  bashrc-Darwin-laptopname  bashrc-Darwin-mininame
bashrc-Linux  bashrc-Linux-machineone  bashrc-Linux-machinetwo
Run Code Online (Sandbox Code Playgroud)

bashrc在每台机器上加载bashrc-Linux,bashrc-Darwin加载到各自的操作系统上,并且几个配置特定于各个机器.(顺便说一下,Darwin是OS X的BSD类内核的名称.)

把它们联系在一起的是bashbootstrap文件.它按照增加的特性加载每个适用的配置文件,这允许每个操作系统和每个机器覆盖具有更高的优先级.另外,我们默默地跳过丢失的配置文件; 您无需为每台计算机创建空配置文件以保持脚本满意.

在一台新机器上,在安装Dropbox之后~/Dropbox,我移开了默认设置.bashrc,只是将引导程序文件符号链接到它的位置:

$ mv ~/.bashrc ~/.bashrc.bak
$ ln -s ~/Dropbox/shell/bash/bashbootstrap  ~/.bashrc
Run Code Online (Sandbox Code Playgroud)

哦,这是bashbootstrap文件的内容:

if [ -z "$PS1" ]; then
   return
fi

dropboxshelldir=~/Dropbox/shell
dropboxdir=$dropboxshelldir/bash
masterbashrc=$dropboxdir/bashrc
osbashrc=$masterbashrc-`uname`
localbashrc=$osbashrc-`hostname | cut -d. -f1`

echo -n "Applicable shell configs: "
for bashfile in "$masterbashrc" "$osbashrc" "$localbashrc"; do
  if [ -r $bashfile ]; then
    . $bashfile
    echo -n "`basename $bashfile` "
  fi
done
echo

# Set convenience aliases
myed=${VISUAL:-${EDITOR:-vim}}
alias editbashrc="$myed $masterbashrc"
alias editosbashrc="$myed $osbashrc"
alias editlocalbashrc="$myed $localbashrc"
Run Code Online (Sandbox Code Playgroud)

最后要注意的是,此脚本还提供了三个便利别名,用于编辑Bash配置文件,而无需记住它们的存储位置.

  • editbashrc:编辑全局配置文件.
  • editosbashrc:编辑特定于操作系统的配置文件.
  • editlocalbashrc:编辑计算机专用配置文件.

我只在Bash上测试过这个,但它可以在其他Bash上运行.但是,正如他们所说,您的里程可能会有所不同.

我在这里发了一篇博文.