每次我设置一个新的 linux 系统时,我都试图制作一个 bash 脚本。我的问题是我收到一条错误消息,提示解析错误:
condition expected: =
我认为问题在于它在 while 循环内,因为当我删除它时它会起作用,但 while 循环对我的程序来说有点重要,所以不能删除它。该脚本旨在制作一个列表,显示我可以在我的脚本中执行的选项,它应该有颜色和一种检查该选项之前是否运行过的方法。该脚本似乎在该行之后停止工作: while [ $loop = 1 ];做 当我尝试在没有 while 循环的情况下运行时它起作用了,所以我认为这可能是问题的原因。
loop=1
scriptstatus=(0 0 0 0 0 0 0 0 );
whatitdoes=('Install terminal programs' 'Install security programs' 'Install normal programs' 'Install spotify' 'Customize gnome' 'Install and customize zsh' 'Install etcher' 'Not yet run' 'Not yet run');
#'$(tput bold)$(tput setaf 1)Not yet run$(tput sgr0)'
while [ $loop = 1 ]; do
answer=""
clear
echo "$(tput bold) Script: What script does: Status:$(tput sgr0)"
COUNTER=0
while [ $COUNTER -lt 7 ]; do
if [[ $scriptstatus[$COUNTER] = 0 ]]
then
echo " ["$COUNTER"] "$'\x1d'" ${whatitdoes[$COUNTER]} "$'\x1d'" $(tput bold)$(tput setaf 1)Not yet run$(tput sgr0)"
elif [[ $scriptstatus[$COUNTER] = 1 ]]
then
echo " ["$COUNTER"] "$'\x1d'" ${whatitdoes[$COUNTER]} "$'\x1d'" $(tput bold)$(tput setaf 2)Completed$(tput sgr0)"
else
echo "error"
fi
let COUNTER=COUNTER+1
done | column -t -s$'\x1d'
echo "quit: Exits the program"
echo "Choose one option:"
read answer
done
最佳答案
原来我只是把数组扩展写错了;而不是:
if [[ $scriptstatus[$COUNTER] = 0 ]]
我必须使用:
if [[ ${scriptstatus[$COUNTER]} = 0 ]]
数组下标需要 {
和 }
。
关于linux - Bash 脚本中的解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45070547/