一、简介
  因经常需要对不同机器配置主从机(不同机器的主机名一致,IP地址为 “192.168.101.*** ”格式),故整理此脚本来提高自己频繁切换机器和主从机/单机模式的切换效率,进行一个命令行配置/取消主从机模式。
二、配置步骤
1.编辑脚本文件
  脚本文件名为 set_master_slave.sh,内容如下:
#!/bin/bash
# Author:   chatgpt
# Verifier: ZYG
# Date:     2023.06.29
ip_pre="192.168.101."
ip_master_post="143"
ip_slave_post="145"
ip_master=${ip_pre}${ip_master_post}
ip_slave=${ip_pre}${ip_slave_post}
remote_name="robot"
locate_name="gene"
edit_files() {
  # Check if ~/.bashrc contains existing ROS master and slave configuration
  if grep -qE "ROS_MASTER_URI|ROS_HOSTNAME" ~/.bashrc; then
    # Remove existing ROS master and slave configuration
    sed -i '/export ROS_MASTER_URI=/d' ~/.bashrc
    sed -i '/export ROS_HOSTNAME=/d' ~/.bashrc
    #echo "Existing ROS master and slave configuration removed from ~/.bashrc."
  fi
  # Check if /etc/hosts contains existing ROS master and slave configuration
  if grep -qE ".*${remote_name}|.*${locate_name}" /etc/hosts; then
    # Remove existing ROS master and slave configuration
    sudo sed -i -E "/.*(${remote_name}|${locate_name})/d" /etc/hosts
    #echo "Existing ROS master and slave configuration removed from /etc/hosts."
  fi
  # Configuring ROS remote master IP
  echo "export ROS_MASTER_URI=http://$1:11311" >> ~/.bashrc
  # Configuring ROS local slave IP
  echo "export ROS_HOSTNAME=$2" >> ~/.bashrc
  # Update /etc/hosts file
  if grep -qE "${remote_name}|${locate_name}" /etc/hosts; then
    # Remove existing ROS master and slave configuration
    sudo sed -i -E "/(${remote_name}|${locate_name})/d" /etc/hosts
  fi
  echo "$1 ${remote_name}" | sudo tee -a /etc/hosts >/dev/null
  echo "ROS master and slave configuration updated."
  echo "ROS master: ${remote_name} ${ip_master}"
  echo "ROS slave:  ${locate_name} ${ip_slave}"
}
# Check the number of arguments
if [[ $# -eq 2 ]]; then
  # Two arguments provided, configure ROS master and slave IPs
  ip_master=${ip_pre}$1
  ip_slave=${ip_pre}$2
  edit_files ${ip_master} ${ip_slave}
elif [[ $# -eq 1 ]]; then
  if [ "$1" == "n" ]; then
    # Check if ~/.bashrc contains existing ROS master and slave configuration
    if grep -qE "ROS_MASTER_URI|ROS_HOSTNAME" ~/.bashrc; then
      # Remove existing ROS master and slave configuration
      sed -i '/export ROS_MASTER_URI=/d' ~/.bashrc
      sed -i '/export ROS_HOSTNAME=/d' ~/.bashrc
      echo "Removed ROS master and slave configuration from ~/.bashrc."
    fi
    # Check if /etc/hosts contains existing ROS master and slave configuration
    if grep -qE ".*${remote_name}|.*${locate_name}" /etc/hosts; then
      # Remove existing ROS master and slave configuration
      sudo sed -i -E "/.*(${remote_name}|${locate_name})/d" /etc/hosts
      echo "Removed ROS master and slave configuration from /etc/hosts."
    fi
  else
    # One argument provided, configure default ROS master and slave IPs
    ip_master=${ip_pre}$1
    #ip_slave=${ip_pre}$1
    edit_files ${ip_master} ${ip_slave}
  fi
else
  # No arguments provided
  echo "Invalid number of arguments."
  echo "Usage: bash set_master_slave.sh [master_ip] [slave_ip]"
  echo "Example: bash set_master_slave.sh 100 143"
  exit 1
fi
# Source the updated ~/.bashrc file
source ~/.bashrc
内容适配说明
- ip_pre=“192.168.101.” 修改成需要的IP前缀
- remote_name=“robot” 修改成远程主机名,作为ROS主机
- locate_name=“gene” 修改成本地主机名,作为ROS从机
2.脚本使用说明
2.1 配置主从机模式
2.1.1双参数
命令格式:./set_master_slave.sh [master_ip] [slave_ip]
 具体用法:./set_master_slave.sh 125 145
 实例:
 
2.1.2 单参数
命令格式:./set_master_slave.sh [master_ip]
 具体用法:./set_master_slave.sh 116
 实例:
 
2.2 取消主从机模式
命令格式:./set_master_slave.sh n (后跟参数是字母n)
 具体用法:./set_master_slave.sh n
 实例:
 
3.小建议
可以将该脚本文件放在一不常翻看的文件路径下,结合使用别名的形式,打开终端即可使用,上述实例即是取别名的形式调用,真香~


















