ubuntu下一个开发环境安装的shell程序

在安装ubuntu后,一些开发环境并未被安装,出于每次安装都要输入一大堆安装命令实属不方便,特写了一个自动安装的小脚本程序以便使用,你也可以在此基础上添加更多你自己的应用,将以下脚本代码保存为shell文件。 使用 chmod +x "脚本文件名"  将脚本文件设置为可执行。
./"脚本文件名" 即可运行
1、配置文件示例 item.cdb
#说明:1为编号,GCC为要安装的项目,这里为GCC则在调用函数时传递的参数也要为GCC, build-essential为安装的文件
      1,GCC,build-essential
       2,GCC,gnome-core-devel
      3,GCC,libglb2.0-doc libgtk2.0-doc
      4,GCC,devhelp
      5,GCC,glade-gnome glade-common glade-doc
      6,MySql,/etc/init.d/mysql restart
      7,PHP,ssh
      8,PHP,mysql-server-5.0
      9,PHP,apache2
     10,PHP,php5 libapache2-mod-php5
     11,PHP,/etc/init.d/apache2 restart
     11,PHP,phpmyadmin

代码如下:
 # !/bin/sh

item_file="item.cdb"
temp_file="/tmp/cdb1.$$"
temp_f="d.cdb"
trap `rm -f $temp_file $temp_f` EXIT

get_return()
{
    read x
    return 0
}

get_helps()
{
cat << !helps!
        目录下item.cdb文件为需要安装的配置文件,如下所示:
        1,GCC,build-essential
        编号,项目名称(程序通过此名称进行查找安装项),安装命令

        安装函数的使用方法:
        setupitem GCC
        说明:参数GCC, 函数通过查找item.cdb文件中是否有GCC相对
              应的项,如果有则取出项进行安装,注意区分大小写
!helps!
    echo "按任意键返回。。。"
    get_return
    return 0
}

restartitem()
{
    echo -e "是否确定启动 $*?(y/n) /c"
    read x
    echo

    if [ "$x" != "y" ];then
        return 1
    fi

    if [ -z "$*" ];then
        echo "参数不可为空,按任意键返回..."
        get_return
        return
    fi

    tmp=$*
    items=${tmp%%,*}
    if [ $items != $tmp ];then
        echo "传递的参数不正确,按任意键返回..."
        get_return
        return
    fi

    if [ -n "$items" ];then
        grep "$items" $item_file > $temp_file
        set $(wc -l $temp_file)
        linescount=$1

        case "$linescount" in
        0)    echo
            echo "未能找到配置项目,按任意键返回..."
            get_return
            return 1
            ;;
        esac

        IFS=","
        read num title items < $temp_file
        if [ -z $num ];then
            break
        fi

        IFS=" "
        echo "开始执行启动命令,请稍候..."
        sudo $items
        echo "启动完成,按任意键返回"
        get_return
    fi
}

#传递进来的参数要与配置文件中第二项对应
setupitem()
{
    echo -e  "是否确定要安装 $*?(y/n) /c " #加-e表示当前不换行
    read dd
    echo
    if [ "$dd" != "y" ];then
        echo "安装失败"
        return 1
    fi

    if [ -z "$*" ];then
        echo "调用参数为空"
    fi

    tmp=$*
    items=${tmp%%,*}
    if [ $items != $tmp ];then
        echo "选择的项不正确,按任意键返回"
        get_return
        return 1
    fi

    if [ -n "$items" ];then
        grep "$items" $item_file > $temp_file
        set $(wc -l $temp_file)
        linescount=$1

        case "$linescount" in
        0)    echo
            echo "未找到要安装的配置项,按任意键返回"
            get_return
            return 1
            ;;
        *)  echo "共找到 $1 条安装内容项,内容如下"
            cut -f 3- -d , $temp_file
            echo
            echo -e "按任意键开始安装... /c "
            get_return
        esac

        while true
        do
            IFS=","
            read num title items < $temp_file
            if [ -z $num ];then
                break
            fi

            IFS=" "
            echo "将要开始安装 $items, 请稍候..."
            sudo apt-get install $items    #执行命令       
            echo "安装完成..."
            echo

            if [ -f "$temp_f" ];then
                touch $temp_f
            fi
            grep -v "^${num}," $temp_file > $temp_f
            #cat $temp_f
            sleep 1
            mv $temp_f $temp_file
            rm -f $temp_f
        done
    else
        echo " 选择的 $tmp 安装项未被配置,按任意键返回"
        get_return
        return 1
    fi
   
    echo
    echo "$tmp 安装完成,按任意键返回主界面..."
    get_return
    return
}

while true
    do
    clear
    cat << !begin!

    Linux 开发环境安装程序(v1.0)
        作者:沙能智  email:tyjsnz@gmail.com
        2009-2-23

    请输入下列的编号进行操作

    1 安装 GCC开发环境
    2 安装 PHP开发环境
    3 使用方法
    4 重启MySql数据库
    0或q 退出程序
   

!begin!

    read x
    case "$x" in
    1) setupitem GCC #这里传递的GCC与安装配置文件中的第二项对应
    ;;
    2) setupitem PHP
    ;;
    3) get_helps
    ;;
    4) restartitem MySql
    ;;
    0 | q) break
    ;;
    *) ;;
    esac
done

rm -f $temp_file $temp_f
clear

你可以在至此脚本基础上加入更多的安装选项及功能,省去了将来安装的烦锁!

原文地址:https://www.cnblogs.com/tyjsjl/p/2156052.html