Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本不工作。有人知道为什么吗?_Bash_Syntax - Fatal编程技术网

Bash脚本不工作。有人知道为什么吗?

Bash脚本不工作。有人知道为什么吗?,bash,syntax,Bash,Syntax,我用bash脚本编写了这段代码。当我运行它时,会出现提示“开始?[y/n]”,但无论我如何响应,它都会关闭窗口。另外,if语句的正确语法是什么 @echo off set /p choice="Start? [y/n] " echo $choice echo $choice$ if $choice$ == "y" then goto Labelyes fi if $choice$ == "n" then exit fi :Labelyes echo YAY set /p goodbye="

我用bash脚本编写了这段代码。当我运行它时,会出现提示“开始?[y/n]”,但无论我如何响应,它都会关闭窗口。另外,if语句的正确语法是什么

@echo off
set /p choice="Start? [y/n] "
echo $choice
echo $choice$
if $choice$ == "y"
then 
goto Labelyes
fi

if $choice$ == "n"
then
exit
fi

:Labelyes
echo YAY
set /p goodbye="BYE"
if [1==1]; then exit; fi
谢谢大家!


您的脚本通常是不正确的,因此思考它为什么不工作是没有意义的。

如前所述,您使用的语法更像是Microsoft Windows批处理(
.bat
)文件,而不是其他任何文件。下面是对bash的转换。要查看read的语法,请键入
help read

if
语句(read
manbash
or)的语法一开始很混乱。
[[
实际上是一个进行模式匹配的命令,下面给出了一个简单的示例。模式只能位于
=
的右侧。请注意,
==
也可以使用,但标准要求使用
=

简单测试也可以使用
test
命令或
[
。算术测试使用

goto
是一个由四个字母组成的单词,不用于礼貌圈(有些人不同意)


您可以使用
case
语句,但在尝试之前先理解
if
语句。

对于初学者来说,这不是
bash
。它看起来更像是DOS批处理脚本。使用“read”命令:获取基本的bash教程,然后在搜索引擎中键入“bash tutorial”。
#!/usr/bin/env bash

#set /p choice="Start? [y/n] "
read -p "Start? [y/n] " choice

echo "$choice"
#echo $choice$

if [[ $choice = [Yy]* ]]   # Accepts y, Y, yes, Yup, etc...
then
    #goto Labelyes
    echo YAY               # Quotes are optional, but beware embedded whitespace
    #set /p goodbye="BYE"
    read -p "BYE" goodbye

elif [[ $choice == "n" ]]  # Only n   "elif" means "else if"
then
    exit
else
    # What happens if neither Y nor N were given?
    echo "Invalid response, exiting anyway"
fi

#I have no idea what this is supposed to be for
#if [1==1]; then exit; fi