您的位置:首页 > 运维架构 > Shell

通过shell脚本生成C代码例程

2016-08-16 10:29 435 查看
通过shell脚本生成C代码例程:

#!/bin/sh

target=$1

BUILDER_VERSION_BRANCH=${VERSION_BRANCH}

BUILDER_VERSION_MAJOR=${VERSION_MAJOR}
BUILDER_VERSION_MINOR=${VERSION_MINOR}
BUILDER_VERSION_EXTRA=${VERSION_EXTRA}
BUILDER_VERSION_REVNO=${VERSION_REVNO}
BUILDER_VERSION_STAGE=${VERSION_STAGE}

BUILDER_CMPL_YEAR=`date +%Y | sed -e 's/^0//g'`
BUILDER_CMPL_MONTH=`date +%m | sed -e 's/^0//g'`
BUILDER_CMPL_DATE=`date +%d | sed -e 's/^0//g'`
BUILDER_CMPL_TIME="`date +%T`"

echo "/* Automatically generated file; DO NOT EDIT. */" > $target

echo "char *builder_version_branch(void)" >> $target
echo "{" >> $target
echo " return \"${BUILDER_VERSION_BRANCH}\";" >> $target
echo "}" >> $target

echo "unsigned int builder_version_major(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_VERSION_MAJOR};" >> $target
echo "}" >> $target

echo "unsigned int builder_version_minor(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_VERSION_MINOR};" >> $target
echo "}" >> $target

echo "unsigned int builder_version_extra(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_VERSION_EXTRA};" >> $target
echo "}" >> $target

echo "unsigned int builder_version_revno(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_VERSION_REVNO};" >> $target
echo "}" >> $target

echo "char *builder_version_stage(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_VERSION_STAGE};" >> $target
echo "}" >> $target

echo "unsigned int builder_cmpl_year(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_CMPL_YEAR};" >> $target
echo "}" >> $target

echo "unsigned int builder_cmpl_month(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_CMPL_MONTH};" >> $target
echo "}" >> $target

echo "unsigned int builder_cmpl_date(void)" >> $target
echo "{" >> $target
echo " return ${BUILDER_CMPL_DATE};" >> $target
echo "}" >> $target

echo "char *builder_cmpl_time(void)" >> $target
echo "{" >> $target
echo " return \"${BUILDER_CMPL_TIME}\";" >> $target
echo "}" >> $target

执行上面的shell。
./builder.sh abc.c

生成一下C代码:

/* Automatically generated file; DO NOT EDIT. */
char *builder_version_branch(void)
{
return "";
}
unsigned int builder_version_major(void)
{
return ;
}
unsigned int builder_version_minor(void)
{
return ;
}
unsigned int builder_version_extra(void)
{
return ;
}
unsigned int builder_version_revno(void)
{
return ;
}
char *builder_version_stage(void)
{
return ;
}
unsigned int builder_cmpl_year(void)
{
return 2016;
}
unsigned int builder_cmpl_month(void)
{
return 8;
}
unsigned int builder_cmpl_date(void)
{
return 16;
}
char *builder_cmpl_time(void)
{
return "10:26:48";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell linux c