命令行安装Mongo,无论操作系统如何

时间:2016-06-15 19:07:58

标签: bash mongodb shell scripting operating-system

尝试编写将安装Mongo的脚本(如果它不存在),至少在Ubuntu,Red Hat Linux和Mac OSX上。

以下是这个想法:

  #!/bin/bash
  if [$mongoDoesntExist]; then
    if [ $myOS = "Ubuntu" ]; then
      # install Mongo on Ubuntu
    else if [ $myOS = "Red Hat Linux"]; then
      # install Mongo on Red Hat Linux
    else if [ $myOS = "Mac OSX"]; then
      # install mongo on Mac OSX
    else
      echo "Please visit https://docs.mongodb.com/v3.0/installation/ to install Mongo on your OS"
    fi
  fi    

2 个答案:

答案 0 :(得分:0)

尝试cross-installer,其目的是能够以统一的方式安装软件,无论操作系统如何:

# Install cross-installer
curl -fsSL https://raw.githubusercontent.com/elifarley/cross-installer/master/install.sh | sudo sh

# Install  MongoDB
sudo xinstall add mongodb

答案 1 :(得分:0)

最终我选择首先检测操作系统并安装相应的Conda发行版,然后根据需要轻松安装Conda的依赖项,例如

download_miniconda() {
    echo "Downloading Miniconda for Python dependencies..."
    OS_BIT_TYPE="$(uname -m)"
    OS_ARCHITECTURE="$(uname -s)"
    if [ $OS_BIT_TYPE == "i686" ]; then
        OS_BIT_TYPE="x86"
    fi
    if [ $OS_ARCHITECTURE == "Darwin" ]; then
        OS_ARCHITECTURE="MacOSX"
    fi

    MINICONDA_INSTALL_FILE="Miniconda2-latest-$OS_ARCHITECTURE-$OS_BIT_TYPE.sh"
    MINICONDA_DOWNLOAD_URL="https://repo.continuum.io/miniconda/$MINICONDA_INSTALL_FILE"
    $(curl -O $MINICONDA_DOWNLOAD_URL)
    $(chmod +x $MINICONDA_INSTALL_FILE)
}

install_miniconda() {
    echo "Installing Miniconda..."
    echo "$(./$MINICONDA_INSTALL_FILE -b -p $HOME/miniconda)"
    echo "$(rm $MINICONDA_INSTALL_FILE)"    
}

confirm_miniconda_installed() {
    if hash conda 2>/dev/null; then
        echo "Miniconda installed!"
    else
        echo "Failed to install Miniconda. Please visit http://conda.pydata.org/docs/install/quick.html to install and then try rerunning this script, making sure that Miniconda is accessible in the PATH"
    fi
}

update_script_startup_file() {
    echo "if [[ \":\$PATH:\" != *\":\$HOME/miniconda/bin:\"* ]]; then" >> $STARTUP_FILE
    echo "  export PATH=\"\$PATH:\$HOME/miniconda/bin\"" >> $STARTUP_FILE
    echo "fi" >> $STARTUP_FILE
}

add_miniconda_to_path() {
    # temporary update to PATH for this script
    export PATH="$PATH:$HOME/miniconda/bin"

    # permanent update to PATH for user's convenience
    if [ -n "`$SHELL -c 'echo $BASH_VERSION'`" ]; then
        STARTUP_FILE="$HOME/.bashrc"
        update_script_startup_file
    elif [ -n "`$SHELL -c 'echo $ZSH_VERSION'`" ]; then
        STARTUP_FILE="$HOME/.zshrc"
        update_script_startup_file
    else
        echo "Couldn't automatically add Miniconda to the PATH of your preferred terminal. We suggest working from Bash or ZShell." 
    fi
}

create_conda_environment() {
    if hash conda 2>/dev/null; then
        CONDA_ENVIRONMENTS="$(conda env list)"
        if [[ "$CONDA_ENVIRONMENTS" != *"words2map"* ]]; then
            conda create --name my_mongo_environment --yes mongo
        fi
    fi
}

当然,用户可以更改mongo并安装Conda涵盖的任何依赖项,其中包含超过700个包...

仅供参考,这是words2map库的更大安装顺序的一部分,因此任何更新都将反映在那里。