相关疑难解决方法(0)

Bash 运算符 [[ vs [ vs ( vs ((?

我对这些运算符在 bash 中使用时有何不同(括号、双括号、括号和双括号)感到有些困惑。

[[ , [ , ( , ((
Run Code Online (Sandbox Code Playgroud)

我见过人们在这样的 if 语句中使用它们:

if [[condition]]

if [condition]

if ((condition))

if (condition)
Run Code Online (Sandbox Code Playgroud)

shell bash test

436
推荐指数
6
解决办法
19万
查看次数

为什么我的 shell 脚本会因空格或其他特殊字符而阻塞?

或者,有关强大的文件名处理和在 shell 脚本中传递的其他字符串的介绍性指南。

我写了一个 shell 脚本,它在大多数情况下运行良好。但它在某些输入(例如某些文件名)上窒息。

我遇到了如下问题:

  • 我有一个包含空格的文件名hello world,它被视为两个单独的文件helloworld.
  • 我有一个带有两个连续空格的输入行,它们在输入中缩小为一个。
  • 前导和尾随空格从输入行中消失。
  • 有时,当输入包含其中一个字符时\[*?,它们会被一些文本替换,这实际上是文件的名称。
  • 输入中有一个撇号'(或双引号"),在那之后事情变得很奇怪。
  • 输入中有一个反斜杠(或者:我使用的是 Cygwin 并且我的一些文件名具有 Windows 样式的\分隔符)。

这是怎么回事,我该如何解决?

shell bash shell-script quoting whitespace

341
推荐指数
3
解决办法
32万
查看次数

if 条件中的括号:为什么我会收到没有空格的语法错误?

当脚本在一年的开始两天运行时,我使用下面的脚本将两天后移,并检查每个月的第一天和第二天,然后将后移两天。

if [$month="01"] && [$day="01"]; then
        date="$last_month/$yes_day/$last_year"
        fulldate="$last_month/$yes_day/$last_year"
else
        if [$month="01"] && [$day="02"]; then
                date="$last_month/$yes_day/$last_year"
                fulldate="$last_month/$yes_day/$last_year"
        else
                if [ $day = "01" ]; then
                        date="$last_month/$yes_day/$year"
                        fulldate="$year$last_month$yes_day"
                else
                        if [ $day = "02" ]; then
                                date="$last_month/$yes_day/$year"
                                fulldate="$year$last_month$yes_day"
                        else
                                date="$month/$yes_day/$year"
                                fulldate="$year$month$yes_day"
                        fi
                fi
        fi
fi
Run Code Online (Sandbox Code Playgroud)

但我的坏消息是收到以下错误消息

Etime_script.sh: line 19: [06=01]: command not found
Etime_script.sh: line 24: [06=01]: command not found
Run Code Online (Sandbox Code Playgroud)

shell bash shell-script whitespace test

18
推荐指数
2
解决办法
7709
查看次数

标签 统计

bash ×3

shell ×3

shell-script ×2

test ×2

whitespace ×2

quoting ×1