Skip to content

Instantly share code, notes, and snippets.

@flyfire
Created October 25, 2013 12:13
Show Gist options
  • Select an option

  • Save flyfire/7153709 to your computer and use it in GitHub Desktop.

Select an option

Save flyfire/7153709 to your computer and use it in GitHub Desktop.
#!/bin/bash
downloads_dir=~/downloads
#>>>>>>>>>>linux系统相关
#>>>>>>>>>>scm配置管理相关
#>>>>>>>>>>分布式编译相关
#>>>>>>>>>>linux系统相关
#>>>>>>>>>>字符串处理相关
function unset_color()
{
unset GREP_OPTIONS
unset GREP_COLOR
}
##派断是否为 32 位系统
function sh_system_is_i386()
{
local cmd="uname -a"
local result=
#cmd=`sh_get_arch_cmd`
echo "$cmd | grep 64" >&2
result=`$cmd | grep 64`
if [ "$result" = "" ]
then
echo "0" >&2
return 0
else
echo "1" >&2
return 1
fi
}
#判断 CPU 是否支持64位
function sh_cpu_support_64()
{
result=`cat /proc/cpuinfo | grep flags | grep lm`
if [ "$result" = "" ]
then
return 1
else
return 0
fi
}
#获取ubuntu系统版本
function sh_get_ubuntu_version()
{
local ac_version=
ac_version=`cat /etc/issue`
ac_version=`echo $ac_version|awk '{print $2}'`
echo $ac_version
}
function sh_print_system_info()
{
os_ver=`cat /etc/issue`
os_arch=`uname -m`
meminfo=`free -m`
cpu_kernel=`cat /proc/cpuinfo | grep processor | wc -l`
cpu_info=`cat /proc/cpuinfo |grep "model name"`
result=`cat /proc/cpuinfo | grep flags | grep lm`
if [ -n "$result" ]; then
cpu_bit=64
else
cpu_bit=32
fi
echo "
=========================================
ubuntu 版本: $os_ver
操作系统位数: $os_arch
内存信息:
$meminfo
cpu信息:
`cat /proc/cpuinfo |grep -E 'model name|vendor_id' | sort |uniq`
$cpu_info
cpu核数: $cpu_kernel
cpu位数: $cpu_bit
==========================================
"
}
##
function sh_dpkg_status()
{
if [ "$1" = "" ]; then
return 0
fi
status=`dpkg -s $1 |grep "install ok installed"`
if [ "$status" != "" ]
then
return 0
else
return 1
fi
}
function sh_dpkg_install()
{
local old_install=
local new_install=
local not_exist=
for pkg in $1
do
if ! sh_dpkg_status $pkg;then
sudo apt-get install $pkg
if ! sh_dpkg_status $pkg;then
new_install="$new_install $pkg"
else
not_exist="$not_exist $pkg"
fi
else
old_install="$old_install $pkg"
fi
done
echo "pkg exist: $old_install" >&2
echo "pkg installed: $new_install" >&2
echo "pkg not exist: $not_exist" >&2
}
function sh_dpkg_remove()
{
local ok_removed=
local no_removed=
for pkg in $1
do
if sh_dpkg_status $pkg; then
sudo apt-get remove $pkg
ok_removed="$ok_removed $pkg"
else
no_removed="$no_removed $pkg"
fi
done
echo "pkg removed: $ok_removed" >&2
echo "pkg not exist: $no_removed" >&2
}
##
function sh_get_date()
{
local ac_date="$(date +%Y%m%d)"
echo $ac_date
}
##
function sh_get_time()
{
local ac_time="$(date +%T)"
echo $ac_time
}
##
function sh_get_date_and_time()
{
local ac_date="$(date +%Y%m%d)"
local ac_time="$(date +%T)"
echo $ac_date-$ac_time
}
##
function sh_get_host_ip()
{
unset GREP_OPTIONS
unset GREP_COLOR
local route_info=(`ifconfig |grep "地址:" |grep "广播"`)
local ipaddr=`echo ${route_info[1]} | sed s/地址://`
echo $ipaddr
}
function sh_get_ip_address()
{
unset GREP_OPTIONS
unset GREP_COLOR
local route_info=(`ifconfig |grep "地址:" |grep "广播"`)
local ipaddr=`echo ${route_info[1]} | sed s/地址://`
echo $ipaddr
}
function sh_get_netmask()
{
unset GREP_OPTIONS
unset GREP_COLOR
local route_info=(`ifconfig |grep "地址:" |grep "广播"`)
local netmask=`echo ${route_info[3]} | sed s/掩码://`
echo $netmask
}
function sh_get_gateway()
{
unset GREP_OPTIONS
unset GREP_COLOR
local route_info=(`route |grep "default"`)
local gateway=`echo ${route_info[1]}`
echo $gateway
}
function sh_get_dns()
{
unset GREP_OPTIONS
unset GREP_COLOR
local route_info=(`cat /etc/resolv.conf |grep "nameserver"`)
local nameserver=`echo ${route_info[1]}`
echo $nameserver
}
##
function sh_at_home()
{
unset GREP_OPTIONS
unset GREP_COLOR
local ipaddr=`sh_get_host_ip`
local at_home=`echo $ipaddr|grep "192.168"`
if [ "$at_home" = "" ]
then
return 1
else
return 0
fi
}
function VERSION_COMPARE()
{
ac_cmd=`which $1`
if [ "$ac_cmd" = "" ]; then
soft_required=true
files_should_install="$1 $files_should_install"
fi
if test `VERSION_TO_NUMBER $2` -ge `VERSION_TO_NUMBER $3`
then
echoText "Version $ac_version found" GREEN
else
echoText "Version $ac_version found. You need at least bash $3" RED
soft_required=true
files_should_install="$1 $files_should_install"
fi
}
function sh_versin_char_to_number()
{
ac_var=`echo $1 | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 10000 + $2) * 10000 + $3;}'`
echo $ac_var
}
function sh_cmp_version()
{
#echo `sh_versin_char_to_number $1`>&2
#echo `sh_versin_char_to_number $2`>&2
if test `sh_versin_char_to_number $1` -gt `sh_versin_char_to_number $2`
then
echo "1" >&2
return 1
else
echo "0" >&2
return 0
fi
}
function sh_version_equel()
{
if test `sh_versin_char_to_number $1` -eq `sh_versin_char_to_number $2`
then
echo "1" >&2
return 1
else
echo "0" >&2
return 0
fi
}
function sh_at_comp()
{
unset GREP_OPTIONS
unset GREP_COLOR
local ipaddr=`sh_get_host_ip`
local at_com=`echo $ipaddr|grep "172.20"`
if [ "$at_com" = "" ]
then
return 1
else
return 0
fi
}
function sh_is_svn_server()
{
unset GREP_OPTIONS
unset GREP_COLOR
local ipaddr=
if [ -n "$1" ]; then
ipaddr=$1
else
ipaddr=`sh_get_host_ip`
fi
if [ "$ipaddr" = "172.20.127.15" ]
then
return 0
else
return 1
fi
}
function sh_get_lineno()
{
index=1
local cunzai
local authz_file=
local lineno=
if [ "$1" = "" ]; then
echo "$lineno"
return
else
authz_file=$1
fi
while read gname;
do
result=`echo $gname | grep "$2" -w`
if [ "$result" != "" ]; then
lineno=$index
fi
((index=${index}+1))
done <${authz_file}
echo $lineno
}
function sh_sed_replace_low()
{
index=1
local cunzai
local authz_file=
local lineno=
if [ "$1" = "" ]; then
echo "$lineno"
return
else
authz_file=$1
fi
while read gname;
do
result=`echo $gname | grep "$2" -w`
if [ "$result" != "" ]; then
lineno=$index
sed -i "${lineno}s/.*/${3}/" ${authz_file}
fi
((index=${index}+1))
done <${authz_file}
echo $lineno
}
[ -z "$EDITOR" ]
function sh_wget()
{
local result=0
local ac_dir=./
if [ "$1" = "" ]; then
return 0
fi
if [ "$2" != "" ]; then
ac_dir=$2
fi
wget --no-check-certificate $1 --directory-prefix=$ac_dir --no-clobber
result=1
return ${result}
}
#>>>>>>>>>>字符串处理相关end
#http://hlee.javaeye.com/blog/577628
#http://linux.chinaunix.net/techdoc/develop/2008/12/31/1055895.shtml
function IsNumeric()
{
_NUM=$1
_NUM=`echo $1|awk '{if($0~/[^0-9]/) print "1"}'`
if [ "$_NUM" != "" ]
then
return 1
else
return 0
fi
}
# characters()
function IsCharacters()
{
_LETTERS_ONLY=$1
_LETTERS_ONLY=`echo $1|awk '{if($0~/[^a-zA-Z]/) print "1"}'`
if [ "$_LETTERS_ONLY" != "" ]
then
return 0
else
return 1
fi
}
#$1信息,$2默认路径,功能:获得手动输入目录:BY dwc
#用法:curr_dir=`inputDirectory "请输入路径," "~/workspace"`
function inputDirectory()
{
local message=$1
local defaultDir=$2
dirc=
while [ 1 ]
do
echoText "$message 默认路径为:$defaultDir" RED >&2
echo -n "input directory:" >&2
read dirc
if [ "x$dirc" = "x" ]; then
dirc="$defaultDir/"
break;
elif [ -d "$dirc" ]; then
dirc="$dirc/"
break;
else
result=`question "$dirc is not a directory! Create it ?" 1 yes`
if [ "$result" = "yes" ] ; then
mkdir -pv $dirc #创建目录
break;
fi
fi
done
echo $dirc
}
#$1,信息,$2,最小值(包含),$3最大值(包含),$4默认值:BY dwc
#功能:获得手动输入指定范围内的数值
function inputNumber()
{
local message=$1
local numBegin=$2
local numEnd=$3
local numDefault=$4
user_num=
while [ 1 ]
do
echoText "$1数值范围为[$numBegin,$numEnd],默认值为: $numDefault" GREEN >&2
echo -n "input number:" >&2
read user_num
if [ "x$user_num" = "x" ] ; then
user_num=$numDefault
break;
elif $(IsNumeric $user_num) ; then
if [ $user_num -ge $numBegin -a $user_num -le $numEnd ] ; then
break;
fi
fi
done
echo $user_num
}
#$1 提示信息 $2数组 $3 默认选项:BY dwc
#默认选项从“1”开始计数,
#调用示例 : list=(a b c d e f g)
#调用示例 : getChoice "该文件夹下的版本库有:" list 2
#调用示例 : echo "you choose $?"
#获得返回值使用aa=`getChoice "该文件夹下的版本库有:" list 2`
#直接回车得到答案真实答案 b,而不是2
function getChoice()
{
local index=1
local message=$1
eval parameter_list=(\${$2[@]})
local default_value=$3
local ANSWER=
local v
echoText "$message 默认选项为[ $default_value ]" RED >&2
for v in "${parameter_list[@]}"
do
echo " $index. $v" >&2
index=$(($index+1))
done
while [ 1 ]
do
echoText "请选择! 默认选项为: [ $default_value ] " GREEN >&2
echo -n "enter number:" >&2
read ANSWER
if [ -z "$ANSWER" ] ; then
ANSWER=$default_value
break;
elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
if [ $ANSWER -le ${#parameter_list[@]} -a $ANSWER -gt 0 ] ; then
# MATCH=$ANSWER
break;
fi
fi
done
# echo "$ANSWER" >&2
echo ${parameter_list[$(( $ANSWER - 1 ))]}
}
#整数比较
#-eq 等于,如:if [ "$a" -eq "$b" ]
#字符串比较
#= 等于,如:if [ "$a" = "$b" ]
#!= 不等于,如:if [ "$a" != "$b" ]
#-z 字符串为"null".就是长度为0.
#-n 字符串不为"null"
#>>>>>>>>>>字符串处理相关end
#>>>>>>>>>>linux系统相关end
#tr -d ' ' 删除空格
#tr -d ' ' 删除所有回车
#tr -d ' ' 'aaaa' 所有空格用 aaaa 代替
#echo $aaaa |awk '{print $0}' 打印整行
script_test=false
MKYAFFS2="$(which mkyaffs2image)"
#=========================
htc=~/htc
google_apk_list="\
checkin \
Gmail \
GmailProvider \
GoogleApps \
GoogleContactsProvider \
GoogleSubscribedFeedsProvider \
gtalkservice \
Maps \
MediaUploader \
NetworkLocation \
SetupWizard \
Street \
Talk \
Vending \
YouTube \
ImProvider \
"
google_apk_list1="\
GooglePartnerSetup \
GoogleSettingsProvider \
VoiceSearch \
"
google_xml_list="\
com.google.android.gtalkservice \
com.google.android.maps"
google_jar_list="\
com.google.android.gtalkservice \
com.google.android.maps"
function cp_google_apps()
{
src_dir=$1
dst_dir=$2
echo "src_dir=$src_dir dst_dir=$dst_dir"
#获取apk文件
mkdir -pv ${dst_dir}/system/app
for file in ${google_apk_list}
do
cp ${src_dir}/system/app/${file}.apk ${dst_dir}/system/app/${file}.apk -rfv
done
#获取配置文件
mkdir -pv ${dst_dir}/system/etc/permissions
for file in ${google_xml_list}
do
cp ${src_dir}/system/etc/permissions/${file}.xml ${dst_dir}/system/etc/permissions/${file}.xml -rfv
done
#获取库文件
mkdir -pv ${dst_dir}/system/framework
for file in ${google_jar_list}
do
cp ${src_dir}/system/framework/${file}.jar ${dst_dir}/system/framework/${file}.jar -rfv
done
}
#==========================
function check_build_env()
{
if test x"$ANDROID_BUILD_TOP" = x ; then
echoBegin "环境变量为空,现在将进行设置..."
android_dir=`question "输入android源码路径,默认使用:" 2 ${defult_android_dir}`
if test ! -d $android_dir ; then
echoColor "目录 $android_dir 不存在! " RED
exit
fi
cd ${android_dir}
. build/envsetup.sh
choosecombo
#. ./make_env15.sh
fi
}
#按照特定的格式获取时间串
function get_current_time_date()
{
#时间信息相关
#ac_date=`date | sed 's/ //g'|sed 's/CST//g'`
#ac_time=`date +%X | sed 's/ //g'|sed 's/CST//g'`
ac_time="$(date +%T)"
ac_date="$(date +%Y%m%d)"
ac_time=`echo $ac_time|sed "s/://g"`
if [ "$1" = "date" ]; then
echo $ac_date
elif [ "$1" = "time" ]; then
echo $ac_time
else
echo $ac_date$ac_time
fi
}
#时间信息相关
ac_time=`get_current_time_date time`
ac_date=`get_current_time_date date`
ac_time_date=`get_current_time_date`
#echo "当前日期:$ac_date"
#echo "当前时间:$ac_time"
#echo "当前日期和时间:$ac_time_date"
#=================颜色相关================
#在shell中使用echo命令输出带颜色的文本
#参考文档:http://hi.baidu.com/lonelycorn/blog/item/5f6a09d8c184fa3332fa1c45.html
#0 透明(使用终端颜色), 40 黑, 41 红, 42 绿, 43 黄, 44 蓝 45 紫, 46 青绿, 47白(灰)
#前景色(也就是文本的颜色)可以被以下数字替换
#30 黑 31 红, 32 绿, 33 黄, 34 蓝, 35 紫, 36 青绿, 37 白(灰)
#高亮是1,不高亮是0
#注意m后面紧跟字符串。
function echoColor()
{
if [ $# != 2 ]
then
return 1
fi
szString=$1
case "$2" in
BLACK)
szColor=31
;;
RED)
szColor=31
;;
GREEN)
szColor=32
;;
YELLOW)
szColor=33
;;
BLUE)
szColor=34
;;
PURPLE)
szColor=35
;;
CYAN)
szColor=36
;;
WHITE)
szColor=36
;;
reset)
szColor=0
;;
*)
#szColor=39
szColor=50
esac
echo -e "\\033["$szColor"m$szString\\033[39m"
return 0;
}
function echoText()
{
if [ $# != 2 ]
then
return 1
fi
szString=$1
case "$2" in
BLACK)
szColor=31
;;
RED)
szColor=31
;;
GREEN)
szColor=32
;;
YELLOW)
szColor=33
;;
BLUE)
szColor=34
;;
PURPLE)
szColor=35
;;
CYAN)
szColor=36
;;
WHITE)
szColor=36
;;
reset)
szColor=0
;;
*)
#szColor=39
szColor=50
esac
echo -e "\\033["$szColor"m$szString\\033[39m"
return 0;
}
function echoTextRed()
{
echoText "$1" RED
}
function echoTextGreen()
{
echoText "$1" GREEN
}
function echoTextPurple()
{
echoText "$1" PURPLE
}
function echoTextBlue()
{
echoText "$1" BLUE
}
function echoTextCyan()
{
echoText "$1" CYAN
}
function echoBegin()
{
for BG in 44m;
do
echo -en " \033$FG\033[$BG $1 \033[0m";echo
done
}
function echoEnd()
{
for BG in 45m;
do
echo -en " \033$FG\033[$BG $1 \033[0m"
done
}
function echoColorTest()
{
T='gYw' # The test text
echo
echo " default 40m 41m 42m 43m 44m 45m 46m
47m"
## FGs 为前景(foreground)色, BG 为背景(background)色
for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' '1;32m' '
33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' ' 36m' '1;36m' ' 37m' '1;37m'
do
FG=$(echo $FGs|tr -d ' ')
echo -en " $FGs \033[$FG $T "
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
do
echo -en " \033[$FG\033[$BG $T \033[0m"
done
echo
done
echo
}
#================交互式输入函数===============
# qmode : 1 = yes/no
# 2 = string
function question () {
unset GREP_OPTIONS
unset GREP_COLOR
local q=$1
local qmode=$2
local def="$3"
if [ "$qmode" = "1" ] ; then
while [ 1 = 1 ] ; do
#echo -n "$q [yes/no]" >&2
echoText "$q [yes/no]" RED >&2
if [ x"$def" != x ]; then
echo -n " ($def)" >&2
fi
echo -n ": " >&2
read answer
if [ "x$answer" = "x" ] ; then
echo "$def"
return
fi
if [ "$answer" != "yes" ] && [ "$answer" != "no" ] ; then
echo "Answer 'yes' or 'no' !!" >&2
else
echo "$answer"
return
fi
done
elif [ "$qmode" = "2" ] ; then
#echo -n "$q" >&2
echoText "$q" RED>&2
if [ x"$def" != x ]; then
echoText "default:($def)" GREEN >&2
#echo ":($def)" >&2
fi
echo -n ": " >&2
read answer
if [ "x$answer" = "x" ] ; then
answer="$def"
fi
echo $answer
return
fi
echo "ERROR: With question mode (script internal error) !?!!" >&2
}
########################################################################
parameter_list_1=
parameter_list_2=
parameter_list_3=
parameter_list_4=
seleted_item=
# check to see if the supplied variant is valid
function check_variant()
{
for v in ${1}
do
if [ "$v" = "$2" ]
then
return 0
fi
done
return 1
}
function choosevariant()
{
echoText "可选择的列表项:" GREEN
local index=1
local v
for v in ${parameter_list_1[@]}
do
echo " $index. $v"
index=$(($index+1))
done
local default_value=${parameter_list_1[${i}]}
local ANSWER
export MATCH=
export MATCH_NO=
while [ -z "$MATCH" ]
do
# echo -n "Which would you like? [$default_value] "
echoText "Which would you like? 默认选择: [$default_value] " RED
echo -n "enter number:"
if [ -z "$1" ] ; then
read ANSWER
else
echo $1
ANSWER=$1
fi
echo "ANSWER=$ANSWER"
if [ -z "$ANSWER" ] ; then
export MATCH=$default_value
elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
if [ "$ANSWER" -le "${#parameter_list_1[@]}" ] ; then
export MATCH=${parameter_list_1[$(($ANSWER-$_arrayoffset))]}
fi
else
if check_variant $ANSWER
then
export MATCH=$ANSWER
else
echo "** Not a valid variant: $ANSWER"
fi
fi
if [ -n "$1" ] ; then
break
fi
done
echo "$ANSWER"
seleted_item=$ANSWER
}
function get_match_parameter1()
{
((i=${1}-1))
echo "${parameter_list_1[${i}]}"
}
function get_match_parameter2()
{
((i=${1}-1))
echo "${parameter_list_2[${i}]}"
}
# determine whether arrays are zero-based (bash) or one-based (zsh)
_xarray=(a b c)
if [ -z "${_xarray[${#_xarray[@]}]}" ]
then
_arrayoffset=1
else
_arrayoffset=0
fi
unset _xarray
########################################################################
#创建标准tftboot目录,并修改权限
function mkdir_tftpboot()
{
if test ! -d /tftpboot ; then
sudo mkdir /tftpboot -pv
sudo chown -R $USER.$USER /tftpboot/
fi
}
#创建标准网络文件系统目录,并修改权限
function mkdir_nfsroot()
{
if test ! -d /nfsroot ; then
sudo mkdir /nfsroot -pv
fi
sudo chown -R $USER.$USER /nfsroot/
}
function mksubdir()
{
if [ "$1" = "" ] || [ "$2" = "" ]
then
return 0
fi
if test -d $1 ; then
if test -d $1/$2 ; then
echoColor "****$1/$2 exist!" RED
else
mkdir $1/$2 -pv
fi
fi
}
#检测系统中存在的命令
function check_cmd()
{
EXPECT=`which expect`
echo "expect path=$EXPECT"
if [ "$_cmd" != "" ]
then
return 1
else
return 0
fi
}
# characters()
function check_expect()
{
if check_cmd expect;then
echoColor "****you have installed expect!" GREEN
else
sudo apt-get install expect
fi
}
# characters()
function dpkg_status()
{
result=`dpkg -s $1|grep "Status: install ok installed"`
if [ "$result" = "" ]
then
return 1
else
return 0
fi
}
function dpkgs_status()
{
result=`dpkg -s $1|grep "Status: install ok installed"`
if [ "$result" = "" ]
then
return 1
else
return 0
fi
}
function dpkg_install()
{
should_set_tftp=false
while test $# != 0
do
#case $1 in
if dpkg_status $1 ; then
installed_list="$1 $installed_list"
else
echoColor "****you should install $1" GREEN
echoColor "****sudo apt-get install $1" GREEN
sudo apt-get install $1
should_set_tftp=true
fi
shift
done
echoColor "****packages [ $installed_list ] 已经安装!" GREEN
}
function sh_menu_select()
{
local default_serial_number=1
local default_value=
local index=0
if [ "$1" = "" ]; then
return
else
ac_menu_list=`echo $1| tr '\n' ' '`
if [ "$2" != "" ]; then
ac_value_list=`echo $2| tr '\n' ' '`
fi
fi
#默认值
default_serial_number=1
if [ "$ac_menu_list" != "" ]; then
menu_list=($ac_menu_list)
default_value=${menu_list[0]}
else
echoText "Menu item is empty" RED >&2
return
fi
#value_list=
if [ "$ac_value_list" != "" ]; then
value_list=($ac_value_list)
default_value=${value_list[0]}
fi
echoText "==== Menu items below: ====" GREEN >&2
for i in ${menu_list[@]}
do
((serial_number=${index}+1))
echoText " ${serial_number}. ${menu_list[${index}]}" GREEN >&2
((index=${index}+1))
done
echoText "====Select from the list above====" GREEN >&2
echoText "exmple: index $index for ${menu_list[${index}-1]}" GREEN >&2
export ac_choose=
local ANSWER
while [ -z $ac_choose ]
do
echo -n "Which would you like? [1]" >&2
if [ -z "$3" ] ; then
read ANSWER
else
ANSWER=$3
fi
case $ANSWER in
"")
export ac_choose=true
ANSWER=1
return_value=${default_value}
;;
*)
if IsNumeric $ANSWER;then
if [ "$ANSWER" -gt "$index" ]; then
echoText "I didn't understand your response. Please try again." RED >&2
echo >&2
else
if [ "$value_list" != "" ]; then
return_value=${value_list[${ANSWER}-1]}
else
return_value=${menu_list[${ANSWER}-1]}
fi
export ac_choose=true
fi
else
return_value=$ANSWER
export ac_choose=true
fi
;;
esac
if [ -n "$3" ] ; then
break
fi
done
if IsNumeric $ANSWER;then
echoTextGreen "==> your answer is: ${ANSWER}. ${return_value}" >&2
else
echoTextGreen "==> your answer is: ${return_value}" >&2
fi
result=`echo $return_value| grep ".sh"`
if [ -z "$result" ]; then
return_value=`echo $return_value| tr '.' ' '`
fi
echo "$return_value"
export return_value
}
function sh_menu_select2()
{
local default_serial_number=1
local default_value=
local index=0
if [ "$1" = "" ]; then
return
else
ac_menu_list=`echo $1| tr '\n' ' '`
if [ "$2" != "" ]; then
ac_value_list=`echo $2| tr '\n' ' '`
fi
fi
#默认值
default_serial_number=1
if [ "$ac_menu_list" != "" ]; then
menu_list=($ac_menu_list)
default_value=${menu_list[0]}
else
echoText "Menu item is empty" RED >&2
return
fi
#value_list=
if [ "$ac_value_list" != "" ]; then
value_list=($ac_value_list)
default_value=${value_list[0]}
fi
echoText "==== Menu items below: ====" GREEN >&2
for i in ${menu_list[@]}
do
((serial_number=${index}+1))
echoText " ${serial_number}. ${menu_list[${index}]}" GREEN >&2
((index=${index}+1))
done
echoText "====Select from the list above====" GREEN >&2
echoText "exmple: index $index for ${menu_list[${index}-1]}" GREEN >&2
export ac_choose=
local ANSWER
while [ -z $ac_choose ]
do
echo -n "Which would you like? [1]" >&2
if [ -z "$3" ] ; then
read ANSWER
else
ANSWER=$3
fi
case $ANSWER in
"")
export ac_choose=true
ANSWER=1
return_value=${default_value}
;;
*)
if IsNumeric $ANSWER;then
if [ "$ANSWER" -gt "$index" ]; then
echoText "I didn't understand your response. Please try again." RED >&2
echo >&2
else
if [ "$value_list" != "" ]; then
return_value=${value_list[${ANSWER}-1]}
else
return_value=${menu_list[${ANSWER}-1]}
fi
export ac_choose=true
fi
else
return_value=$ANSWER
export ac_choose=true
fi
;;
esac
if [ -n "$3" ] ; then
break
fi
done
if IsNumeric $ANSWER;then
echoTextGreen "==> your answer is: ${ANSWER}. ${return_value}" >&2
else
echoTextGreen "==> your answer is: ${return_value}" >&2
fi
echo "$return_value"
export return_value
}
function sh_ls()
{
local default_serial_number=1
local default_value=
local index=0
local ac_list=
local dir_list=
local repos_list=
if [ "$1" = "" ]; then
return
else
if [ "$2" = "svn" ]; then
ac_list=`svn list $1| tr '\n' ' '`
else
ac_list=`ls $1 2>/dev/null | tr '\n' ' '`
fi
fi
#判断目录
for file in $ac_list
do
if [ "$2" = "files" ]; then
break
fi
if test -d $1/$file ; then
dir_list="$dir_list $file"
if test -f $1/$file/conf/svnserve.conf ; then
repos_list="$repos_list $file"
fi
fi
done
if [ "$2" = "repo" ]; then
ac_list=$repos_list
elif [ "$2" = "svn" ]; then
ac_list=$ac_list
elif [ "$2" = "files" ]; then
ac_list=$ac_list
else
ac_list=$dir_list
fi
if [ "$ac_list" != "" ]; then
default_serial_number=1
targets_list=($ac_list)
default_value=${targets_list[0]}
else
echoText "Menu item is empty" RED >&2
return
fi
echoText "==== Items below $1 ====" GREEN >&2
for i in ${targets_list[@]}
do
((serial_number=${index}+1))
echoText " ${serial_number}. ${targets_list[${index}]}" GREEN >&2
((index=${index}+1))
done
echoText "====Select from the list above====" GREEN >&2
echoText "exmple: index $index for ${targets_list[${index}-1]}" GREEN >&2
echo >&2
export ac_choose=
local ANSWER
while [ -z $ac_choose ]
do
echo -n "Which would you like? [1]" >&2
if [ -z "$3" ] ; then
read ANSWER
else
ANSWER=$3
fi
case $ANSWER in
"")
export ac_choose=true
ANSWER=1
if test -d $1; then
return_value=$1/${default_value}
else
return_value=${default_value}
fi
;;
*)
if [ "$ANSWER" -gt "$index" ]; then
echoText "I didn't understand your response. Please try again." RED >&2
echo >&2
else
if [ "$2" = "files" ]; then
return_value=${targets_list[${ANSWER}-1]}
elif test -d $1; then
return_value=$1/${targets_list[${ANSWER}-1]}
else
return_value=${targets_list[${ANSWER}-1]}
fi
export ac_choose=true
fi
;;
esac
if [ -n "$3" ] ; then
break
fi
done
echoTextGreen "==> your answer is: ${ANSWER}. ${targets_list[${ANSWER}-1]}" >&2
echo $return_value
export return_value
}
function read_by_line()
{
File=$1
num=`wc -l <$File`
echo $num
i=0
while [ $i -lt $num ]
do
{
let "i = $i + 1"
read line[$i]
echo -e "${line[$i]}"|tee ~/aaa.log
}
done <$File
}
function read_by_line2()
{
TMPFILE=`mktemp -q /tmp/$0.XXXXXX`
echo "TMPFILE=$TMPFILE"
cat ~/svndiff.log > $TMPFILE
cat $TMPFILE | while read line
do
echo $line
done
}
function check_env()
{
if test x"$ANDROID_BUILD_TOP" = x ; then
echoBegin "环境变量为空,现在将进行设置..."
android_dir=`question "输入android源码路径,默认使用:" 2 ${defult_android_dir}`
if test ! -d $android_dir ; then
echoColor "目录 $android_dir 不存在! " RED
exit
fi
cd ${android_dir}
. ./make_env15.sh
fi
}
#初始化镜像文件名称
function init_name_of_imgs()
{
add_info=${1}
if [ "$1" = "" ]; then
ac_add_info=${1}
else
ac_add_info=_${1}
fi
name_of_img_system=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}${ac_add_info}_system.img
name_of_img_data=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}${ac_add_info}_data.img
name_of_kernel=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}${ac_add_info}_${_kernel}
name_of_kernel_nfs=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}${ac_add_info}_${_kernel}_nfs
echo "add_info=${_board}_ ${_bbk_version} ${add_info} _system"
if [ "$name_of_img_system" = "" ]; then
name_of_img_system=system.img
fi
if [ "$name_of_img_data" = "" ]; then
name_of_img_data=data.img
fi
if [ "$name_of_kernel" = "" ]; then
name_of_kernel=zImage
fi
if [ "$name_of_kernel_nfs" = "" ]; then
name_of_kernel_nfs=zImage_nfs
fi
}
function get_name_of_img()
{
if [ "$1" = "kernel" ];then
add_kernel_info=`question "为文 本机启动的内核 输入补充信息:" 2 `
if [ "$add_kernel_info" != "" ];then
add_kernel_info=_${add_kernel_info}
fi
ac_img_name=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}_${_kernel}${add_kernel_info}
elif [ "$1" = "kernel-nfs" ];then
add_kernel_info=`question "为文 网络启动的内核 输入补充信息:" 2 `
if [ "$add_kernel_info" != "" ];then
add_kernel_info=_${add_kernel_info}
fi
ac_img_name=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}_${_kernel}${add_kernel_info}_nfs
elif [ "$1" = "system" ];then
add_imgs_info=`question "为文件系统 system 分区输入补充信息:" 2 `
if [ "$add_imgs_info" != "" ];then
add_imgs_info=_${add_imgs_info}
fi
ac_img_name=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}${add_imgs_info}_system.img
elif [ "$1" = "data" ]; then
add_imgs_info=`question "为文件系统 data 分区输入补充信息:" 2 `
if [ "$add_imgs_info" != "" ];then
add_imgs_info=_${add_imgs_info}
fi
ac_img_name=${_project}-${_platform}_${_android}_${ac_date}_svn${SVN_R}_${_board}_${_bbk_version}${add_imgs_info}_data.img
fi
echo $ac_img_name
}
#修改 /nfsroot/rootfs/system/build.prop 文件
function sed_config_build_prop()
{
result=`grep ro.software.version /nfsroot/rootfs/system/build.prop`
if [ "$result" = "" ]; then
sed -i "\$a\ro.software.version=${_project}-${_platform}_${_android}_svn${SVN_R}_${ac_date}_${_board}_${_bbk_version}" /nfsroot/rootfs/system/build.prop
else
sed -i "/ro.software.version/c\\ro.software.version=${_project}-${_platform}_${_android}_svn${SVN_R}_${ac_date}_${_board}_${_bbk_version}" /nfsroot/rootfs/system/build.prop
fi
}
function awk_config_kernel_usb_eth()
{
cat .config > config_tmp
cat config_tmp | awk '{if($0 ~ /# CONFIG_USB_ETH is not set/) print "CONFIG_USB_ETH=y"; else if($0 ~ /CONFIG_USB_ANDROID=y/) print "# CONFIG_USB_ANDROID is not set"; print $0;}' > .config
rm config_tmp
}
function awk_config_kernel_usb_android()
{
cat .config > config_tmp
cat config_tmp | awk '{if($0 ~ /CONFIG_USB_ETH=y/) print "# CONFIG_USB_ETH is not set"; else if($0 ~ /# CONFIG_USB_ANDROID is not set/) print "CONFIG_USB_ANDROID=y"; print $0;}' > .config
rm config_tmp
}
function sed_config_initrc()
{
#支持nfs启动
if [ "$1" = "" ]; then
return
fi
echo;echo;echoColor "***** 去掉 /nfsroot/$rootfs_name/init.rc 中的 mount项来支持网络启动 " GREEN
sed -i "s/mount rootfs rootfs/#mount rootfs rootfs/g" $1/init.rc
sed -i "s/mount yaffs2 mtd/#mount yaffs2 mtd/g" $1/init.rc
sed -i "s/ro remount/rw remount/g" $1/init.rc
}
function add_busybox()
{
cmds="mkdir rm touch mv cp top mount"
bin_path=/nfsroot/rootfs/data/bin
mkdir -p ${bin_path}
cp $ANDROID_BUILD_TOP/vendor/marvell/littleton/busybox ${bin_path} -rfv
#创建一些链接
for cmd in ${cmds}
do
ln -sv /data/bin/busybox /nfsroot/rootfs/system/bin/$cmd
ln -sv /data/bin/busybox /nfsroot/rootfs/data/bin/$cmd
done
}
function add_links_to_busybox()
{
cmds="touch cp top tee"
if test -f /nfsroot/rootfs/system/bin/busybox; then
for cmd in ${cmds}
do
ln -sv /system/bin/busybox /nfsroot/rootfs/system/bin/$cmd
done
fi
}
parameter_list_1=(cupcake-jianping-framework linux-2.6.28-a1-ddr256M-20100120 linux-2.6.28-a1)
parameter_list_2=($HOME/workspace/gphone/cupcake-jianping-framework $HOME/workspace/gphone/linux-2.6.28-a1-ddr256M-20100120 $HOME/workspace/gphone/linux-2.6.28-a1)
function mkfs_nfs()
{
echo 当前日期=$ac_date
echo 当前时间=$ac_time
#检测环境变量
check_env
rootfs_name=`basename $ANDROID_BUILD_TOP`
#创建/nfsroot目录
mkdir_nfsroot
mksubdir /nfsroot $rootfs_name
#检测android编译是否完整
echo;echo;echoColor "*****拷贝 $ANDROID_PRODUCT_OUT/root/* 到 /nfsroot/$rootfs_name/ " GREEN
if test ! -d $ANDROID_PRODUCT_OUT/root ; then
echoColor "***** 生成的文件系统不正常,请到目录 $ANDROID_BUILD_TOP 下重新编译 " RED
cd $ANDROID_BUILD_TOP
echoColor "***** 执行命令: . ./make_image15.sh " RED
exit
else
cp $ANDROID_PRODUCT_OUT/root/* /nfsroot/$rootfs_name/ -rfv
fi
if test -L $ANDROID_PRODUCT_OUT/system ; then
echo "符号链接 $ANDROID_PRODUCT_OUT/system 存在"
rm $ANDROID_PRODUCT_OUT/system -rf
ln -svf /nfsroot/$rootfs_name/system $ANDROID_PRODUCT_OUT/system
else
echo "符号链接 $ANDROID_PRODUCT_OUT/system 不存在"
cp $ANDROID_PRODUCT_OUT/system /nfsroot/$rootfs_name/ -rf
rm $ANDROID_PRODUCT_OUT/system -rf
ln -svf /nfsroot/$rootfs_name/system $ANDROID_PRODUCT_OUT/system
fi
if test -L $ANDROID_PRODUCT_OUT/data ; then
echo "符号链接 $ANDROID_PRODUCT_OUT/data 存在"
rm $ANDROID_PRODUCT_OUT/data -rf
ln -svf /nfsroot/$rootfs_name/data $ANDROID_PRODUCT_OUT/data
else
echo "符号链接 $ANDROID_PRODUCT_OUT/data 不存在"
cp $ANDROID_PRODUCT_OUT/data /nfsroot/$rootfs_name/ -rf
rm $ANDROID_PRODUCT_OUT/data -rf
ln -svf /nfsroot/$rootfs_name/data $ANDROID_PRODUCT_OUT/data
fi
#创建必要的设备节点
echo;echo;echoColor "***** 创建必要的设备节点 " GREEN
mkdir /nfsroot/$rootfs_name/dev/ -p
sudo mknod /nfsroot/$rootfs_name/dev/console c 5 1
#sudo mknod console c 5 1
#创建必要的链接
echo;echo;echoColor "*****创建必要链接 " GREEN
ln -svf /system/etc /nfsroot/$rootfs_name/etc
#
#拷贝必要的文件到 system/ data 目录 下
echo;echo;echoColor "*****拷贝必要的文件到 system / data 目录 下 " GREEN
ac_select=system
if [ "$ac_select" = "data" ]; then
if test -d /nfsroot/$rootfs_name/system/media; then
rm /nfsroot/$rootfs_name/system/media -rf
fi
ln -svf /data/media /nfsroot/$rootfs_name/system/media
mkdir /nfsroot/$rootfs_name/data/media -pv
cp vendor/marvell/littleton/media/audio /nfsroot/$rootfs_name/data/media -rf
#添加busybox
add_busybox
cp $ANDROID_BUILD_TOP/out/target/product/littleton/symbols/system/bin/sqlite3 /nfsroot/$rootfs_name/data/bin -rf
find /nfsroot/$rootfs_name/data/media/ -name ".svn"| xargs rm -rf
else
if test -L /nfsroot/$rootfs_name/system/media; then
rm /nfsroot/$rootfs_name/system/media -rf
fi
rm /nfsroot/$rootfs_name/system/media/ -rf
mkdir /nfsroot/$rootfs_name/system/media -pv
cp vendor/marvell/littleton/media/audio /nfsroot/$rootfs_name/system/media -rf
find /nfsroot/$rootfs_name/system/media/ -name ".svn"| xargs rm -rf
#如果存在 /system/bin/busybox 那么创建一些必要的应用
add_links_to_busybox
fi
#cp vendor/marvell/littleton/media/songs /nfsroot/$rootfs_name/data/ -rfv
#默认内核路径
if [ "$KERNEL_DIR" = "" ]; then
choosevariant
KERNEL_DIR=`get_match_parameter2 $seleted_item`
#KERNEL_DIR=`question "输入内核源码路径,默认使用:" 2 ${defaut_kernel_dir}`
if test ! -d $KERNEL_DIR ; then
echoColor "*****内核:$KERNEL_DIR 不存在!" RED
echoColor "*****跳过:$ANDROID_PRODUCT_OUT/root 拷贝" RED
echoColor "或者执行命令: KERNEL_DIR=\"path to kernel dir\" $0 " RED
else
echo;echoColor "*****拷贝 initramfs 到内核 $KERNEL_DIR/root 目录 " GREEN
if test ! -d $KERNEL_DIR/root/.svn; then
echoColor "不存在 $KERNEL_DIR/root/.svn" RED
rm $KERNEL_DIR/root -rfv
svn update $KERNEL_DIR/root
fi
cp $ANDROID_PRODUCT_OUT/root $KERNEL_DIR/ -rfv
ac_info=`svn st $KERNEL_DIR/root/ |grep M`
echoColor "ac_info=$ac_info" RED
if [ "$ac_info" != "" ]; then
result=`question "是否提交 $KERNEL_DIR/root " 1 yes`
if [ "$result" = yes ] ; then
echo "svn commit $KERNEL_DIR/root/"
fi
fi
fi
fi
result=`question "是否删除/data/目录下的缓存文件" 1 yes`
if [ "$result" = yes ] ; then
sudo rm /nfsroot/$rootfs_name/data/* -rf
fi
#创建链接 /nfsroot/rootfs
if test -L /nfsroot/rootfs ; then
rm /nfsroot/rootfs -rf
echoColor "*****删除原有链接 /nfsroot/rootfs " GREEN
else
if test -d /nfsroot/rootfs ; then
mv /nfsroot/rootfs /nfsroot/rootfs${ac_date}-${ac_time}
echoColor "*****原有的: /nfsroot/rootfs 备份为:/nfsroot/rootfs${ac_date}-${ac_time} " GREEN
fi
fi
echoColor "*****创建指向 /nfsroot/$rootfs_name 的链接 /nfsroot/rootfs " GREEN
ln -sv /nfsroot/$rootfs_name /nfsroot/rootfs
#去掉 /nfsroot/$rootfs_name/init.rc 中的 mount项来支持网络启动
sed_config_initrc /nfsroot/$rootfs_name/
result=`question "是否重启nfs服务器?如果网络文件系统实际位置有变更,必须重启!" 1 yes`
if [ "$result" = "yes" ] ; then
sudo /etc/init.d/nfs-kernel-server restart
fi
#sudo /etc/init.d/nfs-kernel-server restart
#补充的临时操作
#使用李照提供的输入法apk
#cp /opt/shared/lizhao/ime_new/BbkPinyinIME.apk /nfsroot/$rootfs_name/system/app -rfv
#cp /opt/shared/ime_0924/BbkPinyinIME.apk /nfsroot/$rootfs_name/system/app -rfv
#sqlite3_cupcake_init.sh /nfsroot/rootfs
exit
}
#>>>>>>>>>>scm配置管理相关
function is_svn_working_copy()
{
if test -d $1/.svn ;then
return 0
else
return 1
fi
}
function sh_get_svn_url()
{
local ac_url=
if is_svn_working_copy $1; then
ac_url=`svn info $1 |grep "URL:" |awk '{print $2}'`
fi
echo $ac_url
}
function sh_get_svn_url_root()
{
local ac_url_root=
if is_svn_working_copy $1; then
ac_url_root=`svn info $1 |grep "版本库根:" |awk '{print $2}'`
fi
echo $ac_url_root
}
function sh_get_svn_committed_no_from_url()
{
local ac_return=
unset_color
if [ -n "$1" ]; then
ac_return=`svn info $1 |grep "最后修改的版本:" |awk '{print $2}'`
fi
echo $ac_return
}
function sh_get_svn_committed_no_from_copy()
{
local ac_return=
unset_color
ac_url=`sh_get_svn_url $1`
echo "ac_url=$ac_url">&2
if [ -n "$ac_url" ]; then
ac_return=`svn info $1 |grep "最后修改的版本:" -w |awk '{print $2}'`
fi
echo "ac_return=$ac_return">&2
echo $ac_return
}
function sh_get_svn_head_no_from_url()
{
local ac_return=
unset_color
if [ -n "$1" ]; then
ac_return=`svn info $1 |grep "版本:" -w |awk '{print $2}'`
fi
echo $ac_return
}
function print_svn_version_info()
{
for x in $1
do
if [[ $x == r* ]] ; then
svn_version_info=$x
echo "$svn_version_info"
return
fi
done
echo "$svn_version_info"
return
}
#从log信息中获取版本号码
function get_svn_no_from_log()
{
for x in $1
do
if [[ $x == r* ]] ; then
tmp=`expr substr $x 2 5`
if IsNumeric $tmp;then
ac_svn_no=$tmp
break
fi
fi
done
echo "$ac_svn_no"
}
#从log信息中获取版本号码
function get_author_from_svn_log()
{
unset_color
local ac_author=
if [ -z $1 ] || [ -z $2 ]; then
echo $ac_author
return
fi
ac_author=`svn log -r $2 $1 --xml|grep author -w`
ac_author=`echo $ac_author |sed "s#author##g" |tr -d '\<\>\/'`
echo "$ac_author"
}
#从log信息中获取版本号码
function get_date_from_svn_log()
{
unset_color
local ac_return=
if [ -z $1 ] || [ -z $2 ]; then
echo $ac_return
return
fi
ac_return=`svn log -r $2 $1 --xml|grep date -w`
ac_return=`echo $ac_return |sed "s#date##g" |tr -d '\<\>\/'`
echo "$ac_return"
}
function get_files_from_svn_diff()
{
unset_color
local files_list=
if [ -z $1 ] || [ -z $2 ]; then
echo $files_list
return
fi
`svn diff -r $2 $1`
}
#指明: 路径 [commited|head|prev] 等获取版本号
function get_svn_version_no_from_working_copy()
{
ac_dir=$1
if test ! -d $ac_dir; then
return
fi
if test ! -d $ac_dir/.svn; then
return
fi
if [ "$2" = "" ] ; then
ac_r_str=committed
else
ac_r_str=$2
fi
ac_svn_log="`svn log -r $ac_r_str $ac_dir`"
#echo "get_svn_no_from_log \"$ac_svn_log\"" >&2
ac_svn_no="`get_svn_no_from_log \"$ac_svn_log\"`"
if [ "$ac_svn_no" = "" ]; then
ac_r_str=committed
ac_svn_log="`svn log -r $ac_r_str $ac_dir`"
ac_svn_no="`get_svn_no_from_log \"$ac_svn_log\"`"
fi
echo $ac_svn_no
}
function get_svn_version_no_from_working_copy_max()
{
_tmp_version=$1
_max_version=`echo $_tmp_version |awk -F ':' '{print $2}'`
echo $_max_version
}
function get_svn_version_no_from_working_copy_min()
{
local _tmp_version=$1
_min_version=`echo $_tmp_version |awk -F ':' '{print $1}'`
echo $_min_version
}
function get_svn_version_no_from_working_copy_max()
{
local _tmp_version=$1
_max_version=`echo $_tmp_version |awk -F ':' '{print $2}'`
echo $_max_version
}
function sh_get_svn_version_min_from_string()
{
local _tmp_version=$1
_min_version=`echo $_tmp_version |awk -F ':' '{print $1}'`
echo $_min_version
}
function sh_get_svn_version_max_from_string()
{
local _tmp_version=$1
_max_version=`echo $_tmp_version |awk -F ':' '{if($2==""){print $1} else {print $2}}'`
echo $_max_version
}
#>>>>>>>>>>scm配置管理相关end
#>>>>>>>>>>分布式编译相关
#>>>>>>>>>>默认值
#172.20.120.87 叶振华 64位
#172.20.120.165 吴爱军 32位
#172.20.120.167 邓伟成 32位
remote_distcc_hosts_conf=http://172.20.127.15/update/hosts.conf
if sh_at_home; then
remote_distcc_hosts_conf=http://`sh_get_host_ip`/update/hosts.conf
fi
local_distcc_hosts_conf=/tmp/hosts.conf
#<<<<<<<<<<
function sh_get_hosts_sppourt_distcc()
{
local result=
rm $local_distcc_hosts_conf -rf
cd /tmp/; wget $remote_distcc_hosts_conf; cd - >&2
if test -f $local_distcc_hosts_conf; then
. $local_distcc_hosts_conf
fi
if [ "${hosts_list_32}" = "" ]; then
hosts_list_32=localhost
fi
if [ "${hosts_list_64}" = "" ]; then
hosts_list_64=localhost
fi
result="--randomize ${hosts_list_64}"
if sh_system_is_i386; then
result="--randomize ${hosts_list_32}"
fi
echo $result >&2
echo $result
}
##
function sh_scan_hosts_sppourt_distcc()
{
max_host_num=255
for (( i = 60; i < 200; i++));
do
HOST=`lsdistcc 172.20.120.$i`
if [ "$HOST" != "" ]; then
HOSTS="$HOST $HOSTS"
fi
echo "HOSTS=$HOSTS" >&2
done
}
#$1 提示信息 $2数组 $3 默认选项
#默认选项从“1”开始计数,
#调用示例 : list=(a b c d e f g)
#调用示例 : choosevariant "该文件夹下的版本库有:" list 2
#调用示例 : echo "you choose $?"
#获得返回值使用aa=`choosevariant "该文件夹下的版本库有:" list 2`
function ChooseVariant(){
local index=1
local message=$1
eval parameter_list=(\${$2[@]})
local default_value=$3
local ANSWER=
local v
echoText "$message 默认选项为[ $default_value ]" RED >&2
for v in "${parameter_list[@]}"
do
echo " $index. $v" >&2
index=$(($index+1))
done
while [ 1 ]
do
echoText "请选择! 默认选项为: [ $default_value ] " GREEN >&2
echo -n "enter number:" >&2
read ANSWER
if [ -z "$ANSWER" ] ; then
ANSWER=$default_value
break;
elif (echo -n $ANSWER | grep -q -e "^[0-9][0-9]*$") ; then
if [ $ANSWER -le ${#parameter_list[@]} -a $ANSWER -gt 0 ] ; then
# MATCH=$ANSWER
break;
fi
fi
done
# echo "$ANSWER" >&2
echo ${parameter_list[$(( $ANSWER - 1 ))]}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment