bash 脚本 : how can we use nested if in a for loop

标签 bash scripting

#! /bin/bash

count=1
step=(a b)

for x in 0 1
do
    if [[ $count != '0' ]]; then
        if [[ ${step[x]} = "a"]]; then
            echo "Python test ($count)"
        else
            echo "stress test"
        fi
    fi
done

我得到以下错误

syntax error in conditional expression: unexpected token `;'
line 20: syntax error near `;
line 20: `        if [[ ${step[x]} = "a"]]; then'

为什么?

最佳答案

第二个 if 中的 "a"]] 之间需要一个空格。

从技术上讲,[[]] 必须是单独的标记,而 bash 的解析器不会在引号或大多数标点符号上分隔标记。

我相信您的原始代码相当于 if [[ ${step[x]} = "a]]";那么 如果这会使问题更加明显。

关于bash 脚本 : how can we use nested if in a for loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4880369/

相关文章:

string - 测试字符串是否包含文字星号

node.js - 使用 shell 选项将 bash 与 Node.js child_process 一起使用失败

bash - 如何从源 bash 脚本返回错误

linux - 如何在父 shell 和子 shell 之间使用相同的 bash 变量

linux - BASH-查找相同大小的文件,使用 cksum,删除 dupe 但保留其名称作为符号链接(symbolic link)

python - 以节省内存的方式在 Python 中从字典创建迭代器

bash - 用于确定 Mac 机器是否在 Intel 上的 Apple Silicon 上运行的 CLI 命令?

bash - Find -type d 不带子文件夹

linux - 使用特定部分的 shell 从属性文件中获取值

php - 使用 SwiftMailer 批量发送电子邮件