ksh88将单引号更改为heredocs中的双引号? [英] ksh88 changing single quotes to double quotes inside heredocs?
问题描述
我似乎遇到了一个特定于ksh88的问题,该问题将单引号更改为双引号,但仅在某些涉及Heredocs和命令替换的情况下.
I seem to be running into an issue that's specific to ksh88 that's changing single quotes to double quotes, but only under certain situations involving heredocs and command substitution.
这是一个例子:
#!/bin/ksh
# This example works correctly
echo "Example 1:"
cat <<EOF
The 'quick' brown fox "jumped" over the lazy dog.
EOF
echo
# This example is broken
echo "Example 2:"
var=$(cat <<EOF
The 'quick' brown fox "jumped" over the lazy dog.
EOF)
echo "${var}"
echo
# This example works correctly
echo "Example 3:"
var=`cat <<EOF
The 'quick' brown fox "jumped" over the lazy dog.
EOF`
echo "${var}"
echo
这是输出(请注意示例2的不同之处):
And here's the output (note how Example 2 is different):
Example 1:
The 'quick' brown fox "jumped" over the lazy dog.
Example 2:
The "quick" brown fox "jumped" over the lazy dog.
Example 3:
The 'quick' brown fox "jumped" over the lazy dog.
在命令运行前 似乎发生了'
到"
的替换.实际上,heredoc正在将SQL传递给Oracle.通过将'
更改为"
,字符串将转换为标识符,从而破坏了SQL.在执行上述代码期间启用xtrace也可以观察到这一点.
The '
to "
substitution seems to occur before the command runs. In actual context, the heredoc is passing SQL to Oracle. By changing '
to "
, strings are being converted to identifiers, thus breaking the SQL. This can also be observed by enabling xtrace during execution of the above code.
如何在不使用反引号的情况下阻止上述代码段中的'
到"
转换?
How can I prevent the '
to "
conversion in the above code snippet without using backticks?
情节变浓了.用反引号表示法替换命令替换$( ... )
不会用双引号替换单引号.所以(可选)问题二:为什么?
The plot thickens. Replacing the command substituion $( ... )
with backtick notation doesn't replace the single quotes with double quotes. So (optional) question two: why?
推荐答案
几年前,当我发现相同的bug时,这里是我的笔记.
Here are my notes when I discovered this same bug several years ago.
测试脚本:
#!/bin/ksh
cat <<EOF
$PWD "$PWD" '$PWD'
EOF
echo `cat <<EOF
$PWD "$PWD" '$PWD'
EOF
`
echo $(cat <<EOF
$PWD "$PWD" '$PWD'
EOF
)
不同外壳的输出:
- Linux KSH版本M 1993-12-28 q
- Linux Bash 3.00.15(1)
(注意:工作正常)
/home/jrw32982 "/home/jrw32982" '/home/jrw32982'
/home/jrw32982 "/home/jrw32982" '/home/jrw32982'
/home/jrw32982 "/home/jrw32982" '/home/jrw32982'
- AIX版本M-11/16/88f
- Solaris版本M-11/16/88i
(注意:单引号替换为双引号,并且变量不被替换)
(NOTE: single quotes replaced with double quotes and variable not substituted)
/home/jrw32982 "/home/jrw32982" '/home/jrw32982'
/home/jrw32982 "/home/jrw32982" '/home/jrw32982'
/home/jrw32982 "/home/jrw32982" "$PWD"
解决方法:
-
从此处文件外部计算单引号字符串
Compute the single-quoted string externally from the here-file
abc=xyz
STR="'$abc'"
x=$( cat <<EOF
$abc "$abc" $STR
EOF
)
在函数中使用此处文件而不是直接使用
Use the here-file in a function instead of directly
fn() {
cat <<EOF
$abc "$abc" '$abc'
EOF
}
abc=xyz
x=$(fn)
这篇关于ksh88将单引号更改为heredocs中的双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!