Passing variable as input & storing output in other variable


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Passing variable as input & storing output in other variable
# 1  
Old 09-06-2015
Passing variable as input & storing output in other variable

I have a below syntax its working fine...
Code:
var12=$(ps -ef | grep apache | awk '{print $2,$4}')

Im getting expected output as below:
Code:
printf "%b\n" "${VAR12}"
dell 123
dell 456
dell 457

Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results to be stored in a variable called as logdata
Code:
while read userpid username
do 
logdata=$(ssh -n $username@dell.sys.com pwdx $userpid | awk '{print $2}')
done < $var12

now my aganda is to store the output in a variables instead of storing output in a file. I have created a code but I m getting unexpected results & since last 2hrs I got stuck up with this

Last edited by Don Cragun; 09-06-2015 at 11:47 AM.. Reason: Change PHP tags to CODE tags.
# 2  
Old 09-06-2015
Quote:
Originally Posted by sam@sam
I have a below syntax its working fine...
Code:
var12=$(ps -ef | grep apache | awk '{print $2,$4}')

Im getting expected output as below:
Code:
printf "%b\n" "${VAR12}"
dell 123
dell 456
dell 457

Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results to be stored in a variable called as log data
Code:
while read userpid username
do 
logdata=$(ssh -n $username@dell.sys.com pwdx $userpid | awk '{print $2}')
done < $var12

now my aganda is to store the output in a variables instead of storing output in a file. I have created a code but I m getting unexpected results & since last 2hrs I got stuck up with this
You do realize that the redirection in:
Code:
while ...
do 
...
done < $var12

only works if $var12 does not contain any IFS characters and only works if $var12 expands to the name of a file, right???

If var12 contains 3 lines of data and you want to read data from those lines using a while loop, that would be something more like:
Code:
printf '%s\n' "$var12" | while read userpid username
do 
	logdata=$(ssh -n $username@dell.sys.com pwdx $userpid | awk '{print $2}')
done

but this doesn't make any sense either. Why set up a loop invoking ssh to set a variable that is not used in the loop. So only the value of logdata set by the last execution of the loop will make any difference after the loop terminates. And, since the loop is now in a pipeline which may be run in a subshell environment, even the last value may disappear when the loop ends (depending on what shell you're using).

And, of course, you haven't said anything about what operating system you're using, what shell you're using, nor what diagnostics were printed when you tried running your script... So, we're left making lots of guesses based on minimal input.

Why is it so important to avoid using a file if you are using a script that needs to read input from a file???
# 3  
Old 09-06-2015
Hello Don,
THanks for your response,
I m using solaris 5.0 (bash script)
Yes, you are right..the loop is in a pipeline which is running in a subshell environment, at the end it is storing only the output of last iteration in variable logdata..........

But suppose If I store output in a file then I m getting accurate results.
Code:
ssh -n $username@dell.sys.com pwdx $userpid | awk '{print $2}' >> file

can you suggest your comments on this..
# 4  
Old 09-06-2015
Hold on - you run a pwdx on a PID that is from another system??
# 5  
Old 09-06-2015
Hello
NO, We run a pwdx command from remote machine to local server.
I.e By default we connect to remote machine then we use ssh hostname connect to respective servers..
# 6  
Old 09-06-2015
Quote:
Originally Posted by sam@sam
Hello Don,
THanks for your response,
I m using solaris 5.0 (bash script)
Yes, you are right..the loop is in a pipeline which is running in a subshell environment, at the end it is storing only the output of last iteration in variable logdata..........

But suppose If I store output in a file then I m getting accurate results.
Code:
ssh -n $username@dell.sys.com pwdx $userpid | awk '{print $2}' >> file

can you suggest your comments on this..
Not really...

You're using an operating system that is about 25 years old. And, on any Solaris 5.x system, you shouldn't be using /usr/bin/awk or /bin/awk (I've forgotten which it was in Solaris 5.0) unless you're using it to run code that you copied from a UNIX System V or earlier AT&T UNIX system.

You have shown us two code segments. And, you've told us you're concerned about how variables are being used. But, whether you set a variable named logdata or you save the output in a file named file, you haven't shown us what output is stored there, you haven't told us what output you want to store there, and you haven't shown us any code that uses the data once it is saved (so we don't know whether a variable, a file, or a pipeline reading it directly would make more sense).

The two code segments you showed us:
Code:
var12=$(ps -ef | grep apache | awk '{print $2,$4}')

and
Code:
while read userpid username
do 
logdata=$(ssh -n $username@dell.sys.com pwdx $userpid | awk '{print $2}')
done < $var12

could easily be combined without using any variables or files (except to store the final output) and getting rid of one invocation of awk with:
Code:
ps -ef | grep apache | while read junk userpid junk username junk
do 	ssh -n $username@dell.sys.com pwdx $userpid | nawk '{print $2}'
done > file

I never used the bash that came with Solaris 5.0, but the grep could also be removed from the pipeline using bash built-ins. I could easily do it with the ksh on Solaris 5.0, but I don't know what parameter expansions were available in bash at that time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

2. Shell Programming and Scripting

Storing output into a variable

My script below seems to be choking because I need the the output of the find command to be stored as a variable that can then be called by used lower in the script. #!/bin/bash cd "/resumes_to_be_completed" var1=find . -mmin -1 -type f \( -name "*.doc" -o -name "*.docx" \)... (1 Reply)
Discussion started by: binary-ninja
1 Replies

3. Programming

Storing input + some text into a variable (C Programming)

Hey guys got a slight problem here, I kindda new to socket programming in C so I need some guide on how to store something like this in a variable. printf ("%s Name : %s\n", id,getNAME(name)); name is declared as name. The getName is a function. So what I'm... (7 Replies)
Discussion started by: aLHaNz
7 Replies

4. Shell Programming and Scripting

Pattern matching & storing result in variable

Hi! i'm trying to parse textfiles against a pattern and storing the result in a variable. The strings i want to get are embraced by and can occur several times in one line, so e.g. some text anything else endwhat i have so far: #!/bin/bash for f in $* do exec 3<&0 exec 0<$f ... (2 Replies)
Discussion started by: thoni
2 Replies

5. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

6. UNIX for Dummies Questions & Answers

Storing lines of output into a script variable

I'm sure this is a simple thing but I can't figure it out. In a script that I'm writing, I'd like to be able to store each line of output from "ls -l" into a variable. Ultimately I'd like to end up with something like: for a in `ls -l` do something with $a doneBut that's reading each... (2 Replies)
Discussion started by: ewoods
2 Replies

7. UNIX Desktop Questions & Answers

problem while storing the output of awk to variable

Hi, i have some files in one directory(say some sample dir) whose names will be like the following. some_file1.txt some_file2.txt. i need to get the last modified file size based on file name pattern like some_ here i am able to get the value of the last modified file size using the... (5 Replies)
Discussion started by: eswarreddya
5 Replies

8. Shell Programming and Scripting

Using 'defaults read' and storing the output in a variable

Hi all, I'm creating a script which uses 'defaults read' to retrieve details from an Info.plist like this; defaults read "/Path/Contents/Info" CFBundleShortVersionString This works fine in Terminal and returns the expected values. Is it possible to use this command in a script, and... (0 Replies)
Discussion started by: davewg
0 Replies

9. UNIX for Dummies Questions & Answers

Storing the output into a variable

Hi unix gurus, I am trying to store the result of a command into a variable. But it is not getting stored. x='hello' y=echo $x | wc -c but it is giving the output as 0(zero) Pls help me its very urgent (7 Replies)
Discussion started by: ravi raj kumar
7 Replies

10. Shell Programming and Scripting

storing output of awk in variable

HI I am trying to store the output of this awk command awk -F, {(if NR==2) print $1} test.sr in a variable when I am trying v= awk -F, {(if NR==2) print $1} test.sr $v = awk -F, {(if NR==2) print $1} test.sr but its not working out . Any suggestions Thanks Arif (3 Replies)
Discussion started by: mab_arif16
3 Replies
Login or Register to Ask a Question