Shell if else fi语法错误,而实际语法似乎正确
因此,我有一个shell脚本,它包含一个大的Shell if else fi语法错误,而实际语法似乎正确,shell,if-statement,sh,Shell,If Statement,Sh,因此,我有一个shell脚本,它包含一个大的if/fi块,在我决定为这个大的if/fi块放置一个else案例之前,它工作得很好。现在我得到了这个错误: /root/VVPN/Scripts/scriptPrincipal.sh: line 201: syntax error near unexpected token `else' /root/VVPN/Scripts/scriptPrincipal.sh: line 201: `else' 我浏览了8~10篇stackoverflow文章,其
if
/fi
块,在我决定为这个大的if
/fi
块放置一个else
案例之前,它工作得很好。现在我得到了这个错误:
/root/VVPN/Scripts/scriptPrincipal.sh: line 201: syntax error near unexpected token `else'
/root/VVPN/Scripts/scriptPrincipal.sh: line 201: `else'
我浏览了8~10篇stackoverflow文章,其中的人都犯了完全相同的错误,只是都是简单的语法错误,比如[
的if语句,或:
而不是;
在然后
关键字之前,或其他
用于已用fi
关闭的if
,等等…(你明白了:p
)
然而,我已经一次又一次地检查了我的代码中所有这些错误,当涉及到if
/else
/fi
语法时,一切似乎都是正确的。我甚至向一些同事展示了代码,他们也找不到这个错误的原因
代码如下:
if [ ${CP} != "continue" ]
then
echo 'Downloading the necessary files from the server...'
# If folder F*** doesn't exist in /root/VVPN/Numbers
if [ ! -d ${N} ]
then
# Create folders F***, and F***/Results
mkdir ${N}
mkdir ${N}/Results
cd ${N}
# Get real number to factorize from server (e.g: wget server.com:8000/route/to/F1067/F1067)
ROUTE="VVPN/Numbers/${N}/${N}"
REAL_NUM_FILENAME=${N}
while true
do
wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${REAL_NUM_FILENAME} --continue
if [ $? -eq 0 ]; then
break
fi
sleep 1
done
# Get ECM-Program tuned for this number from server (e.g: wget server.com:8000/route/to/F1067/ecm)
ROUTE="VVPN/Numbers/${N}/ecm"
PROGRAM_NAME='ecm'
while true
do
wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${PROGRAM_NAME} --continue
if [ $? = 0 ]; then
break
fi
sleep 1
done
else
# The folder already exists, now we have to check whether the number has ",c" label or not
cd ${N}
fi
# Give the permission to execute program
chmod +x ecm
# Make 6 directories, one for each SPE
for i in {1..6}
do
mkdir "SPE${i}"
cp ecm "SPE${i}/"
done
# if there is no checkpoints:
if [ $CP = $N ]
then
# Get currentSigma for this number from server (e.g : wget server.com:8000/rout/to/F1067/currentSigma.txt)
ROUTE="VVPN/Numbers/${N}/currentSigma"
REAL_FILE_NAME='sigma'
while true
do
wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${REAL_FILE_NAME} --continue
if [ $? = 0 ]; then
break
fi
sleep 1
done
else
#The number has a ",c" label (= w/ checkpoint)
# Get i (server sigma) and C (current job counter) from the server (e.g: wget server.com:8000/route/to/F1067/icy)
ROUTE="icy?number=${N}"
SIGMA_C_Y='icy'
while true
do
wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${SIGMA_C_Y} --continue
if [ $? = 0 ]; then
break
fi
sleep 1
done
i=$(cat ${SIGMA_C_Y} | cut -d "," -f1)
C=$(cat ${SIGMA_C_Y} | cut -d "," -f2)
Y=$(cat ${SIGMA_C_Y} | cut -d "," -f3)
echo $i > sigma
echo $C > /root/VVPN/Scripts/C
# Get the checkpoints from the server
ROUTE="VVPN/Numbers/${N}/CP/${i},${C},${Y}"
for speNum in {1..6}
do
SPE="SPE${speNum}"
touch $SPE/again
for bigX in {0..3}
do
CHECKPOINT="pointsCurve${bigX}.${Y}"
while true
do
wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE}/${SPE}/${CHECKPOINT} -O ${CHECKPOINT} --continue
if [ $? = 0 ]; then
mv $CHECKPOINT $SPE
break
fi
sleep 1
done
done
done
fi
cd ..
else
echo "Found ${N}'s folder on this PS3"
fi
因此,错误中提到的else(第201行)实际上是代码中的最后一个else。如果没有这个else和后面的echo,脚本可以正常工作
任何帮助都将不胜感激。
:)
如果[$CP=$N]如下所示,您是否可以更改
if [ "$CP" == "$N" ]
这不是if-else问题;例如,如果您运行以下代码,输出是相同的。所以您应该将其他命令集中在if语句中
CP="continue1"
if [ ${CP} != "continue" ]
then
while #this is wrong
echo ok
echo ok
echo ok
else
fi
./test.sh: line 8: syntax error near unexpected token `else'
./test.sh: line 8: `else'
你能把这归结为一个问题吗?在你发布的代码中有很多
if
语句,我们无法看到哪一行是200/201。@ghoti好的,我会看看我是否可以制作一个仍然产生错误的简化版本,并编辑帖子。很抱歉,我不完全理解你的意思!我还在学习shell,所以我不太确定w你所写的while部分在语法上是否正确(如果不正确,是否是为了论证而故意的)但无论如何,这与我脚本中的内容并不完全相同!Namefie,他的观点是,在他的示例中,roguewhile
产生了与您看到的相同的意外标记“else”错误。您代码中的错误不在else
,而是在前面的某个地方,导致shell以您不知道的方式解析脚本照料。