卡在 if 或循环

Stuck on if or loop

我正在为我的 Linux 制作一个 bash 脚本。但我被困在 if 或 loop 上。如果我 select 是 spamming ==yes.

 echo "Found a previously saved file. Do you want to continue with this? (Yes/No)"
    read Response
if [["$Response" == "Yes" || "$Response" == "yes" || "$Response" == "y" || "$Response" == "Y"]]; then
    echo "Success" 
    else
    echo "Fail"
    fi

您需要 [[" 之间以及 "]] 之间的 space。当使用 [[]] 进行测试时,您还可以在字符串周围放置 " - 但将 space 留在那里。否则,bash 会这样做:

[[yes == Yes

回复:

[[yes: command not found...

|| 让它尝试下一部分,它在这里执行成为“神奇”循环的命令:

yes == yes

yes 实际上是一个命令,它会重复你给它作为参数的内容(它默认为 y 能够做 yes | program 来回答所有问题 programy 询问)。它将继续重复相同的事情,直到 stdout 关闭(接收器关闭 stdin)。

所以,它会源源不断地打印== yes

修复:

#!/bin/bash

echo "Found a previously saved file. Do you want to continue with this? (Yes/No)"
read -r Response

if [[ $Response == Yes || $Response == yes || $Response == y || $Response == Y ]]; then
    echo "Success" 
else
    echo "Fail"
fi