Docker执行行为在命令行和cmd文件中不同。

huangapple go评论64阅读模式
英文:

Docker execution behavior different in command line and cmd file

问题

我在cmd文件中创建了以下脚本:

docker exec my-container bash -c "stat -c '%U' /my/directory"


我得到了以下结果:

U


但是如果我直接在命令行中执行相同的命令,我得到了这个结果:

root


如果我将U替换为A(以获得权限而不是所有权):

docker exec my-container bash -c "stat -c '%A' /my/directory"


在命令行中,我得到了以下结果:

drwxrwxrwx


但在cmd脚本中,我得到了以下结果:

A


我不明白为什么。例如,我对以下命令没有任何问题:

docker exec my-container bash -c "ls -al /"
docker exec my-container bash -c "su - -c 'ls -al /' myUser"


我在脚本和命令行中都得到了预期的结果。
英文:

I created the following script in cmd file :

docker exec my-container bash -c "stat -c '%U' /my/directory"

I have the following result :

U

But if I do the same in the command line directly, I have this :

root

If I replace U by A (to have rights instead ownership) :

docker exec my-container bash -c "stat -c '%A' /my/directory"

I have that with the command line :

drwxrwxrwx

And that with the cmd script

A

I don't understand why. For instance, I don't have any trouble with the following commands :

docker exec my-container bash -c "ls -al /"
docker exec my-container bash -c "su - -c 'ls -al /' myUser"

I have the expected result with the script and the command line.

答案1

得分: 1

以下是已翻译的内容:

"The underlying problem is windows."
根本问题是Windows。

"The %U is parsed in a batch file and the percent is removed, because there is no closing percent sign, else it would be replaced by the variable content like in echo %PATH%"
%U 在批处理文件中被解析,百分号被移除,因为没有闭合的百分号,否则它会被变量内容替代,就像在 echo %PATH% 中一样。

"On the command line exists different rules for the percent handling, therefore it works here."
在命令行中存在百分号处理的不同规则,因此在这里可以工作。

"To solve it in a batch file, just double the percent signs"
要在批处理文件中解决这个问题,只需将百分号符号加倍:

docker exec my-container bash -c "stat -c '%%U' /my/directory"
英文:

The underlying problem is windows.
The %U is parsed in a batch file and the percent is removed, because there is no closing percent sign, else it would be replaced by the variable content like in echo %PATH%

On the command line exists different rules for the percent handling, therefore it works here.

To solve it in a batch file, just double the percent signs

docker exec my-container bash -c "stat -c '%%U' /my/directory"

huangapple
  • 本文由 发表于 2023年5月22日 17:09:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定