linux - Bash 脚本中的解析错误

标签 linux bash

每次我设置一个新的 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/

相关文章:

linux - Spark配置,SPARK_DRIVER_MEMORY、SPARK_EXECUTOR_MEMORY、SPARK_WORKER_MEMORY有什么区别?

linux - 内核模块 makefile 中的 "modules"

linux - Bash循环遍历包括隐藏文件的目录

bash - 在双引号内执行本地 bash 变量

linux - 使用awk解析配置

bash - 在非交互式Shell session 中运行多个命令并解析输出

linux - git 删除系统中不再存在的文件

c - sysconf(_SC_CLK_TCK) 它返回什么?

java - bash -c遇到参数麻烦

sql-server - 是否可以使用 Haskell 和 Linux 连接到 SqlServer (MSSQL)?