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
                do 
                ssh -n $username@$poihostname pwdx $userpid | awk '{print $2}'>> file3
                done < file2

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

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.

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"

and

ssh -n $username@$poihostname pwdx $userpid
1 Like

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

to

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.

1 Like

Hi, I was working on this ...for long time & stuck at some point
Now I m passing 3 variables poihostname userpid and username

poihostname =dellsys.com
while read userpid username
                do 
                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
dell/server/serevr
dell/server/apache
dell/server/weblogic
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
                do 
             currpids_71=`ssh -n $username@$poihostname pwdx $userpid | awk '{print $2}'`
                done < ss

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})"
1 Like

Solved

Assuming you end up with something like the following, after the loop

currpids_71="dell/server/serevr dell/server/apache dell/server/weblogic"

Now, it is time to split it by the space.

$ printf "%s\n" $currpids_71
dell/server/serevr
dell/server/apache
dell/server/weblogic

or time to use each individual result

$ for s in ${currpids_71}; do echo "do something with $s"; done
do something with dell/server/serevr
do something with dell/server/apache
do something with dell/server/weblogic