小编Lia*_*gan的帖子

忽略 shell 脚本输出中的错误

如果 ICMP 回显失败,我想忽略此 IF 语句中的错误。

示例代码:

if ping -q -c 1 -W 1 1.2.3.4 >/dev/null; then
  echo -e "PING OK"
else
  echo -e "PING NOK"
fi
Run Code Online (Sandbox Code Playgroud)

如果 ping 成功或者您在脚本之外运行命令,它会完美运行,但如果没有响应,则会给出以下输出。

PING 1.2.3.4 (1.2.3.4): 56 data bytes

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 0 packets received, 100.0% packet loss
PING NOK
Run Code Online (Sandbox Code Playgroud)

我已经看到了引用 2>/dev/null 的答案,但这会在输出中显示整个 ping 查询,无论成功与否!2>/dev/null 的示例如下。

PING 1.2.3.4 (1.2.3.4): 56 data bytes

--- 1.2.3.4 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = …
Run Code Online (Sandbox Code Playgroud)

bash shell if-statement ping dev-null

3
推荐指数
1
解决办法
620
查看次数

PowerShellWhere-Object 查询括号之间的空或 NULL 内容

如果我的问题没有完全描述我在这里要问的内容,我很抱歉!我不确定如何过滤大括号/括号之间的字段内容或缺失。我的热门搜索结果返回这篇文章,我认为它不能解决我的问题。

我正在尝试从 MSOnline/AzureAD 中提取禁用/未设置多重身份验证选项的所有用户。这个事实有点微不足道,但它设定了背景......

我有以下查询,它返回一个我知道没有配置 StrongAuthenticationRequirements 值的测试用户。

> 获取-MsolUser -UserPrincipalName test@contoso.com | 选择 UserPrincipalName、DisplayName、StrongAuthenticationRequirements

UserPrincipalName DisplayName StrongAuthenticationRequirements
----------------- ---------------------- ---------------------- ----------
test@contoso.com 测试帐户 {}

我还可以使用Where-Object 运行查询来查找那些确实设置了值的对象。

> 获取-MsolUser | where-object { $_.StrongAuthenticationRequirements -like "*Mi​​crosoft.Online.Administration.StrongAuthenticationRequirement*" } | 选择 UserPrincipalName、DisplayName、StrongAuthenticationRequirements

UserPrincipalName DisplayName StrongAuthenticationRequirements
----------------- ---------------------- ---------------------- ----------
test@contoso.com 测试帐户 {Microsoft.Online.Administration.StrongAuthenticationRequirement}

我的问题是;如何运行非用户特定查询来查找 StrongAuthenticationRequirement 字段为“{}”的所有实例?

提前致谢!

powershell filtering

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

bash ×1

dev-null ×1

filtering ×1

if-statement ×1

ping ×1

powershell ×1

shell ×1