围绕扩展变量的击败Heredoc单语

发布于 2025-01-30 09:38:52 字数 565 浏览 1 评论 0原文

我需要$表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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2025-02-06 09:38:52

您确定此处文档中的变量扩展是问题吗?如果您只检查CAT命令的输出(使用bash):

$ database=database123 table=mytable_name cat <<EOF >/dev/stdout
   use $database;
   select column_name from information_schema.columns where table_name = '$table';
EOF
use database123;
select column_name from information_schema.columns where table_name = 'mytable_name';

您可能需要分解其他命令正在做的事情以确保错误在哪里。

从附带说明,您提到的是通过引号('或“)关闭可变扩展的情况,显然将此处文档的语法与管道中其他命令的不相关语法混为一谈。

例如,这是正确的:

### works, prints 'hello hello'
MYVAR='hello' cat <<EOF | grep 'hello hello'
  hello $MYVAR
EOF

相对于:相反:

### WRONG, response empty
MYVAR='hello' cat <<'EOF' | grep 'hello hello'
  hello $MYVAR
EOF

不是执行变量替换,因为 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):

$ database=database123 table=mytable_name cat <<EOF >/dev/stdout
   use $database;
   select column_name from information_schema.columns where table_name = '$table';
EOF
use database123;
select column_name from information_schema.columns where table_name = 'mytable_name';

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:

### works, prints 'hello hello'
MYVAR='hello' cat <<EOF | grep 'hello hello'
  hello $MYVAR
EOF

As opposed to:

### WRONG, response empty
MYVAR='hello' cat <<'EOF' | grep 'hello hello'
  hello $MYVAR
EOF

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 is grep 'hello hello', have quotes or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文