shell - 阵列在 AIX KSH 中不起作用
问题描述
我有代码:
typeset -i idx=0
for each in `find . -name "*.log" -print `
do
ILIST[idx]=`basename $each`
idx=idx+1
print :${ILIST[$idx]}:$each:$idx << does not print array element "::file1.log:o"
done # for each
print ${ILIST[@]} << prints entire array as expected
exit
那么,如何打印数组的单个元素?它适用于 HPUX。目标是并排创建 2 个数组,其中一个是文件名,另一个是创建时间,以便可以检查该文件的时间。
解决方案
首先,我无法访问 AIX 机器来测试语法是否良好,但它在 Linux 实现上运行良好。
由于 KSH 支持复合变量 (93l +),我认为您最好使用它们而不是 2 个数组。
这是一段代码,可以做你想做的(至少我认为:))
#!/bin/ksh
typeset -L50 Col1
typeset -L25 Col2
#need to give the path as a parameter here since it's a test script
DIRNAME=$1
idx=0
#The caveat for "find" still apply, so I'm assuming there is no special characters nor space in the filenames
for file in $(find $DIRNAME -type f -name "*.log")
do
#Full path name
LOGFILE[$idx].FName=$file
#Only File name
LOGFILE[$idx].BName=$(basename $file)
#Last modification time since the beginning of time in second as it is easier to manage afterward. There is no creation date per se on Linux at least
LOGFILE[$idx].MDate=$(stat -c %Z $file)
((idx++))
done
Col1="Filename"
Col2="Date"
print "$Col1$Col2\n"
#Some prefer the syntax ${#LOGFILE[@]} but won't make a difference here since it's an indexed array.
for ((i=0;i<${#LOGFILE[*]};i++)); do
Col1=${LOGFILE[$i].BName}
#Translating the seconds to human date. Format can of course be modified at will
Col2=$(date -d @"${LOGFILE[$i].MDate}" +"%Y%m%d%H%M")
print "$Col1$Col2"
done
希望能帮助到你。
推荐阅读
- php - Laravel 迁移数据库/和供应商/中的重复类名
- css - Angular Material - 单选按钮标签字体大小太大
- regex - 将字符串与正则表达式 scala 匹配
- javascript - 如何获得单位圆上2个角度之间的顺时针角度
- post - POST 使用 HttpClient 来自内容类型 application/x-www-form-urlencoded 使用 grant_type 和 scope
- apache-kafka - Kafka Connect Cassandra 源未获取 timeuuid 字段的数据
- php - 使用 Laravel 5.7 电子邮件验证从代理网络中的服务器发送验证电子邮件
- permissions - 如果 Windows 10 拒绝权限,我如何取消从文件夹中读取只读选项?
- hyperledger - 共识算法在超级账本区块链网络中的作用是什么?它是如何工作的?
- c# - 循环遍历数据表中特定列的值