IF 语句期间批处理脚本硬崩溃

时间:2021-06-19 23:39:36

标签: if-statement batch-file crash

我正在编写批处理脚本来为 RPG 生成字符的文本描述。出于某种原因,在这一行很难崩溃:

if "%sex%" equ "male" (
set title=Prince
)

我已经尝试了我所知道的 if 语句的所有其他格式,包括更简单的:

if %sex%==male set title=Prince

我知道是这一行导致了问题,因为我已经使用暂停/回声标记来准确地找到崩溃发生的位置,并且是它到达该语句的那一刻(%sex% 的其他选项与测试目的无关很难编码为男性)。我会在这里发布整个代码,但目前它有 5,537 行(我知道我真的不应该批量进行程序生成,但这就是我所知道的......)

该脚本在开头有一些内容,这可能会使事情与正常情况有所不同。具体以以下开头:

chcp 65001

@echo off
setlocal ENABLEDELAYEDEXPANSION
mode con cols=150 lines=50
CD %~dp0/data

据我所知,chcp 65001 应该做的就是强制程序接受 Unicode 字符。为了创建某些名称,它必须这样做。由于从大约 80 个文件中提取数据,它还必须处理该数据目录。

我不确定这些会如何影响检查一个变量并根据它设置另一个变量的简单 if 语句。特别是当我在脚本中有数百条这样的语句而其他语句都运行良好时。

从 CMD 运行它作为 Squashman sugested 输出以下内容:

 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>if Ros EQU Ros set parentnames=Your parents were King Camden and Queen Reece of Wigord, a small monarchy making up one of the many nations on Ros.
 
 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>goto FMRoyTitleSelect
 
 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>if Ros EQU Bel goto titleskip
 
 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>if Ros EQU Avar goto titleskip
 
 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>if Ros EQU Tavros Station goto titleskip
 
 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>if Ros EQU Ros if male == male set title=Prince
 
 C:\Users\Meep\Desktop\Steel Tech Autobigraphy\data>if Ros EQU Ros if male == female set title=Princess
 set was unexpected at this time.

应 Squashman 的要求,这里是完整的 if 语句表,以及接下来发生的代码。

:FMRoyTitleSelect
if %homeworld% equ Bel goto titleskip
if %homeworld% equ Avar goto titleskip
if %homeworld% equ Tavros Station goto titleskip
if %homeworld% equ Ros if %sex%==male set title=Prince
if %homeworld% equ Ros if %sex%==female set title=Princess
if %homeworld% equ Pernia if %sex%==male set title=Lord
if %homeworld% equ Pernia if %sex%==female set title=Lady
if %homeworld% equ Temian if %sex%==male set title=Heir
if %homeworld% equ Temian if %sex%==female set title=Heiress
if %homeworld% equ Fables if %sex%==male set title=Thane
if %homeworld% equ Fables if %sex%==female set title=Thane
if %homeworld% equ Kelezza if %sex%==male set title=Master
if %homeworld% equ Kelezza if %sex%==female set title=Mistress
set boop=%title% %firstname%
set firstname=%boop%
echo title selected
pause

有什么建议吗?

这是产生错误的最短代码片段:

chcp 65001

@echo off
title test system
setlocal enabledelayedexpansion

::------- setting varriables staticly for test purposes -----------

set sex=male
set homeworld=Ros
set kingdom=Somewhereia
set dad=Dad
set mom=Mom

::-------- vvv code in question vvv --------

:FMRoyA
if %homeworld% equ Ros goto FMRoyA1
if %homeworld% equ Pernia goto FMRoyA2
if %homeworld% equ Temian goto FMRoyA3
if %homeworld% equ Fables goto FMRoyA4
if %homeworld% equ Bel goto FMRoyA5
if %homeworld% equ Avar goto FMRoyA6
if %homeworld% equ Kelezza goto FMRoyA7
if %homeworld% equ Tavros Station goto FMRoyA8
:FMRoyA1
if %homeworld% equ Ros set parentnames=Your parents were King %dad% and Queen %mom% of %kingdom%, a small monarchy making up one of the many nations on Ros.
goto FMRoyTitleSelect
:FMRoyA2
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 50 set parentnames=Your parents were Lord Governer %dad% and %mom% of %kingdom%, the ruling family of Pernia.
if %choice% gtr 50 set parentnames=Your parents were Lord Governer %mom% and %dad% of %kingdom%, the ruling family of Pernia.
goto FMRoyTitleSelect
:FMRoyA3
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 50 set parentnames=Your parents were Grand Minister %dad% and %mom% of %kingdom%, one of the many nations of Temian.
if %choice% gtr 50 set parentnames=Your parents were Grand Minister %mom% and %dad% of %kingdom%, one of the many nations of Temian.
goto FMRoyTitleSelect
:FMRoyA4
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 50 set parentnames=Your parents were High King %dad% of Fables, and %dp2% concubine %mom%.
if %choice% gtr 50 set parentnames=Your parents were High Queen %mom% of Fables, and %mp2% concubine %dad%.
goto FMRoyTitleSelect
:FMRoyA5
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 50 set parentnames=Your parents were President %dad% %lastname% of Bel, and %dp2% wife %mom%.
if %choice% gtr 50 set parentnames=Your parents were President %mom% %lastname% of Bel, and %mp1% husband %dad%.
goto FMRoyFin
:FMRoyA6
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 50 set parentnames=Your parents were Prime Minister %dad% of Avar, and %dp2% concubine %mom%.
if %choice% gtr 50 set parentnames=Your parents were Prime Minister  %mom% of Avar, and %mp1% concubine %dad%.
goto FMRoyFin
:FMRoyA7
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 50 set parentnames=Your parents were Lord Governer %dad% of Kelezza, and %dp2% concubine %mom%.
if %choice% gtr 50 set parentnames=Your parents were Lord Governer %mom% of Kelezza, and %mp2% concubine %dad%.
goto FMRoyTitleSelect
:FMRoyA8
set /a choice=(%RANDOM% %% 100)+1
if %choice% leq 33 set parentnames=You are the child of Captain Steel, and your mother %mom%.
if %choice% gtr 33 if %choice% leq 66 set parentnames=You are the child of Captain Steel, and your fauther %dad%.
if %choice% gtr 66 if %choice% leq 98 set parentnames=You are the clone of Captain Steele, and were raised mostly by their dear friend %mom%.
set steeleflag=1
goto FMRoyFin
:FMRoyTitleSelect
if %homeworld% equ Bel goto titleskip
if %homeworld% equ Avar goto titleskip
if %homeworld% equ Tavros Station goto titleskip
if %homeworld% equ Ros if %sex%==male set title=Prince
if %homeworld% equ Ros if %sex%==female set title=Princess
if %homeworld% equ Pernia if %sex%==male set title=Lord
if %homeworld% equ Pernia if %sex%==female set title=Lady
if %homeworld% equ Temian if %sex%==male set title=Heir
if %homeworld% equ Temian if %sex%==female set title=Heiress
if %homeworld% equ Fables if %sex%==male set title=Thane
if %homeworld% equ Fables if %sex%==female set title=Thane
if %homeworld% equ Kelezza if %sex%==male set title=Master
if %homeworld% equ Kelezza if %sex%==female set title=Mistress
set boop=%title% %firstname%
set firstname=%boop%
:titleskip
:FMRoyFin
if steeleflag=1 goto steelespacial
set /a choice=(%RANDOM% %% 8)+1
if %choice% equ 1 set parentsatt=Your parents were very distant. blah blah blah
if %choice% equ 2 set parentsatt=Your parents were distant. blah blah blah
if %choice% equ 3 set parentsatt=Your parents provided you with the normal blah blah blah
if %choice% equ 4 set parentsatt=Your parents set asside matters of state blah blah blah
if %choice% equ 5 set parentsatt=Your parents made riasing you a priority. blah blah blah
if %choice% equ 6 set parentsatt=Your parents waned to care for you, but blah blah blah
if %choice% equ 7 set parentsatt=Your parents did their best to raise you, but blah blah blah
if %choice% equ 8 set parentsatt=Your parents never wanted to have you. blah blah blah
goto elemntaryschool
:steelespacial
set parentsatt=blah blah blah
goto elemntaryschool
:elemntaryschool
echo %firstname% %parentsname% %parentsatt%
pause
exit

0 个答案:

没有答案