【问题标题】:(pdksh) Looping through n files and store their dates in array(pdksh) 循环遍历 n 个文件并将它们的日期存储在数组中
【发布时间】:2015-06-02 11:03:36
【问题描述】:

使用 pdksh

stat() 命令在系统上不可用。

我需要遍历找到的文件数量并将它们的日期存储在一个数组中$COMMAND 存储在$location 中找到的文件数,如下所示。

有人可以帮帮我吗?

COMMAND=`find $location -type f | wc -l`
CMD_getDate=$(find $location -type f | xargs ls -lrt | awk '{print $6} {print $7}')

【问题讨论】:

  • 好的,你希望最终的数组是什么样子的?只是一个日期数组,无法确定哪个文件名与哪个日期对应?什么样的日期 - 像Tue Jun 2 08:58:06 EDT 2015 这样的字符串? 1433249886 之类的 time_t 值? ISO 8601,如2015-06-02T12:58:06Z?我假设您想要 mtime(而不是 atime 或 ctime)?
  • 如果我有 2 个数组,那么我就能知道。这是因为它们会事先排序,不是吗?例如,我只需要保存日期 6 月 2 日.. 然后我对文件名、时间等执行相同操作..

标签: arrays shell scripting pdksh


【解决方案1】:

嗯,首先,您不需要执行wc。数组的大小会告诉你有多少。这是构建日期和名称数组的简单方法(专为 pdksh 设计;在 AT&T ksh 或 bash 中有更好的方法):

set -A files 
set -A dates
find "$location" -type f -ls |&
while read -p inum blocks symode links owner group size rest; do 
  set -A files "${files[@]}" "${rest##* }"
  set -A dates "${dates[@]}" "${rest% *}"
done

这是检查结果的一种方法:

print "Found ${#files[@]} files:"
let i=0
while (( i < ${#files[@]} )); do
  print "The file '${files[i]}' was modified on ${dates[i]}."
  let i+=1
done

这将为您提供完整的日期字符串,而不仅仅是月份和日期。这可能是您想要的 - ls -l(或 find -ls)的日期输出是可变的,具体取决于文件被修改的时间。给定这些文件,你的原始格式如何区分ab的修改时间?

$ ls -l
total 0
-rw-rw-r--+ 1 mjreed  staff  0 Feb  3  2014 a
-rw-rw-r--+ 1 mjreed  staff  0 Feb  3 04:05 b

如所写,上面的代码会为上面的目录产生这个location=.

Found 2 files:
The file './a' was modified on Feb  3  2014.
The file './b' was modified on Feb  3 00:00.

如果您指出实际的最终目标是什么会有所帮助..

【讨论】:

  • 我认为我的系统不允许使用 typeset -a,因为它说“0403-010 指定的标志对此命令无效。” ...对于数组,我可以看到日期和文件名,但正如你所说,它们在循环退出时已经消失了。
  • 好的,您使用的是 pdksh,而不是 AT&T ksh。检查我的编辑。
  • 感谢您的帮助,但是当输入 set -A 等的代码块时,我得到以下信息 - [1] + Done find "$location" -type f -ls |&跨度>
  • 这是在循环结束时预期的。协处理作业终止。
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
相关资源
最近更新 更多