Script FOR IN loop does not match webGui sort order


taychive

Recommended Posts

Hello,

 

I trying to write a bash script that will generate a report about one of my unraid shares. Inside is a FOR IN loop that points to the shared folder. By default the FOR IN loop searches the directory in ASCII order for example: ( ) 012 ABC [ ] abc { } 

 

However, in the WebGui if i browse the disk or shared folder and select ascending by name i get: 012 ( ) [ ] AaBbCc { }

 

I do not know what this type of sorting order is called and couldn't find anything that matched when searching online so this is my first part of my question. What is it called?

The second part is how can I write my FOR IN loop to use this order? 

Link to comment

Agh it is natural sort, you are right. I read up on it before and thought i understood it, but i was wrong. I thought natural sorting only applies to numbers so a 10 would come after 9 and so on.

 

With that now known, if i add the V and f options to the sort command i can get: 012 AaBbCc ( ) [ ] { }

Its closer but still not exactly like the webgui

 

Any idea how to get parentheses and square brackets to come between the numbers and letters? Here is a snippet i was playing around with.

#!/bin/bash

Folder="Test"

oldIFS="$IFS"
IFS=$'\n'
for Subfolder in `ls -d /mnt/user/"${Folder}"/*/ | sort -t/ -Vf`; do

	SubfolderSize=$(du -sb "${Subfolder}" | cut -f1)
	TotalSize=$((TotalSize+SubfolderSize))
	DirCount=$((DirCount+1))
	echo -n "${Subfolder}" is folder number "${DirCount}" and is" "
	echo "${SubfolderSize}" | numfmt --to="iec"

done
IFS="$oldIFS"

echo -n "${Folder}" has "${DirCount}" folders and is" "
echo "${TotalSize}" | numfmt --to="iec"

 

Edited by taychive
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.