0

I have a shell script that I use to launch some ROS launcher file (not that important for my question). My requirement is to use some arguments and therefore I am using getopts.

Basically, I am getting one file or a few, playing them with ROS and simultaneously recording all the files back to a single file. After that, if I have provided an argument -r or -c, I would like to run two additional operations (reindex and compress) on the recorded file. But it is required that the other process are done, before I can run the -r and -c. I am using the wait keyword, but I am not sure I really understand the flow. In other words, the playing and recording should be done and only then -r and -c should be run if provided as arguments.

Second question is related to how do I get or pass the same file that was outputted to these two functions (reindex and compress)?

So my desired format is:

./myscript -i file1 -i file2 -o /home/output -r -c

#!/bin/bash  

OUTPUT_FILE_NAME="output_$(date +%Y.%m.%d--%H_%M_%S)"

usage="
$(basename "$0") [-i] [-o] [-r] [-c] [-h] -- to be done"

# Reset is necessary if getopts was used previously in the script.
OPTIND=1    

while getopts ":i:orch:" opt; do
    case $opt in
        i ) echo "INPUT file - argument = $OPTARG"; input_files+=($OPTARG) ;;
        o ) echo "OUTPUT dir - argument = $OPTARG"; output_dir=($OPTARG) ;;
        r ) echo "REINDEX"; reindex ;;
        c ) echo "COMPRESS"; compress ;;
        h ) echo "$usage"
            graceful_exit ;;
        * ) echo "$usage"
            exit 1
    esac
done

# Shift off the options
shift $((OPTIND-1))

roslaunch myLauncher.launch &

echo "Number of loaded files: ${#input_files[@]}"
echo -n "FILES are:"
rosbag play ${input_files[@]} &
rosbag record -o $output_dir/$OUTPUT_FILE_NAME -a &
wait

function reindex{
    rosbag reindex $output_dir/$OUTPUT_FILE_NAME
}

function compress{
    rosbag reindex $output_dir/$OUTPUT_FILE_NAME
}

Thank you in advance!

1 Answer 1

1

You're very close to where you need to be — and using getopts puts you firmly on the correct track, too. Note whether or not you need to reindex or compress in the option parsing loop. Then, after the music has been played and the output file written, run the code from the functions if you need to:

#!/bin/bash  

OUTPUT_FILE_NAME="output_$(date +%Y.%m.%d--%H_%M_%S)"

usage="$(basename "$0") [-i file] [-o file] [-r] [-c] [-h]"

input_files=()  # Empty list of files/tracks
output_dir="."  # Default output directory
r_flag="no"     # No reindex by default
c_flag="no"     # No compress by default

while getopts ":i:orch:" opt; do
    case $opt in
        (i) echo "INPUT file - argument = $OPTARG"; input_files+=("$OPTARG");;
        (o) echo "OUTPUT dir - argument = $OPTARG"; output_dir="$OPTARG";;
        (r) echo "REINDEX"; r_flag="yes";;
        (c) echo "COMPRESS"; c_flag="yes";;
        (h) echo "$usage" >&2; exit 0;;
        (*) echo "$usage" >&2; exit 1;;
    esac
done

# Shift off the options
shift $((OPTIND-1))

if [ $# != 0 ] || [ "${#input_files[@]}" = 0 ] ||
then
    echo "$usage" >&2
    exit 1
fi

roslaunch myLauncher.launch &

echo "Number of loaded files: ${#input_files[@]}"
echo -n "FILES are:"
rosbag play "${input_files[@]}" &
rosbag record -o "$output_dir/$OUTPUT_FILE_NAME" -a &
wait

if [ "$r_flag" = "yes" ]
then
    rosbag reindex "$output_dir/$OUTPUT_FILE_NAME"
fi
if [ "$c_flag" = "yes" ]
then
    rosbag compress "$output_dir/$OUTPUT_FILE_NAME"
fi

I didn't keep the functions since they didn't provide any value in the rewritten code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But now, how do I delay the execution of -r and -c so that they are being run after rosbag record ... has completed?
You already did; the wait waits until both the 'play' and 'record' processes complete (exit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.