Problem assigning variables to arrays


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem assigning variables to arrays
# 1  
Old 04-05-2009
Problem assigning variables to arrays

Hi All,
I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that.
At present my scrot looks like:

Code:
 
co=0
pco=0
co=`cat /tmp/highcpu | wc -l`
pco=`expr $co + 1`
i=1
while [ $i -lt $pco ]
do
var1=`cat /tmp/highcpu | awk -F" " '{print $1}' | head -$i | tail -1`
cat /tmp/prsta | grep -i $var1 >>  /tmp/commonpid
cat /tmp/commonpid | grep -i $var1 |sort -t/ -k2n >> /tmp/commonpid_sort
i=`expr $i + 1`
done

At present i am redirecting the output of this line:
Code:
 
cat /tmp/commonpid | grep -i $var1 |sort -t/ -k2n >> /tmp/commonpid_sort

to a file as seen above.
Instead i want to assign those values to an array so that it can be used later.

Any idea how to do that.
Thanks
# 2  
Old 04-05-2009
post the o/p of this command
Code:
cat /tmp/commonpid | grep -i $var1 |sort -t/ -k2n

# 3  
Old 04-05-2009
The out put of
Quote:
cat /tmp/commonpid | grep -i $var1 |sort -t/ -k2n
is:

Quote:
566 java/372
566 java/369
566 java/365
566 java/364
566 java/376
566 java/374
566 java/4
566 java/345
566 java/335
566 java/330
566 java/352
566 java/359
566 java/328
566 java/351
566 java/311
7850 java/71
7850 java/77
7850 java/4
7850 java/68
7850 java/66
7850 java/34
7850 java/49
7850 java/36
7850 java/31
7850 java/67
7850 java/11
7850 java/56
7850 java/27
7850 java/51
7850 java/70
7850 java/17
7850 java/29
7850 java/72
# 4  
Old 04-05-2009
if you wanna use it one by one then directly pass it to the for loop or while loop
Code:
for i in `cat /tmp/commonpid | grep -i $var1 |sort -t/ -k2n` ; do
....
done
while read value ; do
...
done < `cat /tmp/commonpid | grep -i $var1 |sort -t/ -k2n`

# 5  
Old 04-06-2009
Hey thanks vidyadhar for your reply.
I am a newbie to scripting and i want to work with arrays but i am not so comfortable.
Could you please explain how to assign variables or output of some command(like ls or contents of a file after doing cat) to arrays and how those arrays work under for loop.?
Thanks for your patience.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

2. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

3. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

4. Shell Programming and Scripting

Help with reading and assigning variables

Hi Gurus, I have a file named log with 2 lines Each line is a file name. eg $ cat log monday tuesday I need to read log and assign each output(filename) to a different variable. The following doesn't work:- while read A B do echo " a is ${A} " echo " b is ${B} " done <... (6 Replies)
Discussion started by: wisdom
6 Replies

5. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

6. UNIX for Dummies Questions & Answers

Assigning String to Arrays

Hello everybody, this is my first post here, I think IŽll be here for a long time :D I wanted to know how I can assign each single character from a string to an array... For example, "unix" I have to set, array=u array=n array=i array=x However, that string may change in each... (5 Replies)
Discussion started by: Gartlar
5 Replies

7. UNIX for Dummies Questions & Answers

Assigning variables using awk

Hi, I am having a line which is separated by - I need to extract each field and put into some variable. a=`echo "this-is-the-case" | awk -F- '{print $1}' ` b=`echo "this-is-the-case" | awk -F- '{print $2}' ` c=`echo "this-is-the-case" | awk -F- '{print $3}' ` d=`echo "this-is-the-case" | awk... (2 Replies)
Discussion started by: posix
2 Replies

8. UNIX for Advanced & Expert Users

assigning variables to their defaults

Hi, Is there any way to assign defaults values to the shell variables without reassigning them ( restarting the session) for example after login the value of ORACLE_HOME=/a/b/c i have changed this value from the console export ORACLE_HOME=/c/d now what if i want the value exported to... (1 Reply)
Discussion started by: clx
1 Replies

9. Shell Programming and Scripting

Problem in assigning values to variables

Hi, I have some problem in assigning values to variables: This is what Iam literally doing: i=0 input=test temp$i = $input In the sense, I try to assign the value in the variable input (ie., test) to another variable temp0 (since i is assigned 0, temp$i is temp0). Seems simple, but I get... (3 Replies)
Discussion started by: mohanprabu
3 Replies

10. UNIX for Dummies Questions & Answers

assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have Gotopg = "go to page 3" then page = 3 (9 Replies)
Discussion started by: k@ssidy
9 Replies
Login or Register to Ask a Question