我正在尝试将软件安装到多个 WSL 发行版,因此我使用批处理文件来执行此操作。
以下是示例命令及其输出:
CMD> wsl -u root -d Ubuntu-22.04 "apt-get update && apt-get upgrade -y && apt-get install curl -y"
/bin/bash: apt-get update && apt-get upgrade -y && apt-get install curl -y: command not found
但是,它可以在发行版内部运行:
CMD> wsl -u root -d Ubuntu-22.04
root@device-name:/mnt/d/username/Documents/tech-projects/exp/exp-py# apt-get update && apt-get upgrade -y && apt-get install curl -y
Hit:1 http://security.debian.org/debian-security bullseye-security InRelease
[...]
如何解决这个问题?
完整脚本,我的详细信息已删除。皮耶恩和GitHub 命令行来自各自的文档;相应的部分已链接。
wsl --shutdown
wsl -u root -d Debian "apt-get update && apt-get upgrade -y && apt-get install curl -y"
wsl -u root -d Debian "apt-get update && apt-get upgrade -y"
wsl -u root -d Debian "apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev gh git vim bat"
wsl -u root -d Ubuntu-16.04 "apt-get update && apt-get upgrade -y && apt-get install curl -y"
wsl -u root -d Ubuntu-16.04 "type -p curl >/dev/null || apt-get install curl -y curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt-get/sources.list.d/github-cli.list > /dev/null"
wsl -u root -d Ubuntu-16.04 "apt-get update && apt-get upgrade -y"
wsl -u root -d Ubuntu-16.04 "apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev gh git vim bat"
wsl -u root -d Ubuntu-16.04 "curl https://pyenv.run | /bin/bash"
wsl -u root -d Ubuntu-16.04 "echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> /home/username/.bashrc"
wsl -u root -d Ubuntu-16.04 "echo 'eval "$(pyenv init --path)"' >> /home/username/.bashrc"
wsl -u root -d Ubuntu-16.04 "echo 'eval "$(pyenv virtualenv-init -)"' >> /home/username/.bashrc"
wsl -u root -d Ubuntu-20.04 "apt-get update && apt-get upgrade -y && apt-get install curl -y"
wsl -u root -d Ubuntu-20.04 "type -p curl >/dev/null || apt-get install curl -y curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt-get/sources.list.d/github-cli.list > /dev/null"
wsl -u root -d Ubuntu-20.04 "apt-get update && apt-get upgrade -y"
wsl -u root -d Ubuntu-20.04 "apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev gh git vim bat"
wsl -u root -d Ubuntu-18.04 "apt-get update && apt-get upgrade -y && apt-get install curl -y"
wsl -u root -d Ubuntu-18.04 "type -p curl >/dev/null || apt-get install curl -y curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt-get/sources.list.d/github-cli.list > /dev/null"
wsl -u root -d Ubuntu-18.04 "apt-get update && apt-get upgrade -y"
wsl -u root -d Ubuntu-18.04 "apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev gh git vim bat"
wsl -u root -d Ubuntu-18.04 "curl https://pyenv.run | /bin/bash"
wsl -u root -d Ubuntu-18.04 "echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> /home/username/.bashrc"
wsl -u root -d Ubuntu-18.04 "echo 'eval "$(pyenv init --path)"' >> /home/username/.bashrc"
wsl -u root -d Ubuntu-18.04 "echo 'eval "$(pyenv virtualenv-init -)"' >> /home/username/.bashrc"
wsl -u root -d Ubuntu-22.04 "apt-get update && apt-get upgrade -y && apt-get install curl -y"
wsl -u root -d Ubuntu-22.04 "type -p curl >/dev/null || apt-get install curl -y curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt-get/sources.list.d/github-cli.list > /dev/null"
wsl -u root -d Ubuntu-22.04 "apt-get update && apt-get upgrade -y"
wsl -u root -d Ubuntu-22.04 "apt-get install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev gh git vim bat"
答案1
使用标志--exec bash -c
有效。
shell 脚本中整行的示例:
wsl -u root -d Ubuntu-22.04 --exec bash -c "apt-get update && apt-get upgrade -y && apt-get install curl -y"
来源:https://www.reddit.com/r/bashonubuntuonwindows/comments/1163wm0/comment/j94yu63/?context=3