1. 程式人生 > >Linux開發環境配置及shell script

Linux開發環境配置及shell script

本文主要是以快速搭建環境為目標學習shell script。
轉自https://hceng.cn/

之前寫過一個Linux嵌入式開發環境搭建的部落格,後面每次搭環境都翻來複制上面的程式碼。感覺就像記事本一樣,還是有點用,這也是寫部落格的一點動力吧。
用了Linux也有段時間了,它的魅力也逐漸展現出來了。以前還在熟悉Linux命令的過程中,shell script就是個老虎,覺得沒接觸過,害怕,每次都繞開它。
直到這周遇到了ti的SDK,嘗試一邊百度,一邊測它的指令碼,感覺有點小入門了,於是想練習下。想到每次搭建環境重複操作的痛點,就拿它開刀吧。

0.環境搭建的幾個方面

根據過去的經驗,用虛擬機器裝好Ubuntu後,一般會做這樣幾個步驟:
1.安裝vmware tools,以便複製貼上和檔案共享;
2.更新軟體源,以便更快的下載軟體;
3.安裝及配置常用軟體,比如:
  3-1.安裝git,方面後續的一些安裝;
  3-2.安裝vim和簡單配置;
  3-3.安裝ftp和簡單配置;
  3-4.安裝ftp和簡單配置;
  3-5.安裝samba和簡單配置;
  3-6.安裝tmux、htop等;
4.安裝開發所需的 g++等工具、庫;
除了第一步,其它都計劃用指令碼實現。

厭煩了每次編輯了驅動都要拖進Linux主機進行編譯,然後還得複製到開發板。
重新打造了工作流,只需三步:
編輯->make->insmod

1.檢測當前環境

  • 在執行指令碼前,需要檢測一些東西:

1.1檢測網路狀態

  • 如果不能聯網,後續的沒必要做了。之前想過自動檢測修復網路的,但想了想,網路的情況比較多,暫時擱置。而且剛裝好虛擬機器肯定是聯網的,如果不能聯網肯定是虛擬機器設定的問題,也不是指令碼能解決的。
    為了方便列印更改使用者名稱,這裡先設定幾個變數:
#define echo print color.
RED_COLOR='\E[1;31m'        
PINK_COLOR='\E[1;35m'
YELOW_COLOR='\E[1;33m' BLUE_COLOR='\E[1;34m' GREEN_COLOR='\E[1;32m' END_COLOR='\E[0m' #set linux host user name. user_name=hceng
  • 檢測網路函式:
check_network() {
    ping -c 1 www.baidu.com > /dev/null 2>&1  
    if [ $? -eq 0 ];then  
        echo -e "${GREEN_COLOR}
Network ok.${END_COLOR}"
else echo -e "${RED_COLOR}Network failure!${END_COLOR}" exit 1 fi }
  • 先ping一次百度,能夠ping通就表示網路沒問題。

1.2檢測是否是root使用者

  • 很多操作都需要root許可權,因此必須使用root使用者許可權執行指令碼,這裡先檢測是否是root使用者:
# Check user must root.
check_root() {
    if [ $(id -u) != "0" ]; then
        echo -e "${RED_COLOR}Error: You must be root to run this script, please use root.${END_COLOR}"
        exit 1
    fi
}

1.3檢測設定的使用者是否存在

  • 現在用的使用者名稱是hceng,後面為了方便其它使用者使用,所以在前面定義了個變數,改變變數就可以修改使用者。但為了防止設定的使用者在系統中不存在,這裡需要進行檢測。
# Check set linux host user name.
check_user_name() {
    cat /etc/passwd|grep $user_name
    if [ $? -eq 0 ];then
        echo -e "${GREEN_COLOR}Check the set user name OK.${END_COLOR}" 
    else
        echo -e "${RED_COLOR}Check the set user name failure!${END_COLOR}" 
        exit 1
    fi
}

1.4檢測執行結果

  • 為檢測某些命令,是否執行正常。
check_status() {
    ret=$?
    if [ "$ret" -ne "0" ]; then
        echo -e "${RED_COLOR}Failed setup, aborting..${END_COLOR}" 
        exit 1
    fi
}

1.5獲取系統版本資訊

  • 後面更新源和裝某些軟體需要當前Ubuntu的版本代號。
# Get the code name of the Linux host release to the caller.
get_host_type() {
    local  __host_type=$1
    local  the_host=`lsb_release -a 2>/dev/null | grep Codename: | awk {'print $2'}`
    eval $__host_type="'$the_host'"
}

2.更新軟體源

  • 這裡使用了阿里、網易和官方的三個源,應該沒問題了。
    修改配置檔案的原則就是先備份再修改。同時為了防止指令碼再次執行覆蓋掉備份,還需要檢測是否已經存在了備份。
#This function will update the source of the software.
update_software_source() {
    local back_file=/etc/apt/sources.list.backup
    if [ ! -e "$back_file" ];then  
        cp /etc/apt/sources.list $back_file         
    fi 
    check_status

    get_host_type host_release
    check_status

    echo \
    "#Ali source.
    deb-src http://archive.ubuntu.com/ubuntu    $host_release main restricted 
    deb http://mirrors.aliyun.com/ubuntu/       $host_release main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release main restricted multiverse universe 
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-updates main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release-updates main restricted multiverse universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-updates universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release multiverse
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-updates multiverse
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release-backports main restricted universe multiverse 
    deb http://archive.canonical.com/ubuntu     $host_release partner
    deb-src http://archive.canonical.com/ubuntu $host_release partner
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-security main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release-security main restricted multiverse universe 
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-security universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-security multiverse

    #Netease source.
    deb http://mirrors.163.com/ubuntu/          $host_release main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-security main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-updates main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-proposed main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-backports main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-security main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-updates main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-proposed main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-backports main restricted universe multiverse

    #Official source                            
    deb http://archive.ubuntu.com/ubuntu/       $host_release main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-security main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-updates main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-proposed main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-backports main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-security main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-updates main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-proposed main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-backports main restricted universe multiverse" \
    > /etc/apt/sources.list
    check_status

    #apt-get update 1>/dev/null
    apt-get update
    check_status    

    echo -e "${GREEN_COLOR}Update source completed.${END_COLOR}" 
}

3.安裝軟體及配置

  • 軟體主要包括自己一般常用的軟體,像vim、tmux、samba等。還有就是開發所需的g++、各種庫。
    其中git要在vim前面,因為vim需要git下載。
# Execute an action.
FA_DoExec() {
    echo -e "${BLUE_COLOR}==> Executing: '${@}'.${END_COLOR}"
    eval [email protected] || exit $?
}

# Install list software.
install_software() {
    local install_software_list=("git" "vim" "tmux" "htop" "vsftpd" "openssh-server" "nfs-kernel-server" "portmap" "samba")
    echo -e "${PINK_COLOR}install_software_list:${install_software_list[*]}.${END_COLOR}" 

    #install git
    if (echo "${install_software_list[@]}" | grep -wq "git");then
        apt-get -y install git && echo -e "${BLUE_COLOR}git install completed.${END_COLOR}" 
    fi

    #install and configure vim
    if (echo "${install_software_list[@]}" | grep -wq "vim");then
        apt-get -y install vim && vim_configure && echo -e "${BLUE_COLOR}vim install completed.${END_COLOR}" 
    fi

    #install tmux
    if (echo "${install_software_list[@]}" | grep -wq "tmux");then
        apt-get -y install tmux && echo -e "${BLUE_COLOR}tmux install completed.${END_COLOR}" 
    fi

    #install htop
    if (echo "${install_software_list[@]}" | grep -wq "htop");then
        apt-get -y install htop && echo -e "${BLUE_COLOR}htop install completed.${END_COLOR}" 
    fi

    #install and configure vsftpd
    if (echo "${install_software_list[@]}" | grep -wq "vsftpd");then
        apt-get -y install vsftpd && ftp_configure && echo -e "${BLUE_COLOR}vsftpd install completed.${END_COLOR}" 
    fi

    #install openssh-server
    if (echo "${install_software_list[@]}" | grep -wq "openssh-server");then
        apt-get -y install openssh-server && echo -e "${BLUE_COLOR}openssh-server install completed.${END_COLOR}" 
    fi

    #install and configure nfs-kernel-server
    if (echo "${install_software_list[@]}" | grep -wq "nfs-kernel-server");then
        apt-get -y install nfs-kernel-server && nfs_configure && \
        /etc/init.d/nfs-kernel-server restart && echo -e "${BLUE_COLOR}nfs-kernel-server install completed.${END_COLOR}" 
    fi

    #install portmap
    if (echo "${install_software_list[@]}" | grep -wq "portmap");then
        apt-get -y install portmap && echo -e "${BLUE_COLOR}portmap install completed.${END_COLOR}" 
    fi

    #install and configure samba
    if (echo "${install_software_list[@]}" | grep -wq "samba");then
        apt-get -y install samba && samba_configure && echo -e "${BLUE_COLOR}samba install completed.${END_COLOR}" 
    fi


    #others
    get_host_type host_release

    FA_DoExec apt-get -y install \
    gnupg flex bison gperf build-essential \
    zip curl libc6-dev libncurses5-dev libncurses5-dev:i386 x11proto-core-dev \
    libx11-dev:i386 libreadline6-dev:i386 \
    libgl1-mesa-glx-lts-$host_release:i386 libgl1-mesa-dev-lts-$host_release \
    g++-multilib mingw32 tofrodos libncurses5-dev:i386 \
    python-markdown libxml2-utils xsltproc zlib1g-dev:i386


    if [ ! -h /usr/lib/i386-linux-gnu/libGL.so ]; then
    FA_DoExec ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 \
        /usr/lib/i386-linux-gnu/libGL.so
    fi

    # Development support
    FA_DoExec apt-get -y install \
    dos2unix minicom gawk

    echo -e "${GREEN_COLOR}software install completed.${END_COLOR}"
}

3.1配置vim

  • vim的配置檔案來自GitHub的Amir,我就不重複造輪子了。
    這裡只是先clone下來,然後執行安裝指令碼。這裡值得一提的是執行指令碼要指定使用者執行,不然會以root使用者的路徑設定,導致指令碼執行錯誤。
# Configure vim form github.
vim_configure() {
    git clone --depth=1 https://github.com/amix/vimrc.git /home/$user_name/.vim_runtime 

    touch /home/$user_name/.vim_runtime/my_configs.vim
    echo ":set number" > /home/$user_name/.vim_runtime/my_configs.vim

    chown -R $user_name /home/$user_name/.vim_runtime 
    chmod u+x /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
    su - $user_name -s /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
}

3.2配置ftp

  • FTP主要是修改為可寫。在使用MobaXterm SSH登陸後,可以直接通過左邊的Sftp進行檔案的傳輸,貌似這個可以不用配置了。
# Configure ftp.
ftp_configure() {
    sed  -i 's/#loacl_enable=YES/loacl_enable=YES/g'  /etc/vsftpd.conf
    sed  -i 's/#write_enable=YES/write_enable=YES/g'  /etc/vsftpd.conf
}

3.3配置nfs

  • 在前面的工作流中,開發板直接執行Linux主機中交叉編譯好的模組,是通過nfs實現的。因此需要開發板開機後就掛載nfs.
    習慣在Linux主機中單獨開闢一個路徑作為工作目錄,因此這裡是設定的整個工作目錄。
# Configure nfs.
nfs_configure() {
    local work_file=/work
    if [ ! -d "$work_file" ];then  
        mkdir /work
    fi
    check_status

    grep "/work" /etc/exports 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i '$a\/work  *(rw,sync,no_root_squash,no_subtree_check)' /etc/exports
    fi
}

3.4配置samba

  • samba也是實現前面工作流不可確少的一環。這樣就不用每次修改程式碼後,通過ftp上傳到Linux主機。
    samba在Windows上的使用是:
    Windows+r,開啟命令視窗,
    然後輸入:\\192.168.1.xx
    最後建議右鍵,對映網路驅動,方面後續開啟。
    測試中需要重啟生效。
# Configure samba.
samba_configure() {
    local back_file=/etc/samba/smb.conf.bakup
    if [ ! -e "$back_file" ];then  
        cp /etc/samba/smb.conf $back_file       
    fi 
    check_status

    grep "/work" /etc/samba/smb.conf 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i \
        '$a[share_work]\n\
        path = \/work\n\
        available = yes\n\
        public = yes\n\
        guest ok = yes\n\
        read only = no\n\
        writeable = yes\n' /etc/samba/smb.conf
    fi

    /etc/init.d/samba restart
    chmod -R 777 /work
}

4.完整程式碼及心得

#!/bin/bash

# -------------------------------------------------------------------------------
# Filename:    setup_ubuntu_host_env.sh
# Revision:    1.0
# Date:        2017/08/05
# Author:      hceng
# Email:       [email protected]
# Website:     www.hceng.cn
# Function:    setup ubuntu host env.
# Notes:       learn
# -------------------------------------------------------------------------------
#
# Description:     
#1.check env.
#1.1 check network  
#1.2 check use root  
#1.3 check set name  
#1.4 configure samba   
#2.update software sourcev.  
#3.install vim tmux  htop ftp ssh nfs samba.
#3.1 configure vim  
#3.2 configure ftp  
#3.3 configure nfs  
#3.4 configure samba 
#4.install system tool eg:g++ ...
#
# -------------------------------------------------------------------------------


#define echo print color.
RED_COLOR='\E[1;31m'        
PINK_COLOR='\E[1;35m'       
YELOW_COLOR='\E[1;33m'       
BLUE_COLOR='\E[1;34m'       
GREEN_COLOR='\E[1;32m'      
END_COLOR='\E[0m'           

#Set linux host user name.
user_name=hceng

# Check network.
check_network() {
    ping -c 1 www.baidu.com > /dev/null 2>&1  
    if [ $? -eq 0 ];then  
        echo -e "${GREEN_COLOR}Network OK.${END_COLOR}" 
    else
        echo -e "${RED_COLOR}Network failure!${END_COLOR}"  
        exit 1
    fi
}

# Check user must root.
check_root() {
    if [ $(id -u) != "0" ]; then
        echo -e "${RED_COLOR}Error: You must be root to run this script, please use root.${END_COLOR}"
        exit 1
    fi
}

# Check set linux host user name.
check_user_name() {
    cat /etc/passwd|grep $user_name
    if [ $? -eq 0 ];then
        echo -e "${GREEN_COLOR}Check the set user name OK.${END_COLOR}" 
    else
        echo -e "${RED_COLOR}Check the set user name failure!${END_COLOR}" 
        exit 1
    fi
}

# Check the results of the operation.
check_status() {
    ret=$?
    if [ "$ret" -ne "0" ]; then
        echo -e "${RED_COLOR}Failed setup, aborting..${END_COLOR}" 
        exit 1
    fi
}

# Get the code name of the Linux host release to the caller.
get_host_type() {
    local  __host_type=$1
    local  the_host=`lsb_release -a 2>/dev/null | grep Codename: | awk {'print $2'}`
    eval $__host_type="'$the_host'"
}


#This function will update the source of the software.
update_software_source() {
    local back_file=/etc/apt/sources.list.backup
    if [ ! -e "$back_file" ];then  
        cp /etc/apt/sources.list $back_file         
    fi 
    check_status

    get_host_type host_release
    check_status

    echo \
    "#Ali source.
    deb-src http://archive.ubuntu.com/ubuntu    $host_release main restricted 
    deb http://mirrors.aliyun.com/ubuntu/       $host_release main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release main restricted multiverse universe 
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-updates main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release-updates main restricted multiverse universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-updates universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release multiverse
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-updates multiverse
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release-backports main restricted universe multiverse 
    deb http://archive.canonical.com/ubuntu     $host_release partner
    deb-src http://archive.canonical.com/ubuntu $host_release partner
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-security main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/   $host_release-security main restricted multiverse universe 
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-security universe
    deb http://mirrors.aliyun.com/ubuntu/       $host_release-security multiverse

    #Netease source.
    deb http://mirrors.163.com/ubuntu/          $host_release main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-security main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-updates main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-proposed main restricted universe multiverse
    deb http://mirrors.163.com/ubuntu/          $host_release-backports main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-security main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-updates main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-proposed main restricted universe multiverse
    deb-src http://mirrors.163.com/ubuntu/      $host_release-backports main restricted universe multiverse

    #Official source                            
    deb http://archive.ubuntu.com/ubuntu/       $host_release main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-security main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-updates main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-proposed main restricted universe multiverse
    deb http://archive.ubuntu.com/ubuntu/       $host_release-backports main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-security main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-updates main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-proposed main restricted universe multiverse
    deb-src http://archive.ubuntu.com/ubuntu/   $host_release-backports main restricted universe multiverse" \
    > /etc/apt/sources.list
    check_status

    #apt-get update 1>/dev/null
    apt-get update
    check_status    

    echo -e "${GREEN_COLOR}Update source completed.${END_COLOR}" 
}


# Configure vim form github.
vim_configure() {
    git clone --depth=1 https://github.com/amix/vimrc.git /home/$user_name/.vim_runtime 

    touch /home/$user_name/.vim_runtime/my_configs.vim
    echo ":set number" > /home/$user_name/.vim_runtime/my_configs.vim

    chown -R $user_name /home/$user_name/.vim_runtime 
    chmod u+x /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
    su - $user_name -s /home/$user_name/.vim_runtime/install_awesome_vimrc.sh
}

# Configure ftp.
ftp_configure() {
    sed  -i 's/#loacl_enable=YES/loacl_enable=YES/g'  /etc/vsftpd.conf
    sed  -i 's/#write_enable=YES/write_enable=YES/g'  /etc/vsftpd.conf
}

# Configure nfs.
nfs_configure() {
    local work_file=/work
    if [ ! -d "$work_file" ];then  
        mkdir /work
    fi

    grep "/work" /etc/exports 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i '$a\/work  *(rw,sync,no_root_squash,no_subtree_check)' /etc/exports
    fi
}

# Configure samba.
samba_configure() {
    local back_file=/etc/samba/smb.conf.bakup
    if [ ! -e "$back_file" ];then  
        cp /etc/samba/smb.conf $back_file       
    fi 
    check_status

    grep "/work" /etc/samba/smb.conf 1>/dev/null
    if [ $? -ne 0 ];then
        sed -i \
        '$a[share_work]\n\
        path = \/work\n\
        available = yes\n\
        public = yes\n\
        guest ok = yes\n\
        read only = no\n\
        writeable = yes\n' /etc/samba/smb.conf
    fi

    /etc/init.d/samba restart
    chmod -R 777 /work
}

# Execute an action.
FA_DoExec() {
    echo -e "${BLUE_COLOR}==> Executing: '${@}'.${END_COLOR}"
    eval [email protected] || exit $?
}

# Install list software.
install_software() {
    local install_software_list=\
    ("git" "vim" "tmux" "htop" "vsftpd" "openssh-server" "nfs-kernel-server" "portmap" "samba")
    echo -e "${PINK_COLOR}install_software_list:${install_software_list[*]}.${END_COLOR}" 

    #install git
    if (echo "${install_software_list[@]}" | grep -wq "git");then
        apt-get -y install git && echo -e "${BLUE_COLOR}git install completed.${END_COLOR}" 
    fi

    #install and configure vim
    if (echo "${install_software_list[@]}" | grep -wq "vim");then
        apt-get -y install vim && vim_configure && echo -e "${BLUE_COLOR}vim install completed.${END_COLOR}" 
    fi

    #install tmux
    if (echo "${install_software_list[@]}" | grep -wq "tmux");then
        apt-get -y install tmux && echo -e "${BLUE_COLOR}tmux install completed.${END_COLOR}" 
    fi

    #install htop
    if (echo "${install_software_list[@]}" | grep -wq "htop");then
        apt-get -y install htop && echo -e "${BLUE_COLOR}htop install completed.${END_COLOR}" 
    fi

    #install and configure vsftpd
    if (echo "${install_software_list[@]}" | grep -wq "vsftpd");then
        apt-get -y install vsftpd && ftp_configure && echo -e "${BLUE_COLOR}vsftpd install completed.${END_COLOR}" 
    fi

    #install openssh-server
    if (echo "${install_software_list[@]}" | grep -wq "openssh-server");then
        apt-get -y install openssh-server && echo -e "${BLUE_COLOR}openssh-server install completed.${END_COLOR}" 
    fi

    #install and configure nfs-kernel-server
    if (echo "${install_software_list[@]}" | grep -wq "nfs-kernel-server");then
        apt-get -y install nfs-kernel-server && nfs_configure && \
        /etc/init.d/nfs-kernel-server restart && echo -e "${BLUE_COLOR}nfs-kernel-server install completed.${END_COLOR}" 
    fi

    #install portmap
    if (echo "${install_software_list[@]}" | grep -wq "portmap");then
        apt-get -y install portmap && echo -e "${BLUE_COLOR}portmap install completed.${END_COLOR}" 
    fi

    #install and configure samba
    if (echo "${install_software_list[@]}" | grep -wq "samba");then
        apt-get -y install samba && samba_configure && echo -e "${BLUE_COLOR}samba install completed.${END_COLOR}" 
    fi


    #others
    get_host_type host_release

    FA_DoExec apt-get -y install \
    gnupg flex bison gperf build-essential \
    zip curl libc6-dev libncurses5-dev libncurses5-dev:i386 x11proto-core-dev \
    libx11-dev:i386 libreadline6-dev:i386 \
    libgl1-mesa-glx-lts-$host_release:i386 libgl1-mesa-dev-lts-$host_release \
    g++-multilib mingw32 tofrodos libncurses5-dev:i386 \
    python-markdown libxml2-utils xsltproc zlib1g-dev:i386


    if [ ! -h /usr/lib/i386-linux-gnu/libGL.so ]; then
    FA_DoExec ln -s /usr/lib/i386-linux-gnu/mesa/libGL.so.1 \
        /usr/lib/i386-linux-gnu/libGL.so
    fi

    # Development support
    FA_DoExec apt-get -y install \
    dos2unix minicom gawk

    echo -e "${GREEN_COLOR}software install completed.${END_COLOR}"
}

check_network
check_root
check_user_name

update_software_source
install_software

echo -e "${GREEN_COLOR}===================================================${END_COLOR}" 
echo -e "${GREEN_COLOR}============setup ubuntu host env ok!==============${END_COLOR}" 
echo -e "${GREEN_COLOR}===================================================${END_COLOR}"

su $user_name

exit 0
  • 就目前來看,shell指令碼還不算太難。主要就是提取某個文字內容,然後做出判斷,對應執行。技巧性還是蠻多的,很有樂趣。
    遇到要實現某個功能,百度一下也基本有。除錯的時候,能夠打印出變數,或者提出區域性程式碼進行執行測試,還是很方便的。

相關推薦

Linux開發環境配置shell script

本文主要是以快速搭建環境為目標學習shell script。 轉自https://hceng.cn/ 之前寫過一個Linux嵌入式開發環境搭建的部落格,後面每次搭環境都翻來複制上面的程式碼。感覺就像記事本一樣,還是有點用,這也是寫部落格的一點動力吧。

Windows下GO的開發環境配置多工作區配置

mar .html fin 依賴 下載 系統變量 not found rap 最新版 本文主要內容如下幾點: 下載安裝GO 配置多個工作區,第一個默認放第三方包,其他的放項目代碼 包管理器godep的安裝使用 安裝過程中的一些坑(墻) vscode中使用go 1. 下載

Spark教程(3)-開發環境配置單詞計數

1.開發環境準備 安裝scala windows下安裝scala-2.11.8 jdk jdk版本為1.8 安裝eclipse-scala外掛 下載eclipse開發scala的外掛,也可以下載scala專用的eclipse版本. 建立mave

Python開發環境配置使用(更新0.1)

python開發環境配置 環境:Ubuntu16.04 (相關目錄請根據自己的設定進行調整) 1.pyenv安裝 pyenv用來管理我們的Python版,virtualenv 管理不同的環境,pyenv不能管理之前安裝的python版本 安裝 sudo apt-get i

AndroidStudio 開發環境配置安裝

一:jdk的下載與配置    jdk,我的理解就是對Java檔案進行編譯執行的一套東西,所以要在手機或模擬器上執行程式,必須配置jdk,jdk下載就不說了,百度    一下,你就知道,然後是配置環境變數:        1:變數名:JAVA_HOME,變數值:jdk的安裝路徑

MongoDB深入學習系列(一)---開發環境配置工具介紹

                現在隨著網際網路應用的不斷髮展,傳統的關係型強事務型資料庫在某些要求不高的環境,高強度訪問情況下並不佔優,所以今天我們來研究一下記憶體資料庫中的Mongodb,MongoDB是文件型資料庫,他屬於記憶體資料庫,但是他也可以持久化到磁碟,所以根

《從零開始搭建遊戲伺服器》Linux開發環境配置

前言: 之前的開發工作一直是在Windows開發環境下進行的,但是正真釋出和外網運營勢必要釋出到Linux環境下,而且有很多服務端的工具對於Windows系統的相容性並不是非常完善,就像常用的Docker容器就只支援Linux系統,所以提前瞭解和部署

Intellij IDEA開發環境配置實際開發應用

           軟體開發隨著時代的進步也在不斷的進步,並且隨著技術的不斷更新,智慧化逐漸成為軟體開發的主流。智慧開發的前提就是智慧化的開發工具,整所謂上陣打仗,任你武功再好也不敵一槍打死,所以工具很重要,現在主流的開發工具很多,但是為了使團隊便於管理與協調,我們往往會

ubuntu14.04 Android/linux開發環境配置總結(持續更新)

一、更新源方法及地址:1 、首先備份 Ubuntu14.04 源列表sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup  (備份下當前的源列表)2 、修改更新源sudo gedit /etc/apt/sourc

開發環境配置基本執行原理講解

開發環境配置: 1.cocos2dx 2.2.3 windows開發環境搭建過程      win7 64位 + vs2013 + cocos2dx 2.2.3 + python2.7   1) 安裝python2.7,安裝過程預設即可   2)設定環境變數 Path,加上

使用Cordova進行iOS開發 (環境配置基本用法)

以上如果有錯,試試下邊的 1、安裝nodejs(自動包含npm) 2、在命令列中通過npm語句npm install -g cordova 安裝cordova(如果提示網路連線失敗,需要設定網路代理,搭理網址:npm config --global set registry http://regis

Android 原生應用嵌入React-Native模組開發-環境配置填坑記

1.Can't find variable: __fbBatchedBridge 還是在專案的根資料夾下,命令列執行如下命令,啟動測試伺服器。$ npm start 但是部分Android 6.0的機

Win10 QT5.7.1 opencv開發環境配置問題解決

主要參考這兩篇部落格: http://www.cnblogs.com/TooyLee/p/6438070.html http://blog.csdn.net/zhaocj/article/detail

Linux鞏固記錄(1) J2EE開發環境搭建網絡配置

version 環境 com sco 由於 lin spa node 開發環境 由於要近期使用hadoop等進行相關任務執行,操作linux時候就多了 以前只在linux上配置J2EE項目執行環境,無非配置下jdk,部署tomcat,再通過docker或者jenkins自動

VS2017配置Linux開發環境

uil 生成 com linu 項目目錄 rem onf 項目 oot 1、 輸出目錄:$(ProjectDir)bin\$(Platform)\$(Configuration)中間目錄:$(ProjectDir)obj\$(Platform)\$(Configu

java入門---windows和Linux,UNIX,Solaris,FreeBSD下開發環境配置

java應用程序 target details 感覺 開發工具 org chm files java ide 首先來看Windows下的操作。我們需要下載java開發工具包JDK。下載地址:http://www.oracle.com/technetwork/java

linux golang開發環境配置(離線方式)

path ted cal git app clas avi wince 執行文件 <獲取開發工具> 到https://www.golangtc.com/download 下載安裝包, 根據自己的系統選擇合適的開發包,這裏選擇go.1.9.2.linux-

Java配置----JDK開發環境搭建環境變量配置

itl 屬性 ssp 出現 jdk1.8 width tools.jar 創建 pat 1、安裝JDK開發環境 2、配置環境變量: 對於Java程序開發而言,主要會使用JDK的兩個命令:javac.exe、java.exe。路徑:C:\Java\jdk 1.7.0 _0

JDK開發環境搭建環境變量配置

www img 代碼 信息 cmd 需要 系統 title oracl 1、安裝JDK開發環境 下載網站:http://www.oracle.com/ 開始安裝JDK: 修改安裝目錄如下: 確定之後,單擊“下一步”。 註:當提示安裝JRE時,可以選擇不要安裝。 2

Windows下JAVA開發環境搭建環境變數配置

1、安裝JDK開發環境 下載網站:http://www.oracle.com/ 開始安裝JDK: 修改安裝目錄如下: 確定之後,單擊“下一步”。 注:當提示安裝JRE時,可以選擇不要安裝。 2、配置環境變數: 對於Java程式開發而言,主要會