• ros2学习笔记:shell环境变量脚本setup.bash[-z][-n][-f]参数作用


    -n作用

    [ -n 字符串 ] or [ 字符串 ]  字符串的长度为非零(有内容)则为真。加-n与不加-n结果相同。

    -z作用

    [ -z 字符串 ] 字符串的长度为零则为真。 字符串为空即NULL时为真,与上面的-n相反。

    -f作用

    [ -f FILE ]  如果 FILE 存在且是一个普通文件则为真。 

    ros系统环境为ros2  foxy。

    系统自带的包source的第一个系统脚本文件

    /opt/ros/foxy/setup.bash

    1. # copied from ament_package/template/prefix_level/setup.bash
    2. AMENT_SHELL=bash
    3. # source setup.sh from same directory as this file
    4. AMENT_CURRENT_PREFIX=$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" && pwd)
    5. # trace output
    6. if [ -n "$AMENT_TRACE_SETUP_FILES" ]; then
    7. echo "# . \"$AMENT_CURRENT_PREFIX/setup.sh\""
    8. fi
    9. . "$AMENT_CURRENT_PREFIX/setup.sh"

    通过在终端显示这个变量验证,$AMENT_TRACE_SETUP_FILES没有内容。

      [ -n "$AMENT_TRACE_SETUP_FILES" ]没有内容,if fi内代码不运行,直接跳到最后一句执行

    . "$AMENT_CURRENT_PREFIX/setup.sh"

     这个脚本文件的意思是运行同一个文件夹内的setup.sh文件,转入运行下面的脚本。

    /opt/ros/foxy/setup.sh

    1. # generated from ament_package/template/prefix_level/setup.sh.in
    2. # since this file is sourced use either the provided AMENT_CURRENT_PREFIX
    3. # or fall back to the destination set at configure time
    4. : ${AMENT_CURRENT_PREFIX:=/opt/ros/foxy}
    5. # set type of shell if not already set
    6. : ${AMENT_SHELL:=sh}
    7. # function to append non-duplicate values to environment variables
    8. # using colons as separators and avoiding leading separators
    9. ament_append_unique_value() {
    10. # arguments
    11. _listname=$1
    12. _value=$2
    13. #echo "listname $_listname"
    14. #eval echo "list value \$$_listname"
    15. #echo "value $_value"
    16. # check if the list contains the value
    17. eval _values=\$$_listname
    18. _duplicate=
    19. _ament_append_unique_value_IFS=$IFS
    20. IFS=":"
    21. if [ "$AMENT_SHELL" = "zsh" ]; then
    22. ament_zsh_to_array _values
    23. fi
    24. for _item in $_values; do
    25. # ignore empty strings
    26. if [ -z "$_item" ]; then
    27. continue
    28. fi
    29. if [ $_item = $_value ]; then
    30. _duplicate=1
    31. fi
    32. done
    33. unset _item
    34. # append only non-duplicates
    35. if [ -z "$_duplicate" ]; then
    36. # avoid leading separator
    37. if [ -z "$_values" ]; then
    38. eval $_listname=\"$_value\"
    39. #eval echo "set list \$$_listname"
    40. else
    41. # field separator must not be a colon
    42. unset IFS
    43. eval $_listname=\"\$$_listname:$_value\"
    44. #eval echo "append list \$$_listname"
    45. fi
    46. fi
    47. IFS=$_ament_append_unique_value_IFS
    48. unset _ament_append_unique_value_IFS
    49. unset _duplicate
    50. unset _values
    51. unset _value
    52. unset _listname
    53. }
    54. # iterate over all parent_prefix_path files
    55. _prefix_setup_IFS=$IFS
    56. IFS="
    57. "
    58. # this variable contains the concatenated prefix paths in reverse order
    59. _UNIQUE_PREFIX_PATH=""
    60. # this check is used to skip parent prefix path in the Debian package
    61. if [ -z "SKIP_PARENT_PREFIX_PATH" ]; then
    62. # find parent prefix path files for all packages under the current prefix
    63. _RESOURCES="$(\find "$AMENT_CURRENT_PREFIX/share/ament_index/resource_index/parent_prefix_path" -mindepth 1 -maxdepth 1 2> /dev/null | \sort)"
    64. if [ "$AMENT_SHELL" = "zsh" ]; then
    65. ament_zsh_to_array _RESOURCES
    66. fi
    67. for _resource in $_RESOURCES; do
    68. # read the content of the parent_prefix_path file
    69. _PARENT_PREFIX_PATH="$(\cat "$_resource")"
    70. # reverse the list
    71. _REVERSED_PARENT_PREFIX_PATH=""
    72. IFS=":"
    73. if [ "$AMENT_SHELL" = "zsh" ]; then
    74. ament_zsh_to_array _PARENT_PREFIX_PATH
    75. fi
    76. for _path in $_PARENT_PREFIX_PATH; do
    77. # replace placeholder of current prefix
    78. if [ "$_path" = "{prefix}" ]; then
    79. _path="$AMENT_CURRENT_PREFIX"
    80. fi
    81. # avoid leading separator
    82. if [ -z "$_REVERSED_PARENT_PREFIX_PATH" ]; then
    83. _REVERSED_PARENT_PREFIX_PATH=$_path
    84. else
    85. _REVERSED_PARENT_PREFIX_PATH=$_path:$_REVERSED_PARENT_PREFIX_PATH
    86. fi
    87. done
    88. unset _PARENT_PREFIX_PATH
    89. # collect all unique parent prefix path
    90. if [ "$AMENT_SHELL" = "zsh" ]; then
    91. ament_zsh_to_array _REVERSED_PARENT_PREFIX_PATH
    92. fi
    93. for _path in $_REVERSED_PARENT_PREFIX_PATH; do
    94. ament_append_unique_value _UNIQUE_PREFIX_PATH "$_path"
    95. done
    96. unset _REVERSED_PARENT_PREFIX_PATH
    97. done
    98. unset _resource
    99. unset _RESOURCES
    100. fi
    101. # append this directory to the prefix path
    102. ament_append_unique_value _UNIQUE_PREFIX_PATH "$AMENT_CURRENT_PREFIX"
    103. unset AMENT_CURRENT_PREFIX
    104. # store AMENT_SHELL to restore it after each prefix
    105. _prefix_setup_AMENT_SHELL=$AMENT_SHELL
    106. # source local_setup.EXT or local_setup.sh file for each prefix path
    107. IFS=":"
    108. if [ "$AMENT_SHELL" = "zsh" ]; then
    109. ament_zsh_to_array _UNIQUE_PREFIX_PATH
    110. fi
    111. for _path in $_UNIQUE_PREFIX_PATH; do
    112. # trace output
    113. if [ -n "$AMENT_TRACE_SETUP_FILES" ]; then
    114. echo "# . \"$_path/local_setup.$AMENT_SHELL\""
    115. fi
    116. if [ -f "$_path/local_setup.$AMENT_SHELL" ]; then
    117. if [ "$AMENT_SHELL" = "sh" ]; then
    118. # provide AMENT_CURRENT_PREFIX to .sh files
    119. AMENT_CURRENT_PREFIX=$_path
    120. fi
    121. # restore IFS before sourcing other files
    122. IFS=$_prefix_setup_IFS
    123. . "$_path/local_setup.$AMENT_SHELL"
    124. # restore AMENT_SHELL after each prefix-level local_setup file
    125. AMENT_SHELL=$_prefix_setup_AMENT_SHELL
    126. fi
    127. done
    128. unset _path
    129. IFS=$_prefix_setup_IFS
    130. unset _prefix_setup_IFS
    131. unset _prefix_setup_AMENT_SHELL
    132. unset _UNIQUE_PREFIX_PATH
    133. unset AMENT_SHELL

    我们自己生成的包source环境变量脚本 

    工作空间内 source install/setup.bash

    1. # generated from colcon_bash/shell/template/prefix_chain.bash.em
    2. # This script extends the environment with the environment of other prefix
    3. # paths which were sourced when this file was generated as well as all packages
    4. # contained in this prefix path.
    5. # function to source another script with conditional trace output
    6. # first argument: the path of the script
    7. _colcon_prefix_chain_bash_source_script() {
    8. if [ -f "$1" ]; then
    9. if [ -n "$COLCON_TRACE" ]; then
    10. echo ". \"$1\""
    11. fi
    12. . "$1"
    13. else
    14. echo "not found: \"$1\"" 1>&2
    15. fi
    16. }
    17. # source chained prefixes
    18. # setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
    19. COLCON_CURRENT_PREFIX="/opt/ros/foxy"
    20. _colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/local_setup.bash"
    21. # source this prefix
    22. # setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
    23. COLCON_CURRENT_PREFIX="$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)"
    24. _colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/local_setup.bash"
    25. unset COLCON_CURRENT_PREFIX
    26. unset _colcon_prefix_chain_bash_source_script

    $0当前脚本名 $1 脚本的第一个参数 

    _colcon_prefix_chain_bash_source_script(){}函数的意思是第一个参数文件存在就运行第一个参数,没有就报错。

    所以我们在自己的工作空间source install/setup.bash相当于同时source /opt/ros/foxy/local_setup.bash

    source install/local_setup.bash

  • 相关阅读:
    根据文档头判断文档后缀并修改
    RGB灯带蓝牙芯片智能化方案
    Python零基础入门-8 错误和异常
    数字化转型企业成功的关键,用数据创造价值
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    《SpringCloud Alibaba》实战
    【PB续命05】WinHttp.WinHttpRequest的介绍与使用
    本周的error记录
    Python-入门-安装和运行(二)
    MySQL函数(经典收藏)
  • 原文地址:https://blog.csdn.net/m0_73694897/article/details/132921889