|
From: | John Kearney |
Subject: | Re: [Help-bash] safe parsing of configuration files? |
Date: | Tue, 7 May 2013 22:50:09 +0200 |
not sure if its worth putting it on github,anyway I tested this one briefly
ks_val_ChkName() {
case "${1:?Missing Variable Name}" in
[!a-zA-Z_]* | *[!a-zA-Z_0-9]* | '' ) return 3;;
esac
}
ks_val_Set() {
ks_val_ChkName "${1}" || return $?
eval "${1}"'="${2}"'
}
ks_cgf_Src() {local LC_COLLATE=Clocal tmp_quotes="'\""
local cline_tmp
local vval_tmp
while IFS= read -r cline_tmp ; do
case "${cline_tmp}" in
\#* | '') ;; # skip empty lines and commented linesvval_tmp=${vval_tmp#[${tmp_quotes}]}
[a-zA-Z_]*=*) # *[![:space:]]*) # non empty lines
vval_tmp=${cline_tmp#*=}
vval_tmp=${vval_tmp%[${tmp_quotes}]}#printf "%s=%q\n" "${cline_tmp%%=*}" "${vval_tmp}"
ks_val_Set "${cline_tmp%%=*}" "${vval_tmp}" || echo "error '${cline_tmp}'"
;;
*)echo "skipping '${cline_tmp}'"
;;
esac
done
}
declare | grep "^te"
ks_cgf_Src <<EOF
test=kkkk
te=kll
4te=kll
te4te='kllddf dfs g'
te2te="kllddf dfs g"
EOF
declare | grep "^te"On Tue, May 7, 2013 at 8:57 AM, adrelanos <address@hidden> wrote:
John Kearney:
I get a error.> you could do something like
>
>
> ks_val_ChkName() {
> case "${1:?Missing Variable Name}" in
> [!a-zA-Z_]* | *[!a-zA-Z_0-9]* | '' ) return 3;;
> esac
> }
> ks_val_Set() {
> ks_val_ChkName "${1}" || return $?
> eval "${1}"'="${2}"'
> }
> ks_cgf_Src() {
> local IFS=$'\n'
> local LC_COLLATE=C
> local cline_tmp
> local vval_tmp
> while read -d '' cline_tmp ; do
> case "${cline_tmp}" ; in
> \#* | '') continue;; # skip empty lines and commented lines
> [a-zA-Z_]*=*) # *[![:space:]]*) # non empty lines
> vval_tmp="${cline_tmp#*=}"
> vval_tmp="${vval_tmp#["']}"
> vval_tmp="${vval_tmp%["']}"
> ks_val_Set "${cline_tmp%%=*}" "${vval_tmp}" || echo "error
> '${cline_tmp}'"
> ;;
> *)echo "skipping '${cline_tmp}'"
> continue;;
> esac
> done
> }
./extest: line 18: syntax error near unexpected token `;'
./extest: line 18: ` case "${cline_tmp}" ; in'
Since this has been said to be impossible with bash, you could polish it
a bit please and perhaps put it on github?
[Prev in Thread] | Current Thread | [Next in Thread] |