English 中文(简体)
Lazy Evaluation in Bash
原标题:Lazy Evaluation in Bash

是否有比以下更可取的 doing评价方式:

pattern= $x and $y 
x=1
y=2
eval "echo $pattern"

结果:

1 and 2

它只运行eval“echo ......”,只是感到lo,可能有些不安全。 在巴什是否有更好的办法这样做?

最佳回答

https://www.gnu.org/software/gettext/manual/html_node/envsubst-Inue.html”rel=“nofollow noretinger”>envsubst from gettext,例如:

$ pattern= x=$x and y=$y 
$ x=1 y=2 envsubst <<< $pattern
x=1 and y=2
问题回答

一种安全的可能性是使用一种功能:

expand_pattern() {
    pattern="$x and $y"
}

所有这一切都是如此。 然后使用如下:

x=1 y=1
expand_pattern
echo "$pattern"

您甚至可以将<条码>x和y作为环境变量(因此,这些变量不在主要范围):

x=1 y=1 expand_pattern
echo "$pattern"

您的权利,eval是本案的安全风险。 一种可能的办法:

pattern= The $a is $b when the $z is $x $c $g.     # simulated input from user (use "read")
unset results
for word in $pattern
do
    case $word in
        $a)
            results+=($(some_command))   # add output of some_command to array (output is "werewolf"
            ;;
        $b)
            results+=($(echo "active"))
            ;;
        $c)
            results+=($(echo "and"))
            ;;
        $g)
            results+=($(echo "the sky is clear"))
            ;;
        $x)
            results+=($(echo "full"))
            ;;
        $z)
            results+=($(echo "moon"))
            ;;
          *)
            do_something    # count the non-vars, do a no-op, twiddle thumbs
            # perhaps even sanitize %placeholders, terminal control characters, other unwanted stuff that the user might try to slip in
            ;;
    esac
done
pattern=${pattern//$[abcgxz]/%s}    # replace the vars with printf string placeholders
printf "$pattern
" "${results[@]}"  # output the values of the vars using the pattern
printf -v sentence "$pattern
" "${results[@]}"  # put it into a variable called "sentence" instead of actually printing it

产出将是“当月球满,天空清晰时,这些妇女正在活跃”。 同样的方案,如果模式是x美元,则美元为美元,因此,美元必须是美元。 那么,产出将是“整个月球已经消失,天空非常清楚,因此,死胎必须活跃。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签