Commands like “uname -s” is not recognized in WSL when executed from poweshell

被刻印的时光 ゝ 提交于 2021-01-27 12:41:05

问题


I need to execute the following command in WSL:

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

In order to execute it from powershell, i tried to run:

Ubuntu1804 run "sudo curl -L 'https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)' -o /usr/local/bin/docker-compose"

But errors occur as it cannot find the value of uname -s and uname -m

uname : The term 'uname' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:107
+ ... ocker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(u ...
+                                                             ~~~~~
    + CategoryInfo          : ObjectNotFound: (uname:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

But the following command works as i manually entered the value of uname -s and uname -m in that command.

 Ubuntu1804 run "sudo curl -L 'https://github.com/docker/compose/releases/download/1.23.2/docker-compose-Linux-x86_64' -o /usr/local/bin/docker-compose"

Can anyone please help me to find when using powershell, how to incorporate the results of some commands to another command and execute it in WSL?

Also, how can i incorporate the value of environment variables like $USER in WSL commands and execute from powershell?


回答1:


The problem is your subexpressions $( ) in the argument to the command. PowerShell interprets these in expression mode (because of the double-quotes which allow expansion) and is looking for an executable named uname before passing the argument to Ubuntu1804.

Solutions:

  • Use the stop-parser operator after run: --%

  • Flip your quotes so expansion doesn't happen: ... 'sudo curl -L " ...

  • Escape the subexpressions: `$`(`)

To answer how you include environment variables in the WSL command:

& Ubuntu1804.exe ... $Env:USER ...

about_Parsing

about_Environment_Variables




回答2:


For normal commands like sudo apt-get update -y just use stop-parsing operator as:

Ubuntu1804 run --% sudo apt-get update -y

For commands like curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - which includes pipeline characters (|) , using only --% will not work. In such cases enclosing the command within double quotes works as:

 Ubuntu1804 run --% "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -"

Also, avoid using usage of backslash or new line in commands when using --% operator. For example, instead of using like this:

Ubuntu1804 run --% sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

Execute like:

Ubuntu1804 run --% sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Including environment variables in the WSL command by using like $Env:USER doesn't worked for the command sudo usermod -aG docker $USER.

It is an error if i use it like this:

 Ubuntu1804 run sudo usermod -aG docker $Env:USER

or like this:

  Ubuntu1804 run --% sudo usermod -aG docker $Env:USER

What worked for me to include the environment variables of WSL in powershell commands is using the stop-parsing operator --% like this:

 Ubuntu1804 run --% sudo usermod -aG docker $USER


来源:https://stackoverflow.com/questions/55260630/commands-like-uname-s-is-not-recognized-in-wsl-when-executed-from-poweshell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!