powershell - Powershell错误:重定向运算符后缺少文件规范
问题描述
我刚开始Docker教程,下载后按照官方主页上的命令
PS C:\COCcal> cat > Dockerfile <<EOF
>> FROM busybox
>> CMD echo "Hello world! This is my first Docker image."
>> EOF
给我
At line:1 char:19
+ cat > Dockerfile <<EOF
+ ~
Missing file specification after redirection operator.
At line:1 char:18
+ cat > Dockerfile <<EOF
+ ~
The '<' operator is reserved for future use.
At line:1 char:19
+ cat > Dockerfile <<EOF
+ ~
The '<' operator is reserved for future use.
At line:2 char:1
+ FROM busybox
+ ~~~~
The 'from' keyword is not supported in this version of the language.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingFileSpecification
这个错误。我在谷歌上搜索了错误消息,但找不到与我相同的案例。是否缺少任何设置?或者我该怎么做才能使用这些命令?我提前感谢它。
解决方案
您似乎正在 PowerShell 中运行 Linux shell 命令。根据您的配置,这可能会有问题。我建议打开 Linux shell 或使用 PowerShell 支持的命令。
# Run the Set-Content Command Below
Set-Content -Path Dockerfile
# After Running the above command, I am prompted to enter data:
Value[0]: FROM busybox
Value[1]: CMD echo "Hello world! This is my first Docker image."
Value[2]:
运行上述命令时,系统会提示您输入Value[0]
、Value[1]
等(前提是您在每行输入数据后按 Enter 键),直到您在没有任何其他输入的行上按 Enter 键。这些输入中的每一个都将位于Dockerfile
.
关于PowerShell 中的重定向>
运算符,仅支持、>>
和>&1
。如果cat
完全有效,那是因为它是一个别名。您可以运行Get-Alias cat
以查看它映射到哪个命令。在我的系统上,即Get-Content
.
如果您不需要具有 Linux 命令提供的相同体验,还有其他 PowerShell 方法可以完成此任务。下面只是一个例子。
$Content = @'
line 1 stuff
line 2 stuff
line 3 stuff
'@
$Content | Set-Content -Path Dockerfile
UTF8NoBOM
有关使用编码的输出,请参见下文。
$Content = @'
line 1 stuff
line 2 stuff
line 3 stuff
'@
[IO.File]::WriteAllLines('C:\COCcal\Dockerfile',$Content,[Text.UTF8Encoding]::new($false))
我不能说相对于 Docker 运行这个 Docker 配置。因此,在尝试运行本机 PowerShell 命令时可能需要考虑一些事项。
- 如果
Dockerfile
需要存在某些行尾字符,则Set-Content可能会在 Docker 不喜欢的每一行上使用回车符和换行符的某种组合。 - Docker 可能期望
Dockerfile
. 虽然Set-Content
确实提供了-Encoding
参数,但它仍然可能无法提供您需要的东西。例如,您可以使用-Encoding UTF8
. 在 Windows PowerShell 中,这将是带有 BOM 的 UTF8(在 Windows PowerShell 中,此命令没有 No BOM 选项)。Windows PowerShell 中的默认编码Default
可能是ANSI
. 在 PowerShell Core 中,默认编码是UTF8NoBOM
.
推荐阅读
- java - 将方法的返回值分配给 Java 中的 Class 类型变量时会发生什么?
- html5-canvas - 如何在最大化的画布元素中调整 Pixi.js 舞台并使其居中对齐?
- oracle - 使用 PL/SQL 从表中检索数据
- php - GET 参数 PHP
- python - 如何加载使用 make_image_classifier 工具保存的 Tensorflow 模型
- javascript - 动作在 reactjs 中可能没有未定义的“类型”属性
- python - Django password_reset 与 appname
- r - 使用 ifelse,我可以创建范围并在它为 false 的情况下输出空白,这样我就可以创建另一个 ifelse
- bootstrap-4 - 图像流体不缩放 col 大小
- bash - 对失败的 cron 作业发出警报