[awk] printing value of a variable assignment from a file

Heyas

Me try to print only the value of a (specific) variable assignment from a file.

What i get (1):

:) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME

dot-blue
""
"$TUI_DIR_INSTALL_ROOT/usr"
"$TUI_DIR_INSTALL_ROOT/etc/tui"
"$TUI_PREFIX/share/doc/tui"
"$TUI_PREFIX/share/tui"
"/home/sea/.config/tui"
"$TUI_DIR_USER/logs"
"$TUI_DIR_SYSTEM/themes"
"/home/sea/.cache"
"/home/sea/.local/bin"
"/home/sea/.local/man/man1"
"/home/sea/.cache/tui.tmp~"
"$TUI_DIR_USER/loadlist.conf"
"$TUI_DIR_USER/apps.conf"
"$TUI_DIR_USER/user.conf"
"$TUI_DIR_USER/settings.conf"
"$TUI_DIR_CONF/tui.conf"

What i want: (2)

 tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME

dot-blue

Code i got:

CONFILE="$1"
VARNAME="$2"
awk	'  
		/^#/  ||
		/^\[/  ||
		/!VAR/ { next }
		{
			if (VAR = $1) {
				if ("-z" != $2) {
					print $2
				}
			}
	}' VAR="$VARNAME" FS="=" "$CONFFILE"

The RC file looks like:

#/home/sea/.tui_rc
# This file is not ment to be changed manualy!
# Any change of this file may result in an unusable TUI.
# Do changes at your own risk!
#
#	Theme
#	Options are: 	default, default-red, dot-blue, dot-red, \
#			floating, mono, witch-purple, witch-yellow
#	See /usr/share/tui/themes
#
 	TUI_THEME=dot-blue
#
#	Paths
#
[ -z "$TUI_DIR_INSTALL_ROOT" ] &&  \
 	TUI_DIR_INSTALL_ROOT=""
[ -z "$TUI_PREFIX" ] && \
 	TUI_PREFIX="$TUI_DIR_INSTALL_ROOT/usr"
[ -z "$TUI_DIR_CONF" ] && readonly \
 	...
#
#	Files
#
[ -z "$TUI_FILE_TEMP" ] && readonly \
 	TUI_FILE_TEMP="/home/sea/.cache/tui.tmp~"
[ -z "$TUI_FILE_CONF_LOADLIST" ] && readonly \
 	TUI_FILE_CONF_LOADLIST="$TUI_DIR_USER/loadlist.conf"
...

What am i missing?
Tia

Perhaps this will help you:

$ printf "%s=%s\n" one two three four TUI_THEME dot-blue | awk -v VAR="TUI_THEME" -F= '
   /!VAR/ {next}
   { print $2}'
two
four
dot-blue

$ printf "%s=%s\n" one two three four TUI_THEME dot-blue | awk -v VAR="TUI_THEME" -F= '
   $1 != VAR {next}
   { print $2}'
dot-blue

So your choices here are:

/!VAR/    -> Input line contains the string "!VAR" anywhere
!/VAR/    -> Input line does not contain the string "VAR" anywhere
$0 !~ VAR -> Input line doesn't contain the contents of variable VAR anywhere
$1 != VAR -> First field is anything except the exact contents of variable VAR
1 Like

Since you are using an = sign as field separator, $1 will contain leading spaces, so it is not going to equal to the name, unless it it right at the beginning of the line. You could try

$1 ~ VAR "$"

rather than

$1==VAR

--
Try:

$ awk -F= -v VAR=TUI_THEME '$1 ~ "^[^#]*" VAR "$"{print $2}' ~/.tui_rc
dot-blue
1 Like