How to sort Integer Array in ksh | Unix Shell Scripting

懵懂的女人 提交于 2019-12-02 09:49:48

问题


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:


You can use sort with process substitution:

sort -n <(printf "%s\n" "${NUM_ARR[@]}")


来源:https://stackoverflow.com/questions/27900170/how-to-sort-integer-array-in-ksh-unix-shell-scripting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!