前言
学了一个星期脚本编程,该写安装程序了。 还有点前置任务要做。
安装时,假设编译了几个不同版本的目标程序集合,e.g. 32bits, 64bits; redhat or debian; gcc have or not; glibc version depend.
安装时,要判断依赖条件是否满足,安装包内是否有适合的目标程序集合可以安装。 这就要先得到当前linux系统的信息,gcc版本,glibc版本。 捣鼓了一天,封装了几个函数,将取信息和比较信息的测试代码写好了
运行效果
>> func_test, test case
for ./media/helper/sh.os_info
this OS is linux
func_is_64bits_os() = [64bits]
func_get_os_name() = [Debian]
func_get_os_ver() = [
7.5]
func_get_gcc_ver() = [
4.7.2]
func_get_glibc_ver() = [
2.13]
install now, please wait
...
install over, thank you :)
实验
#!/bin/bash
. ../helper/sh.os_info
MIN_GCC_VER=
"3.0"
MIN_LIBC_VER=
"2.09"
function
func_test() {
local rc=
0
local str_tmp=
""
local str_gcc_ver=
""
local str_glibc_ver=
""
clear
echo -e
echo -e ">> func_test, test case for ./media/helper/sh.os_info"
echo -e
func_is_linux_os
rc=$?
if [
$rc -eq 0 ]
then
str_tmp=
"this OS is linux"
echo "$str_tmp"
else
str_tmp=
"install error : this OS isn't linux, installer can't be run"
echo "$str_tmp"
return 255
fi
func_is_64bits_os
rc=$?
if [
$rc -eq 0 ]
then
str_tmp=
"64bits"
echo -e "func_is_64bits_os() = [$str_tmp]"
else
str_tmp=
"not 64bits OS"
echo -e "func_is_64bits_os() = [$str_tmp]"
str_tmp=
"install error : this OS isn't 64bits linux, installer can't be run"
echo "$str_tmp"
return 254
fi
echo -e "func_get_os_name() = [$(func_get_os_name)]"
echo -e "func_get_os_ver() = [$(func_get_os_ver)]"
str_gcc_ver=$(func_get_gcc_ver)
echo -e "func_get_gcc_ver() = [$str_gcc_ver]"
if [[
"$str_gcc_ver" <
"$MIN_GCC_VER" ]]
then
str_tmp=
"install error : gcc version need >= $MIN_GCC_VER"
echo "$str_tmp"
return 253
fi
str_glibc_ver=$(func_get_glibc_ver)
echo -e "func_get_glibc_ver() = [$str_glibc_ver]"
if [[
"$str_glibc_ver" <
"$MIN_LIBC_VER" ]]
then
str_tmp=
"install error : glibc version need >= $MIN_LIBC_VER"
echo "$str_tmp"
return 252
fi
echo "install now, please wait ..."
sleep
6
echo "install over, thank you :)"
}
func_test
exit 0
#!/bin/bash
function
func_remove_space() {
local rv=$(
echo -e $1 | sed
"s/ //")
echo "$rv"
return 0
}
function
func_get_os_name() {
local rv=$(lsb_release -i)
rv=$(
echo -e "$rv" | awk -F:
' { print $2 } ')
echo "$(func_remove_space $rv)"
return 0
}
function
func_get_os_ver() {
echo "$(lsb_release -s -r)"
return 0
}
function
func_is_linux_os() {
local rc=-
1
local rv=$(uname
-s)
if [
"$rv" ==
"Linux" ]
then
rc=
0
fi
return $rc
}
function
func_is_64bits_os() {
local rc=-
1
local rv=$(uname -m)
if [
"$rv" ==
"x86_64" ]
then
rc=
0
fi
return $rc
}
function
func_get_gcc_ver() {
local rv=$(gcc --version)
rv=$(
echo -e "$rv" | sed -n
'1p')
rv=$(
echo -e "$rv" | awk -F
' ' '{ print $NF }')
echo "$rv"
return 0
}
function
func_get_glibc_ver() {
local rv=$(lsof -p $$ | grep libc-)
rv=$(
echo -e "$rv" | awk -F
' ' '{ print $NF }')
rv=$(
echo "$rv --version")
rv=$(
eval "$rv")
rv=$(
echo -e "$rv" | sed -n
'1p')
rv=$(
echo -e "$rv" | awk -F
"release version " '{ print $2 }')
rv=$(
echo -e "$rv" | awk -F
"," '{ print $1 }')
echo "$rv"
return 0
}