Score:0

Debugging bash script libraries

jp flag

I have a list of files that I source in some particular order

Here is an example

source "${edvart_trkrc}/marinex.rc"
source "${edvart_trkrc}/firefly.rc"
source "${edvart_trkrc}/navaid.rc"
source "${baikonur_trkrc}/buranex.rc"

source "${gunars_trkrc}/comseq.rc"
source "${gunars_trkrc}/comint.rc"

I want a way to tag each file name, 1 for setting an option that enables debugging (via set-xv), and 0 or -1 for no debugging.

Preferably I want the user to be able to do this. Perhaps have a file with the correct order as follows

0 marinex.rc
0 firefly.rc
0 navaid.rc
1 buranex.rc
    
0 comseq.rc
0 comint.rc

Or perhaps some suggestions about alternative ways that could possibly be more convenient for users to use.

Score:0
vn flag

This can be done with a relatively simple script. For both options, make sure you have a debug-config file containing what's in your question, except it also need to contain the paths: (else the script will not know the exact location of each file)

0 ${edvart_trkrc}/marinex.rc
0 ${edvart_trkrc}/firefly.rc
0 ${edvart_trkrc}/navaid.rc
1 ${baikonur_trkrc}/buranex.rc

0 ${gunars_trkrc}/comseq.rc
0 ${gunars_trkrc}/comint.rc

Option 1: Modify the source files themselves for debugging

First, in all your source files (marinex.rc, firefly.rc etc.) make sure the following line is the first line: (sets debugging off by default)

set +xv

Now create a debug-set script and make it executable:

#!/bin/bash

# define paths
edvart_trkrc="/define/the/path1"
baikonur_trkrc="/define/the/path2"
gunars_trkrc="/define/the/path3"

# reading each line
while read -r line; do
  # define options
  debug_st=$(echo "$line" | awk '{ print $1 }')
  filename=$(echo "$line" | awk '{ print $2 }')

  # change debug setting
  case "$debug_st" in
    0)
      sed -i 's/set -xv/set +xv/g' "$filename"
      ;;
    1)
      sed -i 's/set +xv/set -xv/g' "$filename"
      ;;
  esac
done < ./debug-config

Make sure you define any variable paths included in the debug-config file.

Now, when you run the ./debug-set script, it will change the first line of each sourced file to set +xv if the config contains a 0, and set -xv if the config contains a 1.

In any other cases, it's not going to do anything. This will easily allow anyone to apply the debug configuration set in debug-config to the source files.

Option 2: Set the debug toggles directly in the script sourcing the files

Another way to go about this is to set the debug info directly in the script sourcing the files. This works much like the debug-set script, except this "just" sources the files with or without debugging.

So have your source-script instead of doing this:

source "${edvart_trkrc}/marinex.rc"
source "${edvart_trkrc}/firefly.rc"
source "${edvart_trkrc}/navaid.rc"
source "${baikonur_trkrc}/buranex.rc"

source "${gunars_trkrc}/comseq.rc"
source "${gunars_trkrc}/comint.rc"

Then do this:

#!/bin/bash

# reading each line
while read -r line; do
  # define options
  debug_st=$(echo "$line" | awk '{ print $1 }')
  filename=$(echo "$line" | awk '{ print $2 }')

  # change debug setting
  case "$debug_st" in
    0)
      set +xv
      source "$filename"
      ;;
    1)
      set -xv
      source "$filename"
      set +xv
      ;;
  esac
done < ./debug-config

Now the command sourcing each file sets the proper debug option before sourcing, and turns it off again afterwards if it was on.

The only downside to this is, that since the debug option is set right before the file is sourced, the output of the entire file (the source command) will be part of the debugging information.

jp flag
I do not like that the file gets modified with such contraption.
Artur Meinild avatar
vn flag
Now there are 2 options - one that changes the source files, and one variant that instead sets the debug toggle directly in the script that sources the files instead. Choose whichever you like.
jp flag
I find that the latter is more appropriate for me. Had an implementation that uses associative array, but then realised how associative arrays in bash are unordered. A solution would then need both an indexed array and an associative one. Still, I did get a variation of your example codes to some use.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.