I recently encountered some strange behaviour in my bash shell while writing a script.
My script would take the contents of a folder and store them into an array. Sounds simple enough, as it should be. But it's not.
If you type verbatim:
" declare -a ARRAY=`ls -1 -x $directory` "
You will find that every space in every filename will produce its own entry in the array.
A file called "An Introduction To Bash.txt" would be seen in the array as
which is an abomination for what I need these the filenames for. It does make sense why this behaviour appears, but if we want the entire filename per entry, we need to take the filename before it's put into the $ARRAY variable and do something with it to ensure spaces either do not exist or do not denote the start of a new array token.
The former being the easiest and resourced method, I converted all spaces to underscores (temporarily) to prove my point.
The command is now " declare -a ARRAY=`ls -1 -x $directory | sed 's/ /_/g'` " and it will output filename entries properly, albeit with underscores, no spaces.
To ensure this effect is permanent and not just for show, one would need to rename (using the 'mv' command) all the files in that directory so no spaces are in the filenames.
Bash isn't one of my stronger languages, so I may have missed a blaringly obvious detail.
Also of note: I'm still working on the freeworld project, albeit slowly after gaining employment. My 1st revision document is approximately 70-80 pages, but that's not including proper use case tables and UML/flowchart diagrams. Least of all the prototypes for necessary components.
I hope to make another entry soon enough, in the coming couple weeks.
Powered by Blogilo