is there any other way to do the same thing with out too many conditions.
That depends on if you need to find out the index or not.
If you need the array index of the element matching the input you have to search through the array, though i would prefer some dynamic solution. If your arrays would become a different size you will have to edit the for-loop to reflect this. But "${#arrayname[@]}" (or ${#arrayname[*]}") evaluates to the number of elements in an array an therefore:
will cycle through all array elements regardless of how many there are.
Note that "*" or "@" as a subscript will always address all array elements, for instance:
You could use this to "grep" through all elements at once if you only need to know if the input is in the array or not.
There is only one subtle difference between the "*" and the "@" subscript: one evaluates to all elements separated by the IFS (internal field separator) and the other to all elements in one string.
You see the difference here. First store a script called "countargs.sh" and flag it executable:
Greetings all,
I've been getting a little frustrated over my scripts as I'm not too experienced with powerful commands such as awk and sed. Hope to find some guidance here.
I need to extract the names of all directories within a specified directory, grab their names and then place each name... (5 Replies)
Hi,
Is there any way to convert a string into an array in KSH? In other words I want to split the string like this:
STRING="one two three four"
into an array of 4 values splitting on white space. The array should be similar to the one that would be created with the following command:
... (3 Replies)
hi all,
need help with putting names in an array, i have a few servers which i look up by doing a 'find . -name "*.pid' and the format of the output is like following :
./servername/myserver.pid
i was wondering how can i iterate through and store each name in one array
my code is... (1 Reply)
I was wondering if ksh supported arrays. I have a script that may work with several hosts. I'd like a means of knowing how many hosts I'm working with and an easy way to access them (as variables) in a loop. I'm assuming there's some kind of foreach in shell scripting. (1 Reply)
Hi all,
Following code in ksh is giving error:
fileLimit=5
func(){
dir="$1"
format="$2"
array="$3"
i=0
ls -lrt $format | tail -${fileLimit} | while read f_det; do
files="${f_det},"
((i+=1))
done
eval $(echo set -A $array '"${files}"')
}
func "." "*.pl" "a"
echo... (10 Replies)
The following command is only intermittently successful, depends on the data I give it:
set -A ImageShifts_sorted `awk '/START_SECTION IMAGE_DEFINITION/ {getline;getline;getline;getline; print $2"-"$3}' temp.ASCII | sort -u`
My Error:
set: -1: unknown option
Finally, If I run that... (3 Replies)
Hi All,
My Requirement is as follows:
1. User will input Source Sytem Code as input.
2. I have source system codes as 11, 34, 56, 99, 45 etc.
OS Version: SunOS 5.8 Generic_117350-62 sun4u sparc SUNW,Sun-Fire-V890
My code is like...
echo 'Source System Code: \c'
read varSSCode... (3 Replies)
Hi all,
I need help with the following scenario in ksh.
If the number of elements contained by arrayA is 11 I need to insert a zero as the element arrayA then print all arrayA elements separated by comma.
Appreciate your help. (9 Replies)
RedHat 5
KSH
I am creating an array, and then using case to go through and count for specific words. Then the count gets stored as an expression.
string='ftp rcp rsh telnet ftp ftp'
set -A myarray $string
FTPCOUNT="0"
for command in ${myarray}
do
case $command in
ftp) FTPCOUNT=`expr... (2 Replies)
I want to extract each and single character from a password string and put it in an array.
I tried this :
set -A password "echo $passwd | awk '{for (i=1; i<=length($1); i++) printf "%s ",substr($1,i,1)}'`
It's working as long that the password string doesn't contains any *
I tried a few... (5 Replies)