UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2004-02-03 03:36 PM

dineshb_2001
Member
From: germany
Registered: 2004-01-30
Posts: 10

Re: can we redirect the output of a command to a variable

Hi,

can we redirect the output of a command to a variable instead of a file.


take the world in ur stride.

Offline

#2 2004-02-03 04:58 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: can we redirect the output of a command to a variable

I think you'd have to be a bit more specific than that.

But basically no. The output is a byte stream transfered in a pipe. If you
currently call a shell command like "ls" with system() and want to use the
output in your program then you have to do it another way. The typical
method would be to fork a child with a redirect of its standard output pipe
into a pipe defined by your program. Then you start the command in that
child with exec. The output is then transfered to your program via this pipe
and you can read it into a buffer (byte array?) for further process.

Offline

#3 2004-02-03 06:28 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: can we redirect the output of a command to a variable

Perhaps he means with a shellscript? Then it's easy to do.

Example:

VAR=`ls`
echo $VAR

Note that the ` isn't the same as a ' (here it's under the same key as ~).

Offline

#4 2004-02-07 04:38 AM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: can we redirect the output of a command to a variable

There is also the standard Unix/POSIX popen()/pclose() interface that lets one read/write (but not both at the same time) from/to a FILE * with stdio.h functions and read the output/write output to a process spawned much liked system() (so it has the same security concerns, etc.).

Offline

#5 2006-07-23 04:13 PM

karthiraj1981
Guest

Re: can we redirect the output of a command to a variable

file:MSC060106.dat
kakakaka
0000004

$ k=`tail -1 MSC060106.dat`
$ echo $k
0000004

Put your command in back quotes.. (the quoate that is available with the "~" (tilde) key.. i mean left of the key 1.

Board footer

Powered by FluxBB