Вопрос по linux, bash, shell, unix – Заставить bash раскрыть переменные в строке, загруженной из файла

61

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

hello $FOO world

Я тогда бегу

export FOO=42
echo $(cat something.txt)

это возвращает:

   hello $FOO world

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededevalError: User Rate Limit Exceeded Dennis Williamson
Error: User Rate Limit Exceededsecurity implications of eval. Dennis Williamson
Error: User Rate Limit Exceededtips hat Michael Neale
Error: User Rate Limit Exceeded Michael Neale

Ваш Ответ

12   ответов
1
foo=45
file=something.txt       # in a file is written: Hello $foo world!
eval echo $(cat $file)
Error: User Rate Limit Exceededproper explanationError: User Rate Limit ExceedededitError: User Rate Limit Exceeded
16

echo $(eval echo $(cat something.txt))
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededecho -e "$(eval "echo -e \"`<something.txt`\"")"Error: User Rate Limit Exceeded
2

contents="$(cat something.txt)"
echo $(eval echo \"$contents\")

Error: User Rate Limit Exceededeval "echo \"$$contents\""Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
1
$ eval echo $(cat something.txt)
hello 42 world
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Copyright (C) 2007 Free Software Foundation, Inc.
2

bash method, (one-line variant of Michael Neale's answer), using process & command substitution:

FOO=42 ..txt))

Output:

hello 42 world

Note that export isn't needed.

GNU sed evaluate method, if something.txt has many lines:

FOO=42 sed 's/"/\\\"/g;s/.*/echo "&"/e' something.txt
Error: User Rate Limit ExceededsedError: User Rate Limit Exceeded\$(date)Error: User Rate Limit Exceeded$(date)Error: User Rate Limit Exceeded
73

Error: User Rate Limit ExceededenvsubstError: User Rate Limit Exceeded

envsubst < something.txt

Error: User Rate Limit Exceeded

GNU package gettext.

Error: User Rate Limit Exceeded

(BTW, there is a "feature" of envsubst, explained at Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

#! /bin/bash
      ## -*-Shell-Script-*-
CmdName=${0##*/}
Usage="usage: $CmdName runs envsubst, but allows '\$' to  keep variables from
    being expanded.
  With option   -sl   '\$' keeps the back-slash.
  Default is to replace  '\$' with '$'
"

if [[ $1 = -h ]]  ;then echo -e >&2  "$Usage" ; exit 1 ;fi
if [[ $1 = -sl ]] ;then  sl='\'  ; shift ;fi

sed 's/\\\$/\${EnVsUbDolR}/g' |  EnVsUbDolR=$sl\$  envsubst  "[email protected]"
Error: User Rate Limit Exceededbrew link --force gettext
Error: User Rate Limit ExceededguaranteedError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Michael Neale
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

envsubstError: User Rate Limit Exceeded

Error: User Rate Limit ExceededenvsubstError: User Rate Limit Exceeded

awk solution

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

needle="doc1_base64" # The "variable name" in the file. (A $ is not needed.)
needle_file="doc1_base64.txt" # Will be substituted for the needle 
haystack=$requestfile1 # File containing the needle
out=$requestfile2
awk "BEGIN{getline l < \"${needle_file}\"}/${needle}/{gsub(\"${needle}\",l)}1" $haystack > $out

Error: User Rate Limit Exceeded

25

evalecho

apply_shell_expansion() {
    declare file="$1"
    declare data=$(< "$file")
    declare delimiter="__apply_shell_expansion_delimiter__"
    declare command="cat <<$delimiter"$'\n'"$data"$'\n'"$delimiter"
    eval "$command"
}

parameters.cfgtemplate.txt

. parameters.cfg
printf "%s\n" "$(apply_shell_expansion template.txt)" > result.txt

Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededevalError: User Rate Limit Exceeded; $(eval date)Error: User Rate Limit ExceededdateError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0

allows replacing of variables which are defined

leaves unchanged variables placeholders which are not defined. This is especially useful during automated deployments.

supports replacement of variables in following formats:

${var_NAME}

$var_NAME

reports which variables are not defined in environment and returns error code for such cases



    TARGET_FILE=someFile.txt;
    ERR_CNT=0;

    for VARNAME in $(grep -P -o -e '\$[\{]?(\w+)*[\}]?' ${TARGET_FILE} | sort -u); do     
      VAR_VALUE=${!VARNAME};
      VARNAME2=$(echo $VARNAME| sed -e 's|^\${||g' -e 's|}$||g' -e 's|^\$||g' );
      VAR_VALUE2=${!VARNAME2};

      if [ "xxx" = "xxx$VAR_VALUE2" ]; then
         echo "$VARNAME is undefined ";
         ERR_CNT=$((ERR_CNT+1));
      else
         echo "replacing $VARNAME with $VAR_VALUE2" ;
         sed -i "s|$VARNAME|$VAR_VALUE2|g" ${TARGET_FILE}; 
      fi      
    done

    if [ ${ERR_CNT} -gt 0 ]; then
        echo "Found $ERR_CNT undefined environment variables";
        exit 1 
    fi
Error: User Rate Limit Exceeded
10

evaluate

FOO=42
while read; do
    eval echo "$REPLY"
done < something.txt

help eval

eval is dangerousError: User Rate Limit Exceeded$varnameError: User Rate Limit ExceededenvsubstError: User Rate Limit Exceeded$(rm -rf ~)Error: User Rate Limit Exceeded
4

something=$(cat something.txt)

echo "echo \"" > temp.out
echo "$something" >> temp.out
echo "\"" >> temp.out

RESULT=$(source temp.out)

Error: User Rate Limit Exceeded
-2

bash -c "echo \"$(cat something.txt)"\"

Error: User Rate Limit Exceeded$fooError: User Rate Limit Exceeded$(rm -rf ~).

Похожие вопросы