Heyas
So while i'm trying to increase security/usability of my TUI commands, i'm currently at tui-conf-get
.
There was also a bug, i wanted to fix, but sadly that bugfix is more insecure - but twice as fast as my current buggy method.
I've added a getopts
toggle, '-s' to toggle beSecure
, meaning to use either source
or awk
.
Anyway, here's the code after getopts:
CONFFILE="$1"
VARNAME="$2"
# If VARNAME starts with Pass|password|pw, do not source the conf file but use awk
case "$VARNAME" in
[Pp][Aa][Ss][Ss][Ww][Oo][Rr][Dd]|[Pp][Ww]|[Pp][Aa][Ss][Ss])
beSecure=true
;;
esac
[ ! -f "$CONFFILE" ] && tui-printf -S 1 "$CONFFILE does not exist!" && exit 1
[ "$CONFFILE" = "${CONFFILE##*/}" ] && CONFFILE="./$CONFFILE"
#
# Display & Action
#
if $beSecure
then $AWK -F= -v VAR="$VARNAME" '$1 ~ "^[^#]*" VAR "$"{print $2,$3,$4,$5}' "$CONFFILE"|$SED s,'"','',g|head -n1
else # This method is about twice the speed of the above
source "$CONFFILE" && echo "${!VARNAME}"
fi
To illustrate my bug:
$ echo "variable=value # unwanted comment" > tmp
$ tui-conf-get -s tmp variable
value # unwanted comment
$ tui-conf-get tmp variable
value
Furthermore, if the variable is a list, i'm getting either too few and too many entries with the same awk call on the very only variable read:
echo "list=\"one two three four five six\" # bad comment" >> tmp
bin/tui-conf-get tmp list
one two three four five six
bin/tui-conf-get -s tmp list
one two three four # bad comment
I'm aware that this is because in awk i only 'use' $2-$5, but then again, why is the comment shown?
My questions:
- Is there a way to tell awk to print all columns but not $1?
I mean, i could add like $2,$3,....$22,$23 -- but what if the list read has 25 or more items? - As i already use sed with the awk command, how would i modify it to remove the quotes, and the hash followed by the comment?
When trying basic regex with sed, i cant get it to work as i expect it to:
$ echo "\"1 2 3 4 5 6\" # comment" | sed s,[\"\#*],,g
1 2 3 4 5 6 comment
Any ideas/advice?
Thank you