设置:pipefail 在 WSL windows 11 ubuntu 22.04.3 上失败

设置:pipefail 在 WSL windows 11 ubuntu 22.04.3 上失败

我目前在运行 .sh 文件时遇到问题,可以在此处找到

https://github.com/grugslair/Rising-Revenant/blob/dev/contracts/scripts/default_auth.sh

我最初是双启动 Windows 和 ubuntu 22.04,以前没有出现这个错误,而且脚本运行良好,所以我知道这不是问题。

但出于工作原因,我现在必须使用 WSL,但现在我似乎无法再运行它了,我一直收到此信息

    alexhalo@Lappy:~/Rising-Revenant$ bash ./contracts/scripts/default_auth.sh
: invalid option nameefault_auth.sh: line 2: set: pipefail

我曾在其他地方询问过,他们告诉我进行一些检查以查看是否一切正常使用,以下是输出:

which bash
/usr/bin/bash

alexhalo@Lappy:~/Rising-Revenant$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.3 LTS
Release:        22.04
Codename:       jammy

被告知将脚本更改为仅使用 -eu 并将 pushd 更改为 cd...但这不起作用

我一直关注着这些人在这里说的话(如何运行.sh 脚本?)授予可执行权限,但没有任何

我也关注了这一点(“set -e -o pipefail” 在 Ubuntu 16 上的 bash 脚本中不起作用)并尝试运行所接受答案中的每一行并获得与该人相同的结果,但我的脚本仍然无法运行。

--编辑:只是为了澄清我所说的运行每一行是什么意思,我的意思是

# Verifying what /bin/sh is symlinked to dash
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 2月  17  2016 /bin/sh -> dash
# Verify that pipefail doesn't exist as option for dash
$ dash    
$ set -o | grep pipefail                                              
$ set -o pipefail
dash: 1: set: Illegal option -o pipefail
$ sh
$ set -o pipefail
sh: 1: set: Illegal option -o pipefail
# Try this same option in bash
$ bash --posix
bash-4.3$ set -o pipefail
bash-4.3$  
# no error

还尝试禁用 dash 作为默认终端(使用 sudo dpkg-reconfigure dash),所以我会得到 bash 但仍然会收到错误。

这里只有这个可能的答案(“set -eo pipefail” 在 Windows Subsystem for Linux(Ubuntu 16.04)中不起作用) 我还没有尝试过,但说实话,我不明白这个答案是否真的是一个答案,或者这个人只是举个例子。

任何帮助,将不胜感激 :)

答案1

可能您需要做的就是使用正确的(Unix 风格)行尾保存脚本。

如果选项名称确实有问题,则错误消息将如下所示

./contracts/scripts/default_auth.sh: line 2: set: pipefail: invalid option name

然而你所看到的

: invalid option nameefault_auth.sh: line 2: set: pipefail

这是因为您的脚本已使用 Windows 行尾保存,由两个字符 CR 和 LF 组成,而不是标准的单个字符 LF。因此,bash 将命令名称解析为pipefail\r,(它不将其识别为有效的选项名称),并且终端看到

./contracts/scripts/default_auth.sh: line 2: set: pipefail

然后回车,然后

: invalid option name

它将覆盖消息的第一部分,从而导致

: invalid option nameefault_auth.sh: line 2: set: pipefail

也可以看看:

相关内容