Find the answer to your Linux question:
Results 1 to 2 of 2
Hi Everyone, I have a ksh script that queries a database. The query output looks like this: $queryResult = [name1 age1 name2 age2 name3 age3] echo ${queryResult[0]} = name1 echo ...
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    1

    ksh scripting- array formats

    Hi Everyone,

    I have a ksh script that queries a database. The query output looks like this:

    $queryResult = [name1 age1 name2 age2 name3 age3]

    echo ${queryResult[0]} = name1
    echo ${queryResult[1]} = age1
    echo ${queryResult[2]} = name2
    echo ${queryResult[3]} = age2

    ...

    I need to insert those values into a new array that would look like this:

    $newArray = [name1age1 name2age2 name3age3]

    echo ${newArray[0]} = name1age1
    echo ${newArray[1]} = name2age2
    echo ${newArray[2]} = name3age3

    Basically, I am trying to concatenate each person's name and age into one postion within $newArray. If anyone can provide me with a solution to this, especially using a function, I would be most appreciative. Thanks.

  2. #2
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    The "$" does not belong on the left side of the assignment unless you are attempting to show the "input prompt".
    Code:
    queryResult = [name1 age1 name2 age2 name3 age3]
    
    echo ${queryResult[0]}
    echo ${queryResult[1]}
    echo ${queryResult[2]}
    echo ${queryResult[3]}
    echo ${queryResult[4]}
    echo ${queryResult[5]}
    Code:
    newArray = [ ${queryResult[0]}${queryResult[1]} ${queryResult[2]}${queryResult[3]} ${queryResult[4]}${queryResult[5]} ]
    or
    Code:
    newarray = [ "${queryResult[0]}${queryResult[1]}" "${queryResult[2]}${queryResult[3]}" "${queryResult[4]}${queryResult[5]}" ]
    Code:
    echo ${newarray[0]}
    echo ${newarray[1]}
    echo ${newarray[2]}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...