Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of Strings in KSH 1

Status
Not open for further replies.

LadyDragon

Programmer
Jan 14, 2004
24
US
I am attempting to create an array of strings using KSH but it doesn't appear to work:

Code:
set -A string_list "string one" "string two" "string three"

Does ksh allow storage of strings in arrays, and if so, what am I doing wrong?

Thanks in advance to anyone out there who can help with this.

Juls
 
That's the correct syntax to create the array. How are you trying to use it? What's the error you're having?

It should be something like...
Code:
    $ set -A string_list "string one" "string two" "string three"
    $ print ${string_list[0]}
    string one
    $ print ${string_list[1]}
    string two
    $ print ${string_list[2]}
    string three
    $ print "Elements in array: ${#string_list[*]}
    3
Hope this helps.

 
I'm trying to cycle through an array of error msgs and I don't seem to have it set up correctly to do this. Here is what I have:
Code:
#!/bin/ksh
set -A error_list "Unable To Connect" "invalid username" "Unable to log in" "load terminated with errors"
let i=0
while [ $i -lt 4 ]
do
        let count=$(grep -c ${error_list[$i]} test.txt)
        if [ $count > 0 ] then
             #do something here
        fi
        let i=i+1
done

However, I get the following output from the grep line:
ftwd02ss% errtest
grep: can't open To
grep: can't open Connect
errtest[6]: count=test.txt:2: bad number
Number of matches: 0
grep: can't open username
errtest[6]: count=test.txt:1: bad number
Number of matches: 0
grep: can't open to
grep: can't open log
grep: can't open in
errtest[6]: count=test.txt:2: bad number
Number of matches: 0
grep: can't open terminated
grep: can't open with
grep: can't open errors
errtest[6]: count=test.txt:1: bad number

Any ideas? I'm sure it's something stupid on my part, I'm just not seeing it. The code works when I substitute the actual string in quotes where I'm calling the array, which is what's really messing with me![sadeyes]

Thanks!
Juls
 
[tt]
let count=$(grep -c "${error_list[$i]}" test.txt)
[/tt]

Jean Pierre.
 
THANK YOU! I think I'll go kick myself a few times now...[smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top