bash - Bash:从数组的最后一个成员迭代到第一个成员
问题描述
我正在尝试查找文件夹的“根”。我正在使用以下(至少在我的脑海中)在 Bash 脚本中执行此操作:
# Get current directory (e.g. /foo/bar/my/subdir)
CURR_DIR = `cwd`
# Break down into array of folder names
DIR_ARRAY=(${CURR_DIR//\// })
# Iterate over items in DIR_ARRAY starting with "subdir"
<HELP WITH FOR LOOP SYNTAX>
# Each loop:
# build path to current item in DIR_ITER; e.g.
# iter N: DIR_ITER=/foo/bar/my/subdir
# iter N-1: DIR_ITER=/foo/bar/my
# iter N-2: DIR_ITER=/foo/bar
# iter 0: DIR_ITER=/foo
# In each loop:
# get the contents of directory using "ls -a"
# look for .git
# set ROOT=DIR_ITER
export ROOT
我已经在 Bash 中搜索了循环,但它都使用“for i in ARRAY”形式,这不能保证反向迭代顺序。实现我想做的事情的推荐方法是什么?
解决方案
关于反向索引引用的一种想法。
首先我们的数据:
$ CURR_DIR=/a/b/c/d/e/f
$ DIR_ARRAY=( ${CURR_DIR//\// } )
$ typeset -p DIR_ARRAY
declare -a DIR_ARRAY=([0]="a" [1]="b" [2]="c" [3]="d" [4]="e" [5]="f")
我们的指数列表:
$ echo "${!DIR_ARRAY[@]}"
0 1 2 3 4 5
我们在rev
erse 中的索引列表:
$ echo "${!DIR_ARRAY[@]}" | rev
5 4 3 2 1 0
循环遍历我们的反向索引列表:
$ for i in $(echo "${!DIR_ARRAY[@]}" | rev)
do
echo $i
done
5
4
3
2
1
0
至于使用这种“反向”索引策略来提升目录结构:
$ LOOP_DIR="${CURR_DIR}"
$ for i in $(echo "${!DIR_ARRAY[@]}" | rev)
do
echo "${DIR_ARRAY[${i}]}:${LOOP_DIR}"
LOOP_DIR="${LOOP_DIR%/*}"
done
f:/a/b/c/d/e/f
e:/a/b/c/d/e
d:/a/b/c/d
c:/a/b/c
b:/a/b
a:/a
虽然我们可以完成同样的事情 a) 没有数组和 b) 使用一些基本的参数扩展,例如:
$ LOOP_DIR="${CURR_DIR}"
$ while [ "${LOOP_DIR}" != '' ]
do
subdir="${LOOP_DIR##*/}"
echo "${subdir}:${LOOP_DIR}"
LOOP_DIR="${LOOP_DIR%/*}"
done
f:/a/b/c/d/e/f
e:/a/b/c/d/e
d:/a/b/c/d
c:/a/b/c
b:/a/b
a:/a
推荐阅读
- php - 试图获取非对象 laravel7.7 的属性“id”
- c# - 在 C# WPF 中“交换”窗口而不是打开一个新窗口?
- sparql - Sparql 查询显示电影列表
- python - 使用 .format 打印“{”
- javascript - 地图左上方的 react-leaflet 花括号
- xml - 将字符“°”与 UTF-8 编码 API 一起使用后的 XML 解析错误
- javascript - Javascript Array.push 创建一个我无法使用索引访问的嵌套数组
- java - 如果 Firebase 表包含来自更多应用身份验证用户的数据,我该如何遍历它?
- node.js - NextJS 无法识别生产中动态添加的静态资产
- php - laravel env 文件比配置文件更强大吗?