The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
function return array dophine Shell Programming and Scripting 6 05-06-2008 01:16 PM
Perl - New line in array elements bperl Shell Programming and Scripting 4 04-03-2008 10:56 PM
pass each elements in array to ssytem command jaganadh Shell Programming and Scripting 0 12-14-2007 06:57 AM
How can i read array elements dynamically in bash? haisubbu UNIX for Dummies Questions & Answers 1 08-29-2006 03:19 AM
how to return an array of elements from oracle to shell script satyakiran Shell Programming and Scripting 3 08-02-2005 10:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-03-2007
Sudhakar333 Sudhakar333 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 7
To return the elements of array

Hi,
Please can someone help to return the array elements from a function. Currently the problem I face is that tempValue stores the value in myValue[] as a string while I need an array of values to be returned instead of string.

Many Thanks,
Sudhakar

the function called is:

get_config_values_from_mediation_cfg()
{
i=0
tempValue=`grep $1 $CONFIG_FILENAME |
awk '
BEGIN {
FS = "="
count = 0
i = 0
}
NF == 2 && !/^#/ {
gsub(/"/, "") # Remove stroke characters
gsub(" ", "") # Remove spaces
gsub("\n", "") # Remove new line character
checkForCommentSymbol = index ( $2, "#" )
if ( checkForCommentSymbol == 0 )
myValue[i] = $2
else
myValue[i] = substr( $2, 1, checkForCommentSymbol - 1 )

count++
i++
}
END {
if ( count == 0 )
print "NO_VALUE"
else if ( count == 1 )
print myValue
else
#print "INVALID_VALUES"
for (j=0; j<=i; j++)
print myValue[j]
}
'`
echo "${tempValue[@]}"
}
===============================================


echo "Configuration Parser Processing $1"

echo "**********************************************************************"
echo "PROTOCOLS:"
myProtocolTypes=`get_config_values_from_mediation_cfg "PROTOCOL_TYPE"`
echo "returned array: ${myProtocolTypes[@]}"
echo "again: ${myProtocolTypes[0]}"
echo "again: ${myProtocolTypes[1]}"
echo "again: ${myProtocolTypes[2]}"
#for (i=0; i<3; i++)
for i in 0 1 2
do
echo "once more: ${myProtocolTypes[$i]}"
done
awk ' {split ($1, a, " "); print a[1]} ' $myProtocolTypes
#awk ' {split (myProtocolTypes, a,); print a[1]} '

echo "FILENAME_PATTERNS:"
echo `get_config_values_from_mediation_cfg "FILENAME_PATTERN"`
echo "MODES:"
echo `get_config_values_from_mediation_cfg "MODE"`
echo "CONFIG_FILENAMES:"
echo `get_config_values_from_mediation_cfg "CONFIG_FILENAME"`
  #2 (permalink)  
Old 08-03-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Check this :


Code:
get_config_values_from_mediation_cfg()
{
echo | awk '/'"$1"'/
        BEGIN {
                FS = "="
                count = 0
                i = 0
        }
        NF == 2 && !/^#/ {
                gsub(/"/, "")   # Remove stroke characters
                gsub(" ", "") # Remove spaces
                gsub("\n", "") # Remove new line character
                checkForCommentSymbol = index ( $2, "#" )
                if ( checkForCommentSymbol == 0 )
                        myValue[i] = $2
                else
                        myValue[i] = substr( $2, 1, checkForCommentSymbol - 1 )
 
                count++
                i++
        }
        END {
                if ( count == 0 )
                        print "NO_VALUE"
                else if ( count == 1 )
                        print myValue
                else
                        #print "INVALID_VALUES"
                        for (j=0; j<=i; j++)
                        print myValue[j]
        }
' $CONFIG_FILENAME
}
===============================================
 

echo "Configuration Parser Processing $1"

echo "**********************************************************************"
echo "PROTOCOLS:"
i=0
get_config_values_from_mediation_cfg "PROTOCOL_TYPE"|while read myProtocolTypes[${i}]
do
  (( i += 1 ))
done
echo "returned array: ${myProtocolTypes[@]}"
echo "again: ${myProtocolTypes[0]}"
echo "again: ${myProtocolTypes[1]}"
echo "again: ${myProtocolTypes[2]}"

  #3 (permalink)  
Old 08-03-2007
Sudhakar333 Sudhakar333 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 7
To return the elements of array

Many thanks.
  #4 (permalink)  
Old 08-06-2007
Sudhakar333 Sudhakar333 is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 7
the first value is blank

Hi,

Please can you someone help me out to eliminate the problem. Here, it correctly prints the myFilenameCount but returns the first value of array myFilenamePatterns as blank.

Thanks,
Sudhakar

i=0
get_config_values_from_mediation_cfg "FILENAME_PATTERN"|while read myFilenamePatterns[${i}]
do
echo "FilenamePattern: ${myFilenamePatterns[$i]}"
(( i += 1 ))
done
myFilenameCount=${i}
echo $myFilenameCount
echo "returned array: ${myFilenamePatterns[@]}"
i=0
while [ $i -lt $myFilenameCount ]
do
echo "FilenamePatterns: ${myFilenamePatterns[${i}]}"
(( i += 1 ))
done

Last edited by Sudhakar333; 08-06-2007 at 08:01 AM..
  #5 (permalink)  
Old 08-06-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Quote:
Originally Posted by Klashxx View Post
Check this :


Code:
get_config_values_from_mediation_cfg()
{
echo | awk '/'"$1"'/
        BEGIN {
                FS = "="
                count = 0
 
....
Must be:

Code:
get_config_values_from_mediation_cfg()
{
awk '/'"$1"'/
        BEGIN {
                FS = "="
                count = 0

  #6 (permalink)  
Old 08-06-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Please, read the rules:
Quote:
(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.
No duplicate postings:
http://www.unix.com/shell-programmin...rns-blank.html
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0