两个Shell参数扩展一个接一个不起作用(Bash) [英] Two Shell Parameter Expansions one after the next doesn't work (Bash)

查看:54
本文介绍了两个Shell参数扩展一个接一个不起作用(Bash)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Bash 4.4,我试图从当前目录中获取文件列表,将它们放入数组中,然后使用shell参数扩展删除路径中包含/cache/和/tmp/的文件从数组中获取.

Using Bash 4.4 I'm trying to get a list of the files from the current directory, put them into an array, and then use shell parameter expansion to remove the files that contain /cache/ and /tmp/ in their paths from the array.

这是我到目前为止的内容,但是没有用.问题似乎是第二个字符串替换发生在第一个字符串将其结果存储在first_array中之前.因此,当执行第二次替换导致second_array为空时,first_array尚无任何值.目的是获取从昨天的日期开始具有时间戳的文件列表,这些文件的路径中不包含/cache/或/tmp/.

This is what I have so far but it doesn't work. The problem seems to be that the second string replacement happens before the first one stores its result in first_array. So first_array has no value yet when the second replace executes resulting in second_array being blank. The goal is to get a list of files that have a timestamp from yesterday's date that don't contain /cache/ or /tmp/ in their paths.

#!/bin/bash

FIND="$(find . -type f -newermt $(date -d 'yesterday 13:00' '+%Y-%m-%d') ! -newermt $(date '+%Y-%m-%d'))"
readarray -t my_array <<<"$FIND"
first_array="${my_array[@]//*\/tmp\/*/}"
second_array="${first_array[@]//*\/cache\/*/}"

推荐答案

过滤掉 find 中的多余路径,并使用 null 分隔来自find的输出:

Filtering-out unwanted paths within find, and populating the array with null delimited output from find:

readarray -d '' -t my_array < <(
  find . -type f \
    -not \( \
      -path '*/tmp/*' -o -path '*/cache/*' \
    \) \
    -newermt "$(date -d 'yesterday 13:00' '+%Y-%m-%d %H:%M:%S')" \
    -not -newermt "$(date '+%Y-%m-%d')" \
    -print0
)

这篇关于两个Shell参数扩展一个接一个不起作用(Bash)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆