LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-07-2011, 12:17 AM   #1
BeyondSora
LQ Newbie
 
Registered: Feb 2011
Location: Waterloo, Ontario, Canada
Distribution: Ubuntu 10.10, Arch
Posts: 11

Rep: Reputation: 0
How to assign a variable to the output value of a program


so i wrote myself a very simple hellworld program in c++

...the usual stuff
int main()
{
cout << "helloworld" << endl;
return 0;
}

------------------
how do i assign output value of this program to a variable in shell?

Code:
myvar=$(./a.out); echo $myvar
prints "0" as myvar got assigned to the return value
Code:
./a.out|myvar=$?; echo $myvar
doesnt work either
 
Old 02-07-2011, 12:43 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Your first attempt should work:
Code:
myvar=$(./a.out); echo $myvar
When you run ./a.out on its own, do you get the expected "helloworld"?

EDIT:
Also, what shell are you running? bash? ksh? tcsh?

Last edited by Dark_Helmet; 02-07-2011 at 12:46 AM.
 
Old 02-07-2011, 01:13 AM   #3
BeyondSora
LQ Newbie
 
Registered: Feb 2011
Location: Waterloo, Ontario, Canada
Distribution: Ubuntu 10.10, Arch
Posts: 11

Original Poster
Rep: Reputation: 0
yes I do get "helloworld"

I am using bash

---------------------
this is so confusing, i made myself the same program using python
and it works......
but just not for c++... this doesnt make sense to me D:
 
Old 02-07-2011, 01:46 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Well, if it works with your python program, then I'd say you've eliminated bash as a suspect. So it's likely something funky with the c++. For comparison, here's a quickie hello world source, compile, and run:

helloworld.cpp
Code:
#include <iostream>

int main( int argc, char *argv[] )
{
  std::cout << "Hello world!" << std::endl;

  return 0;
}
Plus the compilation and command substitution tests:
Code:
$ g++ -o a.out helloworld.cpp
$ ./a.out
Hello world!
$ myvar=$(./a.out); echo "$myvar - test"
Hello world! - test
$ bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to assign a string to a structure variable in C? daYz Programming 10 07-06-2022 12:07 PM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 11:19 AM
inserting program output into a variable MrFixit Linux - General 3 03-13-2008 06:48 PM
assign converted string to variable bmaheni Linux - Newbie 2 12-28-2007 09:41 AM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 07:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration