意外标记 `' ` 附近的语法错误
Posted
技术标签:
【中文标题】意外标记 `\' ` 附近的语法错误【英文标题】:syntax error near unexpected token `' `意外标记 `' ` 附近的语法错误 【发布时间】:2016-02-29 01:03:24 【问题描述】:我正在编写一个 shell 脚本来为游戏下雨的风险制作一个新的保存文件。我正在尝试使可用的解锁部分(例如成就、怪物日志、工件等)可以一次全部添加到文件中,或者用户将能够选择哪些部分他们想要添加。我是编写 shell 脚本的新手,我不太清楚为什么在运行我目前所拥有的东西时会收到错误 syntax error near unexpected token `'我的剧本。如果有人可以向我解释为什么我会收到错误,如何修复它,和/或如何改进我的脚本,将不胜感激。如果这很重要,我也在 Mac OS X 上。这是我的脚本。
#!/bin/bash
mkdir ~/Desktop/ror_save_unlock_all
echo "This script takes ~/Library/Application Support/com.riskofrain.riskofrain/Save.ini and backs it up to ~/Desktop/ror_save_unlock_all/originalSave. It generates a new Save.ini that is built to your specifications, unlocking all items and achievements or just the ones you choose, and replaces the existing Save.ini with the new one." >> ~/Desktop/ror_save_unlock_all/README.txt
mkdir ~/Desktop/ror_save_unlock_all/originalSave
cp ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini ~/Desktop/ror_save_unlock_all/originalSave/
cd ~/Desktop/ror_save_unlock_all
echo -n 'How would you like the new Save.ini to be built? Press 1 for all unlocks or 2 to customize.'
read text
if [ $text = "1" ]
then
echo "[Achievement]" >> EOF1
count=1
while [ $count -lt 55 ]
do
echo "acheivement$count=\"2\"" >> EOF2
count=`expr $count + 1`
done
echo "[Record]" >> EOF3
count=1
while [ $count -lt 31 ]
do
echo "mons$count=\"1\"" >> EOF4
count=`expr $count + 1`
done
count=1
while [ $count -lt 110 ]
do
echo "item$count=\"1\"" >> EOF5
count=`expr $count + 1`
done
count=1
while [ $count -lt 10 ]
do
echo "artifact$count=\"1\"" >> EOF6
count=`expr $count + 1`
done
cat EOF1 EOF2 EOF3 EOF4 EOF5 EOF6 > Save.ini
rm EOF1 EOF2 EOF3 EOF4 EOF5 EOF6
cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini
echo "Original Save.ini successfully overwritten."
exit
elif [ $text = "2" ]; then
echo "You selected customize. Now we will build a custom Save.ini"
echo -n "Press 1 if you want to unlock all achievements, press 2 to continue without unlocking all achievements."
read text
if [ $text = "1" ]; then
echo "[Achievement]" >> EOF1
count=1
while [ $count -lt 55 ]
do
echo "acheivement$count=\"2\"" >> EOF2
count=`expr $count + 1`
done
echo "All achievements successfully unlocked."
echo -n "Press 1 to make the Save.ini and replace the existing one with it and then exit, or press 2 to customize it further."
read text
if [ $text = "1" ]; then
cat EOF1 EOF2 > Save.ini
rm EOF1 EOF2
cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini
echo "Original Save.ini successfully overwritten."
exit
elif [ $text = "2" ] then;
echo -n "Press 1 to unlock all monster logs, or press 2 to continue without unlocking all monster logs."
read text
if [ $text = "1" ]; then
echo "[Record]" >> EOF3
count=1
while [ $count -lt 31 ]
do
echo "mons$count=\"1\"" >> EOF4
count=`expr $count + 1`
done
echo "All achievements successfully unlocked."
echo -n "Press 1 to make the Save.ini and replace the existing one with it and then exit, or press 2 to customize it further."
read text
【问题讨论】:
你有一个不平衡的
。解决这个问题的最佳方法是很好地缩进你的代码,或者使用一个支持你的 bash 编辑器(比如 VIM)
这些大括号实际上都不是必需的。
在 shell 中,大括号用于对 I/O 重定向进行分组,而不仅仅是对命令进行分组。最后有一个右括号流;四个中的第二个应该是 fi
或在它之前需要一个 fi
。您在脚本中至少缺少两个fi
。我会让你整理出你想要保留多少个大括号(它们都不是必需的)——但你的脚本也必须在没有它们的情况下对 shell 有意义。
将代码粘贴到shellcheck.net 将突出显示语法错误。
【参考方案1】:
您的脚本有一些错误:
- 第 93 行:elif [ $text = "2" ] then;
应更改为 elif [ $text = "2" ]; then
- 如果 bash 中的命令必须具有 fi 才能关闭条件。可以参考http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html
希望对您有所帮助
【讨论】:
【参考方案2】:在决定编写脚本之前,您应该对所使用的语言有基本的了解。即使是对the documentation 或任何how-to pages 的最粗略阅读也会告诉您if
语句的正确语法。
除此之外,您的脚本还有许多效率低下的地方。反引号运算符会启动一个子 shell,即 bash
的一个新实例,因此这不一定是您想要在循环中执行 110 次的事情。
这里解决了几个较大的问题。我建议查看functions 以消除大量代码重复和条件嵌套。
#!/bin/bash
mkdir ~/Desktop/ror_save_unlock_all
echo "This script takes ~/Library/Application Support/com.riskofrain.riskofrain/Save.ini and backs it up to ~/Desktop/ror_save_unlock_all/originalSave. It generates a new Save.ini that is built to your specifications, unlocking all items and achievements or just the ones you choose, and replaces the existing Save.ini with the new one." >> ~/Desktop/ror_save_unlock_all/README.txt
mkdir ~/Desktop/ror_save_unlock_all/originalSave
cp ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini ~/Desktop/ror_save_unlock_all/originalSave/
cd ~/Desktop/ror_save_unlock_all
read -n 1 -p 'How would you like the new Save.ini to be built? Press 1 for all unlocks or 2 to customize.' text
if [ $text = "1" ]
then
echo "[Achievement]" > Save.ini
for count in 1..54; do
echo "acheivement$count=\"2\"" >> Save.ini
done
echo "[Record]" >> Save.ini
for count in 1..30; do
echo "mons$count=\"1\"" >> Save.ini
done
for count in 1..109; do
echo "item$count=\"1\"" >> Save.ini
done
for count in 1..9; do
echo "artifact$count=\"1\"" >> Save.ini
done
cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini
echo "Original Save.ini successfully overwritten."
exit
elif [ $text = "2" ]; then
echo "You selected customize. Now we will build a custom Save.ini"
read -n 1 -p "Press 1 if you want to unlock all achievements, press 2 to continue without unlocking all achievements." text
if [ $text = "1" ]; then
echo "[Achievement]" > Save.ini
for count in 1..54; do
echo "acheivement$count=\"2\"" >> Save.ini
done
echo "All achievements successfully unlocked."
read -n 1 -p "Press 1 to make the Save.ini and replace the existing one with it and then exit, or press 2 to customize it further." text
if [ $text = "1" ]; then
cp -force ~/Desktop/ror_save_unlock_all/Save.ini ~/Library/Application\ Support/com.riskofrain.riskofrain/Save.ini
echo "Original Save.ini successfully overwritten."
exit
elif [ $text = "2" ]; then
read -n 1 -p "Press 1 to unlock all monster logs, or press 2 to continue without unlocking all monster logs." text
if [ $text = "1" ]; then
echo "[Record]" >> Save.ini
for count in 1..30; do
echo "mons$count=\"1\"" >> Save.ini
done
echo "All monster logs successfully unlocked."
read -n 1 -p "Press 1 to unlock all monster logs, or press 2 to continue without unlocking all monster logs." text
if [ $text = "1" ]; then
#...
fi
fi
fi
fi
fi
【讨论】:
这是居高临下的。以上是关于意外标记 `' ` 附近的语法错误的主要内容,如果未能解决你的问题,请参考以下文章