围绕扩展变量的击败Heredoc单语
我需要$表var来展开,但是在保留MSSQL所需的引号为table_name参数。我不知道这是可能的,因为我一直在搜索一段时间。我看到的常见答案是,如果有任何引号,那么变量将不会扩展。根本不可能做我需要的事情吗?
代码
cat <<EOF | isql $host sa 'password' -d, | sed '-e 1,10d;$d' | sort > mssql_table_${table}_column_info
use $database;
select column_name from information_schema.columns where table_name = '$table';
EOF
所需的输出
select column_name from information_schema.columns where table_name = 'mytable_name';
请注意,该输出在表名称周围仍然有单个引号。 MSSQL选择适当的表是必要的。
I need the $table var to expand but while keeping the quotes that are required by MSSQL for the table_name parameter. I don't know if this is possible as I have been searching for a while. The common answer I see is if there are any quotes then variables won't be expanded. Is it simply not possible to do what I need here?
Code
cat <<EOF | isql $host sa 'password' -d, | sed '-e 1,10d;$d' | sort > mssql_table_${table}_column_info
use $database;
select column_name from information_schema.columns where table_name = '$table';
EOF
Desired Output
select column_name from information_schema.columns where table_name = 'mytable_name';
Notice that the output has single quotes still around the table name. This is necessary for MSSQL to select the appropriate table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定此处文档中的变量扩展是问题吗?如果您只检查CAT命令的输出(使用bash):
您可能需要分解其他命令正在做的事情以确保错误在哪里。
从附带说明,您提到的是通过引号('或“)关闭可变扩展的情况,显然将此处文档的语法与管道中其他命令的不相关语法混为一谈。
例如,这是正确的:
相对于:相反:
不是执行变量替换,因为 word
'eof'
在此处的文档中引用,不管这是不论变量的扩展。是否在管道中使用其他命令,即grep'Hello hello'
,都有报价。Are you sure the variable expansion in the here document is the problem though? If you just inspect the output of the cat command (using bash):
You might want to break down what the other commands are doing to make sure where the error is.
On a side note, your mention of turning off the variable expansion via quotation marks (' or ") apparently conflates the syntax of the here document with the unrelated syntax of other commands in the pipeline.
For example this is correct:
As opposed to:
Does not perform the variable substitution, because the word
'EOF'
is quoted in the here document, turning off variable expansion. This is regardless of whether other commands in the pipeline, that isgrep 'hello hello'
, have quotes or not.