Search code examples
bashshellopenwrt

Display directives (in lines) from config file that aren't in an another config using bash scripting


There are similar questions like this, but this is specific, and my awk knowledge isn't good at all (though you can use any tool for this). So, I'm working with OpenWRT config files. 1st file looks like this (this is the initial config of my build (a part of it)):

# Use "make defconfig" to expand this to a full .config

# Selecting target and model(s)
CONFIG_TARGET_ar71xx=y
CONFIG_TARGET_ar71xx_generic_ARCHERC7=y

CONFIG_PACKAGE_luci-ssl=y

# we do not need these, since it adds more dependencies
#CONFIG_PACKAGE_mc=y

As you can see the directives (e.g. 'CONFIG_PACKAGE_luci-ssl') usually have value in this file (but this doesn't matter), but they can be commented out, and can be empty lines in it. Let's say the 2nd config file looks like this (just a basic full config):

#
# Automatically generated file; DO NOT EDIT.
# OpenWrt Configuration
#
CONFIG_MODULES=y
CONFIG_HAVE_DOT_CONFIG=y
# CONFIG_TARGET_ppc40x is not set
# CONFIG_TARGET_realview is not set
# CONFIG_TARGET_atheros is not set
CONFIG_TARGET_ar71xx=y
# CONFIG_TARGET_ar71xx_generic_ARCHERC7 is not set

In the 2nd config the directives usually don't have values (these times they are commented out) but sometimes they can have.

I want to display all uncommented directives (with or without their values (doesn't matter)) from the 1st file that aren't in the second file at all whether they are commented out or uncommented. In this example it'd only display:

CONFIG_PACKAGE_luci-ssl

It's good for having an initial config file, and when you upgrade to an another release you can easily test whether you can use your config or not with that release (is there any missing settings). Thanks!


Solution

  • Assuming the files are named 1st and 2nd, this is one possibility:

    grep -vf<(echo ^#;echo ^$;sed -n 's/.*=y/^&/p;s/# \(.*\) is not set/^\1=/p' 2nd) 1st
    

    The commands in between <( ) generate a list of patterns that are searched for in 1st:

    • ^# lines beginning with #
    • ^$ lines beginning and immediately ending, i. e. empty
    • ^.*= lines beginning with directives from 2nd

    Due to option -v, these matching lines are not displayed, leaving the wanted non-matching lines.