Store output in variables instead of files

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Store output in variables instead of files
# 1  
Old 09-05-2015
Store output in variables instead of files

Hi All,
I have written a (bash) function which generates multiple files say file1 file2 file3
now I want to reduce the generation of these three files i.e. store the output of three files in variables, and generate the same results, in-order to avoid multiple creation of files
how is that possible can any one suggest a code please...

functionA () 
{ poihostname=$1
ssh -n $poihostname /usr/ucb/ps -awwx | egrep "java|sys1" | awk '{print $1,$5}' > file1
        cat file1 | awk  -F/ '{print $1,$3}'   > file2
        while read userpid username
                ssh -n $username@$poihostname pwdx $userpid | awk '{print $2}'>> file3
                done < file2

while read path
egrep  "^${path}$" file3
if [ $? -ne 0 ]
echo "DEV path $path is missing " >> finalresult
done < masterfile }

# 2  
Old 09-05-2015
Without seeing/knowing every single detail of your files 1 - 3, it seems to me that you could do without any of them by creatively piping the results.
If you post some more details, we might be able to put together a reasonable solution.
# 3  
Old 09-05-2015
Hi sam@sam,

Is it possible for you to post a few lines from the output of these commands without the rest of the pipes?
ssh -n $poihostname /usr/ucb/ps -awwx | egrep "java|sys1"

ssh -n $username@$poihostname pwdx $userpid

This User Gave Thanks to Aia For This Post:
# 4  
Old 09-05-2015
These two lines can be reduced from:
ssh -n $poihostname /usr/ucb/ps -awwx | egrep "java|sys1" | awk '{print $1,$5}' > file1
        cat file1 | awk  -F/ '{print $1,$3}'   > file2

ssh -n $poihostname /usr/ucb/ps -awwx | egrep "java|sys1" | \
      awk '{print $1,$5}' | awk  -F/ '{print $1,$3}'   > file2

You can reduce the use of the egrep and two awks to a single invocation of awk also. Show use the output of
ssh -n $poihostname /usr/ucb/ps -awwx

and somebody will provide a suitable awk command.
This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 09-05-2015
Hi, I was working on this ...for long time & stuck at some point
Now I m passing 3 variables poihostname userpid and username
while read userpid username
                ssh -n $username@$poihostname pwdx $userpid | awk '{print $2}'>> currpids
                done < ss

ss is filename and output is generated in a file called currpids.
more currpids
now my requirement is I want to store the output in a variable instead of storing output in a file(currpids)........I have created a code but I m getting output for only third value(dell 457) & im not getting the output for 1st & 2nd values from file ss , let me know If your require any more info
more ss
dell 123
dell 456
dell 457
I m passing 3 parameters in file ss, but not getting accurate results
my new code look like this
while read userpid username
             currpids_71=`ssh -n $username@$poihostname pwdx $userpid | awk '{print $2}'`
                done < ss

Last edited by sam@sam; 09-05-2015 at 08:52 PM..
# 6  
Old 09-06-2015
Hi sam@sam,

You are asking why currpids_71 contains only the third value(dell 457). This occurs because currpids_71 gets overwritten each time through the loop.

Since you are using bash you can use an array to store the outputs.
currpids_71+=( $(ssh -n $username@$poihostname pwdx $userpid | awk '{print $2}) )

Now, to access each element on that array you do it by index.
echo ${currpids_71[0]}

or all of them at once
echo ${cuppids_71[@]}

Alternatively, you can build a long string, appending to the same string variable the output of ssh, each time through the loop
currpids_71="$currpids_71 $(ssh -n $username@$poihostname pwdx $userpid | awk '{print $2})"

Last edited by Aia; 09-06-2015 at 09:18 PM.. Reason: Grammar
This User Gave Thanks to Aia For This Post:
# 7  
Old 09-07-2015

Last edited by sam@sam; 09-07-2015 at 01:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Read in Multiple log files and output selected variables and values to cvs file

I have several problems with my problems: I hope you can help me. 1) the If else statement I am getting an error message. My syntax must be incorrect because the entire statement is throwing an error. For example in filew.log if these items don't exist Memsize, SASFoundation and also if... (0 Replies)
Discussion started by: dellanicholson
0 Replies

2. Shell Programming and Scripting

Can -v option in awk be used to store an array of variables?

I want to pass an array of variables to be inserted by awk in the 2nd column of a file. Empl No. Employee Age 1000000 22 1100000 24 1200000 26 Now, I want to pass an array having three different ages which need to replace the... (7 Replies)
Discussion started by: Nishi_Licious
7 Replies

3. Shell Programming and Scripting

saving all input name and store them as variables

Hi I want to write a script such that when executed, it will store all input as different variable, for eg ./ name1 name2 name3 name4 will result in $1=name1 $2=name2 $3=name3 etc How do I do that? Thanks. (1 Reply)
Discussion started by: piynik
1 Replies

4. Shell Programming and Scripting

need to store query output fields in variables edit them and update the same in tables.

Hi , I have a query like select err_qty,drop_qty,unbld_qty,orig_qty from usage_data; I need to store the values of these fetched fields in variables, Need to edit them and update the new values into the table. Can anyone please help me in writing this piece of code:( (1 Reply)
Discussion started by: Rajesh Putnala
1 Replies

5. UNIX for Dummies Questions & Answers

Writing a loop to manipulate a script and store it in multiple output files

I have a script where the the 9th line looks like this: $filename=sprintf("250.1chr%d.ped", $N); I want to modify this script 1000 times, changing 250.1chr%d.ped to 250.2chr%d.ped, 250.3chr%.ped.......and so on all the way to 250.1000chr%d.ped and store each output in files called ... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

to read two files, search for patterns and store the output in third file

hello i have two files temp.txt and temp_unique.text the second file consists the unique fields from the temp.txt file the strings stored are in the following form 4,4 17,12 15,65 4,4 14,41 15,65 65,89 1254,1298i'm able to run the following script to get the total count of a... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

7. Shell Programming and Scripting

store multiple variables in one go

Guys anyone know how i can store fields into multiple variables in one go? I'm wanting to grab the disk id's from format into disk1 and disk2 Below is what i want to work but i know it doesnt :- : | format | awk '/^(\ +)/ {print $2}' | read disk1 disk2 The below does work...but i don't... (5 Replies)
Discussion started by: lavascript
5 Replies

8. UNIX for Dummies Questions & Answers

trying to store variables in an array

im looping through an array setting three variables each time (one of the variables gives me the position in the array and is incremented each loop) im trying to then set the variables to that position in the array without much luck. any ideas? anArray=`$VAR1+$VAR2+"("$pos")"` (1 Reply)
Discussion started by: magnia
1 Replies

9. UNIX for Dummies Questions & Answers

trouble using read to store values in variables from command output

I know there are caveats about using read in pipelines because read is treated by a subshell. I know this but I can't think of any way to accomplish this regardless, I'm still a rookie. I hope somebody will be able to interpret what it is that I'm trying to accomplish and correct me. ... (2 Replies)
Discussion started by: ProGrammar
2 Replies

10. Shell Programming and Scripting

Extract numbers from a string and store in variables

Hi All, Is it possible in Unix shell script to extract numbers from a string containing ".", such as; 5.2.314 And store in variables so; var1 = 5 var2 = 2 var3 = 314 Thanks in advance for any help anyone can provide dave (6 Replies)
Discussion started by: davewg
6 Replies
Login or Register to Ask a Question

Featured Tech Videos