Bash Code Snippets

Kill all processes with common name

PROCESS_NAME="px4_fault_injection"
for PID in $(ps aux | grep "$PROCESS_NAME" | grep -v grep | awk '{print $2}'); do
  if [ -z "$PID" ]; then
      echo "No process found with name $PROCESS_NAME"
  else
      kill $PID
      echo "Process $PROCESS_NAME (PID $PID) has been killed."
  fi
done

Create (--help)ful prompts

display_help() {
  echo "Usage: $0 [-s|--sensor SENSOR] [-f|--fault-type TYPE] [-v|--fault-value VALUE] [--help]"
  echo "Options:"
  echo "  -s, --sensor       Specify the sensor (all caps, e.g., ACCEL)"
  echo "  -f, --fault-type   Specify the fault type (all caps, e.g., NOISE)"
  echo "  -v, --fault-value  Specify the fault value (float value, e.g., 0.2)"
  echo "  -h, --help         Display this help message"
  exit 0
}

while [[ $# -gt 0 ]]; do
  case "$1" in
      -s|--sensor)
          sensor="$2"
          shift 2
          ;;
      -f|--fault-type)
          type="$2"
          shift 2
          ;;
      -v|--fault-value)
          value="$2"
          shift 2
          ;;
      -h|--help)
          display_help
          ;;
      *)
          echo "Unknown argument: $1"
          exit 1
          ;;
  esac
done

if [ -z "$sensor" ] && [ -z "$type" ] && [ -z "$value" ]; then
  display_help
fi

ros2 run px4_fault_injection iteration_runner.py $sensor $type $value

Copy using tar

ssh user@host "tar -cf - -C root_directory_absolute_path copy_directory_relative_path" | tar -xf - -C ./ --strip-components=0 

Streams archive to STDIN from remote and into unarchive in host

if you want to copy ~/Documents/workspace/device-drivers/ folder you do:

ssh user@host "tar -cf - -C ~/Documents/workspace device-drivers" | tar -xf - -C ./ --strip-components=0