How to sort Integer Array in ksh | Unix Shell Scri

2019-03-03 01:56发布

How to sort integer array in KornShell. Found this link, KornShell Sort Array of Integers but it seems to be not working and throwing error.

Code:

NUM_ARR[1]=-1
NUM_ARR[2]=-2
NUM_ARR[3]=-3
NUM_ARR[4]=-4
NUM_ARR[5]=-5
NUM_ARR[6]=-6
NUM_ARR[7]=-7
for file in /home/fimsctl/datafiles/outbound/timelog/timelog_file_*.csv ; do

    SORTED_NUM_ARR=`($(printf "%s\n" ${NUM_ARR[@]} | sort -n))`
 echo ${SORTED_NUM_ARR[*]}
 done

Output:

testb.ksh[118]: -7:  not found

1条回答
放我归山
2楼-- · 2019-03-03 02:24

You can use sort with process substitution:

sort -n <(printf "%s\n" "${NUM_ARR[@]}")
查看更多
登录 后发表回答