How to get value from array and set those values as a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get value from array and set those values as a variable
# 1  
Old 09-18-2012
Question How to get value from array and set those values as a variable

I am new to ksh scripting, specially array. How do i get values from an array and set the value as variable and pass those variables to the different functions??

someone taught me how to get input from a file with have columns i need to read, but now i doesnt know how to set those value to be a variable

please helpSmilie

Code:
while read line; do
  read -A array <<<$line
  n=${#array[@]}
  echo "${array[0]} is my function"
  for ((i=1;i<$n;i++)); do
    print "${array[$i]}"  
   done
  
done < file

# 2  
Old 09-18-2012
Try something like this..

Code:
for i in ${array[@])}
do
echo ${array[$i]}
done

# 3  
Old 09-18-2012
I am able to print the results out but i need to like use the values (the output value) and set each data with a variable sth like

a = ${array[1]}

but i know this wont work.
i am not sure how should i read a file and get the data inside, so that when i read this file it set all the vales to the correct place and run the script.

Quote:
Originally Posted by pamu
Try something like this..

Code:
for i in ${array[@])}
do
echo ${array[$i]}
done

# 4  
Old 09-18-2012
Quote:
Originally Posted by gavin_L
I am able to print the results out but i need to like use the values (the output value) and set each data with a variable sth like

a = ${array[1]}

i am not sure how should i read a file and get the data inside, so that when i read this file it set all the vales to the correct place and run the script.
What you want to do..? Please explain in details.please provide input and desired output.
# 5  
Old 09-18-2012
ok i have a file with content looks like this:

Code:
add         data1          data2
delete     data1          data2
replace    data1          data2              data3

i need to read this file so that when it read, my ksh script willl read the 1st column as a function to call, then it will also get the values eg. for the column add it will get the values of that row in this case data1 and data2 and pass those values to the add function as well to do the adding.
# 6  
Old 09-18-2012
You need eval here:
Code:
# cat test.sh 
#!/usr/bin/ksh

add ()
{
echo "Add: param $1"
}

delete ()
{
echo "Delete: param $1"
}

func=add
param=test
eval $func \$param

func=delete
param=test2
eval $func \$param

Code:
# test.sh    
Add: param test
Delete: param test2

This User Gave Thanks to Klashxx For This Post:
# 7  
Old 09-18-2012
try this..
assuming you have already have functions for add,delete ans so on..

input file
Code:
$cat file
add         data1          data2
delete     data1          data2
replace    data1          data2              data3

functions-
Code:
add ()
{
echo "Add: param $1"
}

delete ()
{
echo "Delete: param $1"
}

replace ()
{
echo "replace: param $1"
}

comand
Code:
for i in "$(awk '{for(i=2;i<=NF;i++) { print $1,$i}}' file)"
do
eval "$i"
done

#output-

Add: param data1
Add: param data2
Delete: param data1
Delete: param data2
replace: param data1
replace: param data2
replace: param data3

This User Gave Thanks to pamu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

2. Shell Programming and Scripting

If a string variable value exists in a set of values

Can some one please help me with the syntax in shell script for the below : if $var1 exists in ('val1','val2','val3') I want to execute a set of commands if the value of var1 variable matches any one of the given string values. Please let me know if there are any other option to go by. ... (10 Replies)
Discussion started by: Pandee
10 Replies

3. Shell Programming and Scripting

Attach variable to array values

Hi All, Need a help. I am just learning Shell scripting. Need a requirement like to attach variable to array values. Value of from is AF and to is AD. froml=$(echo $from | tr '' '') tol=$(echo $to | tr '' '') Schemas=(AWQM WFCONTROLLER PROVCO PRISM) for s in "${Schemas}" do ... (3 Replies)
Discussion started by: pvmanikandan
3 Replies

4. Shell Programming and Scripting

storing multiple values in a array variable

Am using a find command in my script .The output may be one or more. I need to store those values in a array and need to access those. Am unable to find the solution . Any help on this will be helpful. if < code> else a=<find command output which gives the file name either 1 or more> if 1... (1 Reply)
Discussion started by: rogerben
1 Replies

5. Shell Programming and Scripting

Store values from a file into an array variable in Shell

Dear All, I have been trying to do a simple task of extracting 2 fields from the file (3 rows) and store it in an array variable. I tried with: #! /bin/bash ch=`cut -f10 tmp.txt` counter=0 for p in $pid do c=${ch} echo "$c ..$counter" counter=$((counter+1))... (2 Replies)
Discussion started by: ezhil01
2 Replies

6. Shell Programming and Scripting

Finding indices in an array nearest to a set of values

I have an two arrays. One array BINDIST consists of fences. I have another array XOFFS. Eg BINDIST = 0 10 20 30 40 50 60 XOFFS = 2 3 4 23 25 28 55 58 I want to find to find the indices of values in XOFFS that are closest to each BINDIST. My idea is to do as follows I create array... (7 Replies)
Discussion started by: kristinu
7 Replies

7. Shell Programming and Scripting

Piping Unix Variable Array values into AWK

#ksh Here is my code: ERRORLIST="43032 12001 12002 12003 12004 34019 49015 49016 49017 49018 49024 49025 49026 58004 72003 12005 12006 12007 12008 12011 12012 16024 16023" for ERROR in ${ERRORLIST} do awk -v l="$lastdate" '/^....-..-../&&$0>l{d=$0}d&&/Error: '"${ERROR}"'/{print... (3 Replies)
Discussion started by: k1ko
3 Replies

8. Shell Programming and Scripting

Assigning a set of values to a variable

I wnat to assign a set of values to a variable and use it in if condition. for example: i=$1 d=1 2 3 4 5 6 if then echo "Fine" else echo "Check" fi i will either of the value in d, i.e. i can be 1 or 2 or any value in d, How this can be done? Thanks in advance (2 Replies)
Discussion started by: sol_nov
2 Replies

9. Shell Programming and Scripting

How to print array values whose name is inside a variable

I have a array as CArray=( a1 a2 ) and a1,a2,a3 are also array as: a1=(1 2 3) a2=(3 4 5) now I have this in my code: for i in `echo "${CArray}"` do echo ${$i} done It is giving error as :"bad substitution" It should give me value as 1 2 3 3 4 5 how can I get this...Can u please... (2 Replies)
Discussion started by: joshilalit2004
2 Replies

10. Shell Programming and Scripting

split variable values into array

i have these values inside variable $blah BUNGA TERATAI 3 5055 ITH 1 0 0 0 1 1 JADE TRADER 143W ITH 4 0 0 0 4 4 MOL SPLENDOR 0307A ITH 3 0 0 0 3 3 so how do I split them into array with the... (4 Replies)
Discussion started by: finalight
4 Replies
Login or Register to Ask a Question