【问题标题】:Input redirection for cmd from PowerShell来自 PowerShell 的 cmd 输入重定向
【发布时间】:2012-06-12 01:44:43
【问题描述】:

我正在尝试使用来自文件的输入来运行 mysql。我发现以下工作:

cmd /c 'C:\Program Files\MySQL...\mysql -u user -pPassword databasename -f

问题是databasename($database)和inputfilename.sql($file)是变量,需要插值。我还没有找到使插值工作的任何方法。

请注意,invoke-expression 和 & 将不起作用,因为 PowerShell 没有为输入重定向实现

有什么建议吗?

【问题讨论】:

    标签: redirect powershell input cmd


    【解决方案1】:

    我有完全和你一样的需要。以this one 等其他 SO 答案为例,这是我写的:

    $binPath = <local MySQL bin path>
    $mySql = Join-Path $binPath "mysql"
    ...
    $dosCommand = "$mySql --host=$dbHost --user=$dbUser --password=$dbPassword $dbName --verbose < ""$scriptPath"""
    cmd.exe /c $dosCommand
    

    【讨论】:

    • 实际上我现在正在修复我的代码并添加双引号,从... &lt; $scriptPath"... &lt; ""$scriptPath""",否则脚本路径包含空格时会出错。
    【解决方案2】:

    您也许可以使用 Get-Content,如在此类似问题 Why is "<" input redirect not implemented in Powershell? 中所回答的那样

    【讨论】:

      【解决方案3】:

      看起来您将整个命令用单引号括起来。变量不会被插入到单引号字符串中。试试这个:

      cmd /c 'C:\Program Files\MySQL\mysql' -u user -pPassword $database -f < $file
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-11
        • 1970-01-01
        • 2016-11-16
        • 2014-09-07
        • 1970-01-01
        • 1970-01-01
        • 2016-10-11
        • 2011-03-25
        相关资源
        最近更新 更多