BASH 文本模版的简单实现 micro_template_compile

Shell模板替换脚本

详细代码

###############################
#
# Funciton: micro_template_compile
#
# Parameter:
#    [1] => template :String
#    [2..n] => values for placeholder as key=value
#
# Example:
#    <- micro_template_compile '{code: {{error.code}}, message: "{{error.message}}"}' "error.code=127" "error.message=command not found."
#       ---
#    -> { code: 0, message: "" }
###############################
function micro_template_compile() {

  template_string="$1"; shift;
  expression=""

  while [ $# -ne 0 ]; do
    key=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\1~g')
    value=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\2~g')
    expression="s~{{$key}}~$value~g;$expression"
    shift;
  done

  echo "$template_string" | sed "$expression"
}



使用案例

运行

micro_template_compile 'Hi {{name}}, it is {{date}} today and {{weather}} outside. :)
message to you: {{message}}' \
  name='小王' date="$(date +"%Y/%m/%d")" weather="大晴天儿" message="咱们出去钓鱼吧。"


输出

Hi 小王, it is 2014/06/16 today and 大晴天儿 outside. :)
message to you: 咱们出去钓鱼吧!



使用到脚本中:

#!/bin/bash

#
# Usage: $exename [options] -in templatefile key=value ...
#
# Examples:
#   $exename -in ./1.txt.template -out ./1.txt f1=v1 f2=v2 f3=v3
#   $exename -help
#

###############################
#
# Funciton: micro_template_compile
#
# Parameter:
#    [1] => template :String
#    [2..n] => values for placeholder as key=value
#
# Example:
#    <- micro_template_compile '{code: {{error.code}}, message: "{{error.message}}"}' "error.code=127" "error.message=command not found."
#       ---
#    -> { code: 0, message: "" }
###############################
function micro_template_compile() {

  local template_string="$1"; shift;
  local expression=""
  local key=""
  local value=""

  while [ $# -ne 0 ]; do
    key=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\1~g')
    value=$(echo "$1" | sed 's~^\([^=]*\)=\(.*\)$~\2~g')
    expression="s~{{$key}}~$value~g;$expression"
    shift;
  done

  echo "$template_string" | sed "$expression"
}


main() {
  template="$(cat "$arg_in")"
  eval "micro_template_compile '$template' $arg_datagroup > '$arg_out'"
  return 0
}

processargs() {
  # defaults:
  arg_in=""
  arg_out="stdout"
  arg_datagroup=""

  # arguments:
  while echo "$1" | grep "^-" >/dev/null 2>&1; do
    case "$1" in
      -in)
        arg_in="$2"; shift;
        ;;
      -out)
        arg_out="$2"; shift;
        ;;
    esac
    shift
  done

  while [ $# -ne 0 ]; do
    arg_datagroup="$arg_datagroup \"$1\""; shift;
  done

  # exports
  export arg_in arg_out arg_datagroup
}

processargs "$@"
echo ""
echo "arg_in=|$arg_in|"
echo "arg_out=|$arg_out|"
echo "arg_datagroup=|$arg_datagroup|"
echo ""

main



转载于:https://www.cnblogs.com/blfbuaa/p/6962160.html

luckes@luckes-Legion-Y7000P-IRX9:~/PX4-Autopilot$ make px4_sitl_default # 重新 编译 -- PX4 version: v1.14.0 (1.14.0) -- Found PythonInterp: /usr/local/bin/python3 (found suitable version "3.10.9", minimum required is "3") -- PX4 config file: /home/luckes/PX4-Autopilot/boards/px4/sitl/default.px4board -- PLATFORM posix -- ROMFSROOT px4fmu_common -- ROOTFSDIR . -- TESTING y -- ETHERNET y -- PX4 config: px4_sitl_default -- PX4 platform: posix -- PX4 lockstep: enabled -- The CXX compiler identification is GNU 9.4.0 -- The C compiler identification is GNU 9.4.0 -- The ASM compiler identification is GNU -- Found assembler: /usr/bin/cc -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- cmake build type: RelWithDebInfo -- Could NOT find gz-transport (missing: gz-transport_DIR) -- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") -- Checking for module 'bullet>=2.82' -- Found bullet, version 2.88 -- Found ccd: /usr/include (found suitable version "2.0", minimum required is "2.0") -- Found fcl: /usr/include (found suitable version "0.5.0", minimum required is "0.3.2") -- Found assimp: /usr/include (found version "5.0.0") -- Found DART: /usr/include (Required is at least version "6.6") found components: dart -- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.71.0/BoostConfig.cmake (found suitable version "1.71.0", minimum required is "1.40.0") found components: thread system filesystem program_options regex iostreams date_time -- Found Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread (found version "3.6.1") -- Looking for ignition-math6 -- found version 6.15.1 -- Searching for dependencies of ignition-math6 -- Looking for OGRE... -- OGRE_PREFIX_WATCH changed. -- Checking for module 'OGRE' -- Found OGRE, version 1.9.0 -- Found Ogre Ghadamon (1.9.0) -- Found OGRE: optimized;/usr/lib/x86_64-linux-gnu/libOgreMain.so;debug;/usr/lib/x86_64-linux-gnu/libOgreMain.so -- Looking for OGRE_Paging... -- Found OGRE_Paging: optimized;/usr/lib/x86_64-linux-gnu/libOgrePaging.so;debug;/usr/lib/x86_64-linux-gnu/libOgrePaging.so -- Looking for OGRE_Terrain... -- Found OGRE_Terrain: optimized;/usr/lib/x86_64-linux-gnu/libOgreTerrain.so;debug;/usr/lib/x86_64-linux-gnu/libOgreTerrain.so -- Looking for OGRE_Property... -- Found OGRE_Property: optimized;/usr/lib/x86_64-linux-gnu/libOgreProperty.so;debug;/usr/lib/x86_64-linux-gnu/libOgreProperty.so -- Looking for OGRE_RTShaderSystem... -- Found OGRE_RTShaderSystem: optimized;/usr/lib/x86_64-linux-gnu/libOgreRTShaderSystem.so;debug;/usr/lib/x86_64-linux-gnu/libOgreRTShaderSystem.so -- Looking for OGRE_Volume... -- Found OGRE_Volume: optimized;/usr/lib/x86_64-linux-gnu/libOgreVolume.so;debug;/usr/lib/x86_64-linux-gnu/libOgreVolume.so -- Looking for OGRE_Overlay... -- Found OGRE_Overlay: optimized;/usr/lib/x86_64-linux-gnu/libOgreOverlay.so;debug;/usr/lib/x86_64-linux-gnu/libOgreOverlay.so -- Looking for ignition-math6 -- found version 6.15.1 -- Looking for ignition-transport8 -- found version 8.5.0 -- Searching for dependencies of ignition-transport8 -- Config-file not installed for ZeroMQ -- checking for pkg-config -- Checking for module 'libzmq >= 4' -- Found libzmq , version 4.3.2 -- Found ZeroMQ: TRUE (Required is at least version "4") -- Checking for module 'uuid' -- Found uuid, version 2.34.0 -- Found UUID: TRUE -- Looking for ignition-msgs5 -- found version 5.11.0 -- Searching for dependencies of ignition-msgs5 -- Found Protobuf: /usr/lib/x86_64-linux-gnu/libprotobuf.so;-lpthread (found suitable version "3.6.1", minimum required is "3") -- Looking for ignition-math6 -- found version 6.15.1 -- Checking for module 'tinyxml2' -- Found tinyxml2, version 6.2.0 -- Looking for ignition-msgs5 -- found version 5.11.0 -- Looking for ignition-common3 -- found version 3.17.0 -- Searching for dependencies of ignition-common3 -- Looking for dlfcn.h - found -- Looking for libdl - found -- Found DL: TRUE -- Searching for <ignition-common3> component [graphics] -- Looking for ignition-common3-graphics -- found version 3.17.0 -- Searching for dependencies of ignition-common3-graphics -- Looking for ignition-math6 -- found version 6.15.1 -- Looking for ignition-fuel_tools4 -- found version 4.9.1 -- Searching for dependencies of ignition-fuel_tools4 -- Found CURL: /usr/lib/x86_64-linux-gnu/libcurl.so (found version "7.68.0") -- Checking for module 'jsoncpp' -- Found jsoncpp, version 1.7.4 -- Found JSONCPP: TRUE -- Checking for module 'yaml-0.1' -- Found yaml-0.1, version 0.2.2 -- Found YAML: TRUE -- Checking for module 'libzip' -- Found libzip, version 1.5.1 -- Found ZIP: TRUE -- Looking for ignition-common3 -- found version 3.17.0 -- Looking for ignition-math6 -- found version 6.15.1 -- Looking for ignition-msgs5 -- found version 5.11.0 -- Found gazebo-classic 11.15.1, including sitl_gazebo-classic simulator and gazebo-classic targets -- Could NOT find Java (missing: Java_JAVA_EXECUTABLE Java_JAR_EXECUTABLE Java_JAVAC_EXECUTABLE Java_JAVAH_EXECUTABLE Java_JAVADOC_EXECUTABLE) -- ROMFS: ROMFS/px4fmu_common Architecture: amd64 ==> CPACK_INSTALL_PREFIX = @DEB_INSTALL_PREFIX@ -- Configuring done -- Generating done -- Build files have been written to: /home/luckes/PX4-Autopilot/build/px4_sitl_default Scanning dependencies of target events_header Scanning dependencies of target flighttasks_generated Scanning dependencies of target drivers_board Scanning dependencies of target uorb_headers [ 0%] Generating combined event json file [ 0%] Generating Flight Tasks [ 0%] Generating uORB topic headers [ 0%] Building CXX object boards/px4/sitl/src/CMakeFiles/drivers_board.dir/board_shutdown.cpp.o [ 0%] Generating px4 event header file Traceback (most recent call last): File "/home/luckes/PX4-Autopilot/Tools/msg/px_generate_uorb_topic_files.py", line 249, in <module> generate_output_from_file(generate_idx, f, args.outputdir, args.package, args.templatedir, INCL_DEFAULT) File "/home/luckes/PX4-Autopilot/Tools/msg/px_generate_uorb_topic_files.py", line 166, in generate_output_from_file return generate_by_template(output_file, template_file, em_globals) File "/home/luckes/PX4-Autopilot/Tools/msg/px_generate_uorb_topic_files.py", line 182, in generate_by_template em.RAW_OPT: True, em.BUFFERED_OPT: True}) AttributeError: module 'em' has no attribute 'RAW_OPT' [ 0%] Built target flighttasks_generated make[3]: *** [msg/CMakeFiles/uorb_headers.dir/build.make:431:uORB/topics/action_request.h] 错误 1 make[3]: *** 正在删除文件“uORB/topics/action_request.h” make[2]: *** [CMakeFiles/Makefile2:7357:msg/CMakeFiles/uorb_headers.dir/all] 错误 2 make[2]: *** 正在等待未完成的任务.... Scanning dependencies of target git_micro_xrce_dds_client [ 0%] Building C object boards/px4/sitl/src/CMakeFiles/drivers_board.dir/sitl_led.c.o [ 0%] git submodule src/modules/uxrce_dds_client/Micro-XRCE-DDS-Client [ 0%] Built target git_micro_xrce_dds_client [ 0%] Linking CXX static library libdrivers_board.a [ 0%] Built target events_header [ 0%] Built target drivers_board make[1]: *** [Makefile:152:all] 错误 2 make: *** [Makefile:227:px4_sitl_default] 错误 2
最新发布
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值