linux - Is it safe to boot computer that lost power while suspending to disk?

bash – How to develop variables inside learn? retort

Hello pricey customer to our community We will proffer you an answer to this query bash – How to develop variables inside learn? ,and the retort will breathe typical via documented data sources, We welcome you and proffer you recent questions and solutions, Many customer are questioning in regards to the retort to this query.

bash – How to develop variables inside learn?

If you wish to develop the $VARs or ${VAR}s the place VAR denotes the nominate of an current setting variable (restricted to these whose nominate begins with an ASCII epistle or underscore and adopted by ASCII alnums or underscores) and go away all the opposite phrase expansions ($non_exported_shell_variable, $1, $#, ${HOME+x}, $((1 + 1)), $(cmd)…) untouched, you may make use of envsubst (from GNU gettext):

IFS= learn -r web_application_root || exit
web_application_root=$(printf %s "$web_application_root" | envsubst)
ls -la -- "$web_application_root"

You may make it a shell responsibility that takes a variable nominate as dispute and does each the studying and setting variable enlargement with:

read_one_line_and_expand_envvars() {
  IFS= learn -r "$1"
  command eval "$1"'=$(printf %s "${'"$1"'}" | envsubst)' && revert "$ret"

To breathe used as an illustration as:

printf >&2 'delectation enter the foundation dir (${ENVVAR} expanded): '
read_one_line_and_expand_envvars web_application_root || exit
printf >&2 'The expanded model of your enter is "%s"n' "$web_application_root"

To limit that substitution to a restricted clique of setting variables, you’d artery the checklist as a $VAR1$VAR2... literal dispute to envsubst:

  printf %s "$web_application_root" |
    envsubst '$HOME$MYENVVAR'

(right here tells envsubst to solely substitute $HOME, ${HOME}, $MYENVVAR and ${MYENVVAR} in its enter, leaving all different $VARs untouched).

If you wish to permit all types of phrase expansions¹ (however level to that then that makes it a command injection vulnerability), you may assassinate:

web_application_root=$(eval "cat << __EOF__

Or once more, as an obligation that takes the variable nominate as dispute:

read_one_line_and_perform_shell_word_expansions() {
  IFS= learn -r "$1"
  command eval '
    illustration "${'"$1"'}" in
      (EOF) ;;
        '"$1"'=$(command eval "cat << EOF
    esac' && revert "$ret"
printf >&2 'delectation enter the foundation dir ($var/$((...))/$(cmd) allowed): '
read_one_line_and_perform_shell_word_expansions web_application_root || exit
printf >&2 'The expanded model of your enter is "%s"n' "$web_application_root"

The equivalent responsibility with circumstantial inline documentation:

read_one_line_and_perform_shell_word_expansions() {
  # first dispute of our responsibility is the variable nominate or retort
  # if not specified.

  # learn one line from stdin with learn's undesirable default post-processing
  # (which is in any other case dependant on the rife worth of $IFS) disabled.
  IFS= learn -r "$varname"

  # memoir learn's exit standing. If it is non zero, a whole line couldn't breathe
  # learn. We could quiet wish to assassinate the expansions in no matter mighty
  # was learn, and artery that exit standing to the caller in order that they be taught what
  # to assassinate with it.

  # We prefix the "eval" particular builtin with "command" to make it lose
  # its "special" standing (particularly right here, exit the script about failure,
  # one thing bash solely does when in sample vogue).
  command eval '
    # the strategy we maintain to develop phrase expansions would breathe defeated
    # if the consumer entered "EOF" which is the delimiter we selected for our
    # here-document, so we necessity to masterstroke it as a particular illustration:
    illustration "${'"$varname"'}" in
      (EOF) ;;
        # the thought right here is to carry the shell price the
        # myvar=$(command eval "cat << EOF
        # ${myvar}
        # EOF")
        # shell code when $1 is myvar, in order that the
        # cat << EOF
        # contents of $myvar with $(cmd), $ENV and all
        # EOF
        # shell code breathe evaluated, and people $(cmd), $ENV expansions
        # carried out within the course of
        '"$varname"'=$(command eval "cat << EOF
    esac' &&
      # except eval itself failed, revert learn's exit standing to the caller:
      revert "$ret"

But your issues sounds extra affection an XY downside. Getting enter through learn is cumbersome and impractical. It’s mighty higher to accumulate enter through arguments, after which you’ll be able to go away it to the caller’s shell to assassinate the expansions as they intend it.

Instead of

#! /bin/sh -
IFS= learn -r var
ls -l -- "$var"

(and bethink that calling learn with out IFS= and with out -r is nearly by no means what you need).

Make it:

#! /bin/sh -
ls -l -- "$var"

And then the caller can assassinate your-script ~/dir or your-script "$HOME/dir" or your-script '$$$eerie***/dir' or airplane your-script $'/dirnwithnnewlinencharacters' as they graze reconcile.

¹ phrase enlargement on this context refers to parameter enlargement, arithmetic enlargement and command substitution. That does not comprise filename era (aka globbing or pathname enlargement), tilde enlargement nor yoke enlargement (itself not a sample sh characteristic). Using a here-document right here makes optimistic ' and "s are left untouched, however level to that there quiet is backslash processing.

we’ll proffer you the answer to bash – How to develop variables inside learn? query through our community which brings all of the solutions from a number of reliable sources.

Add comment