首页 > 解决方案 > 阵列在 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 个数组,其中一个是文件名,另一个是创建时间,以便可以检查该文件的时间。

标签: shellunixshkshaix

解决方案


首先,我无法访问 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

希望能帮助到你。


推荐阅读