小编Lin*_*uel的帖子

如何通过当前 shell (zsh/bash) 检测数组起始索引?

我们都知道 Bash 中的数组从 0 开始索引,而 zsh 中的数组从 1 开始索引。

如果我无法确保运行环境是 bash、zsh 或其他环境,脚本如何知道它应该使用 0 或 1?

预期代码示例:

#!/bin/sh

detect_array_start_index(){
  # ... how?
  echo 1
}
ARR=(ele1 ele2)
startIndex=$(detect_array_start_index) # 0 or 1
for (( i=${startIndex}; i < ${#ARR[@]} + $startIndex; i++ )); do
    echo "$i is ${ARR[$i]}"
done
Run Code Online (Sandbox Code Playgroud)

我有一个想法是找到固定数组中第一个值的索引,我得到了这个:Get the index of a value in a Bash array,但接受的答案使用 bash 变量间接语法${!VAR[@]},这在 zsh 中无效。

arrays indexing bash shell zsh

5
推荐指数
1
解决办法
864
查看次数

为什么在Makefile中使用shell命令导出变量需要很长时间?

这是我的测试 Makefile:

VERSION ?= $(shell cat VERSION | grep "VERSION" | cut -d'=' -f2)
VERSION_MAJOR ?= $(shell echo $(VERSION) | cut -d'.' -f1)
VERSION_MINOR ?= $(shell echo $(VERSION) | cut -d'.' -f2)
VERSION_PATCH ?= $(shell echo $(VERSION) | cut -d'.' -f3)

DATE ?= "$(shell date +"%Y-%m-%dT%H:%M")"

SOME_NESTED ?= "-X=aaa=$(VERSION_MAJOR) -X=bbb=$(VERSION_MINOR) -X=ccc=$(VERSION_PATCH) -X=ddd=$(DATE)"

#.EXPORT_ALL_VARIABLES:


t:
        echo "Test $(SOME_NESTED)"
Run Code Online (Sandbox Code Playgroud)

和一个测试文件VERSION

VERSION ?= $(shell cat VERSION | grep "VERSION" | cut -d'=' -f2)
VERSION_MAJOR ?= $(shell echo $(VERSION) | cut -d'.' …
Run Code Online (Sandbox Code Playgroud)

shell makefile

4
推荐指数
1
解决办法
88
查看次数

标签 统计

shell ×2

arrays ×1

bash ×1

indexing ×1

makefile ×1

zsh ×1