Bash Help Required

Status
Not open for further replies.

nileshgr

Wise Old Owl
Hi,

I have a C++ program which simply reads and outputs a file. This output i need to be taken into a Bash variable. How do i do that ?

Or there is any other method to get a file output into a bash variable using bash itself instead of the C++ program ?

Its urgent!! :?:
 

QwertyManiac

Commander in Chief
Code:
cat filename.ext |while read line; do echo "${line}"; done
As you see, cat filename.ext reads the file, and stores it in 'line' for each line encountered as the while progresses. You can replace the do method with whatever you wish, the trick is to load the file contents via cat.

This line prints whatever lines are found in file "filename.ext".
 

Sykora

I see right through you.
In general,

Code:
$> variableName=$(command_to_execute_your_program)

That should do it. The only problem is, it won't work for programs in which you're going to enter input. ie, only the stuff printed to stdout using cout will be stored in the variable.
 
OP
nileshgr

nileshgr

Wise Old Owl
@QM,

Thanks.

@Sykora,

you mean to say that if i set a variable name as a command to execute a program, then the program's output will go to the variable ?

Like:

Code:
output=$(program)
 

Sykora

I see right through you.
The variable name should not be the command. You are merely telling bash to assign the output of tehe porogram to the variable using the above syntax.
 
Status
Not open for further replies.
Top Bottom