help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] safe parsing of configuration files?


From: John Kearney
Subject: Re: [Help-bash] safe parsing of configuration files?
Date: Tue, 7 May 2013 22:31:34 +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=C
    local cline_tmp
    local vval_tmp
    local tmp_quotes="'\""
    while IFS= read -r cline_tmp ; do
      case "${cline_tmp}" in
        \#* | '') ;;  # skip empty lines and commented lines
        [a-zA-Z_]*=*)     # *[![:space:]]*) # non empty lines
          vval_tmp=${cline_tmp#*=}
          vval_tmp=${vval_tmp#[${tmp_quotes}]}
          vval_tmp=${vval_tmp%[${tmp_quotes}]}
          ks_val_Set "${cline_tmp%%=*}" "${vval_tmp}" || echo "error '${cline_tmp}'" 
          #printf "%s=%q\n" "${cline_tmp%%=*}" "${vval_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:
> 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
>   }

I get a error.
./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?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]