String to Character Array using Korn Shell
I was trying to reorganize my unix environment for different work. For example, I do not need oracle environment configuration in a machine where I’ll be using informix database and hence I would need informix related configuration only. For that purpose, I was modifying script to prompt a menu whenever I login with available configuration. I could input my choice as a character and corresponding environment will be loaded.
For example, If my input is “1″, config variables in file 1 will be loaded in shell.
While doing so, I realized, I may need related multiple environment configuration in one shell and hence I should be able to input multiple characters as string and corresponding environment variables will be loaded. My assumption was to read all characters in string and split them in array. Finally loop through the array and load all required variable in shell.
For example, the following shell script splits the string variable strValue using whitespace as delimiter and store them into charArray.
$ export IFS=" ";
$ strValue="1 2 3 4 5";
$ set -A charArray $strValue
$ echo "First Element: ${charArray[0]}"
First Element: 1
My idea was to make IFS make empty string and perform the same thing to split string into character array and hence the code for the same would be
$ export IFS="";
$ strValue="12345";
$ set -A charArray $strValue
But when I tried to iterate through the array as
$ echo "First Element: ${charArray[0]}"
First Element: 12345
I was so wrong! Making IFS empty string does not split the string as character wise into array. But why ? I tried to find an answer but !
Tags: Korn, Programming, Solari, Unix













My Profile in Linkedin

Comments