2016-06-15 98 views
0

試圖編寫一個腳本來安裝Mongo,如果它不存在,至少在Ubuntu,Red Hat Linux和Mac OSX上。命令行安裝Mongo,無論操作系統是什麼

這裏的想法:

#!/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  
+0

您可以調用'os_version'函數獲取操作系統名稱和版本 - 請參閱https://github.com/elifarley/shell-lib/blob/master/lib/base.sh – Elifarley

+0

謝謝,我最終這樣做,然後選擇從這些信息建立Conda並更容易地安裝軟件包... – legel

回答

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 
+0

'mkdir:無法創建目錄'/ usr/local/cross-installer':權限被拒絕(即使使用sudo) – legel

+0

我修復了一些bug並在Debian和CentOS上測試過。也許你想重試,如果這不起作用,請通過https://github.com/elifarley/cross-installer/issues報告問題,以避免混亂(請告訴哪個操作系統不起作用)。 – Elifarley

0

最終我選擇了先檢測OS和安裝合適的C​​onda發行版,然後根據需要使用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 
} 

當然,用戶可以更改蒙戈並安裝Conda覆蓋任何依賴,它覆蓋了700+包...

FYI這是對words2map庫更大的安裝序列的一部分,所以任何更新都會反映在那裏。