Search code examples
bashshellros

Calling rosbag info from a bash script


When you call ROS rosbag info abag.bag you get something like

path:        abag.bag
version:     2.0
duration:    1:35s (95s)
start:       Aug 23 2021 03:34:46.34 (1629689686.34)
end:         Aug 23 2021 03:36:21.45 (1629689781.45)
size:        1.1 GB
messages:    952
compression: none [952/952 chunks]
types:       sensor_msgs/Image [060021388200f6f0f447d0fcd9c64743]
topics:      image_results   952 msgs    : sensor_msgs/Image

Is there a way to call this program from a bash shell file and then extract only one field (for example messages) to compare it to something

In pseudo code something like

call rosbag info and get messages
if messages== 952
then print("everything ok")
else print("some messages were skipped"

Is this possible in bash script?


Solution

  • Yes, you actually can. rosbag info has a flag, -k, for pulling out specific fields; you'll also need to use -y for this to work. So, in your example pulling out the number of messages would look like rosbag info -y -k messages abag.bag. If you wanted to put all of this in a bash script it could look something like where you would pass the expected number of messages in as a command line parameter:

    expectedNum=$1
    actualNum=$(rosbag info -y -k messages abag.bag)
    if (( expectedNum==actualNum )); then
        echo "everything okay"
    else
        echo "everything not okay"
    fi