I have the below code, list of string that I'd like to sort.
LIST="APP_PATH_10_TARGET APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET"
My goal is to sort it into:
"APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET APP_PATH_10_TARGET"
So I did this:
SORTEDLIST=$(echo ${LIST} | sort -t"_" -k3n,3)
But its still showing:
SORTEDLIST=APP_PATH_10_TARGET APP_PATH_1_TARGET APP_PATH_2_TARGET APP_PATH_3_TARGET
I can't find out why the sort doesn't work.
================================================================
Update: This is the code that I'm working on.
I have this ENV variables:
APP_PATH_1_TARGET="/prd/example/1"
APP_PATH_2_TARGET="/prd/example/2"
APP_PATH_3_TARGET="/prd/example/3"
APP_PATH_10_TARGET="/prd/example/4"
The code that doesn't work, because the list is not in expected sequence:
create_app_dir(){
# Get all variables with name starts with APP_PATH*
local PARAMLIST=`echo ${!APP_PATH*}`
echo "PARAMLIST=${PARAMLIST}"
local SORTEDLIST=$(sort -t_ -k3n <<< ${PARAMLIST// /$'\n'}|tr -s "\n" " ")
echo "SORTEDLIST=${SORTEDLIST}"
# Iterate the list and create dir if doesn't exist
for p in ${SORTEDLIST}; do
if [[ "${p}" = *_TARGET ]] && [ ! -d "${p}" ]; then
echo "[+] Creating application directory:${!p}"
./make_dir.sh "${!p}"
if [ $? -ne 0 ]; then
echo "[-] Error: Unable to create dir." >&2
return 1
fi
fi
done
}