Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 06-16-2010, 11:16 PM   #1
LQ Newbie
Registered: Apr 2010
Posts: 10

Rep: Reputation: 0
Post command substitution in linux


I am writing a C++ code to get hard disk serial number of the computer.
This can be achieved by the following command in linux as root user

hdparm -I /dev/hda | grep -i Serial

To use this command in C++ i used

system("hdparm -I /dev/hda | grep -i Serial");

after compiling and running the code the output is printed on the console as

Serial Number: S00JJ20X897934

which is correct,

What i want to do is to store this value in a variable and then use that
variable further in my cpp code.Which i am unable to do...

I could substitute that value in a variable using command substitution of linux, as

system("hdd_id=`hdparm -I /dev/hda | grep -i Serial`");

but then had no access to that variable hdd_id in cpp code..
If i do the above without using system and echo $hdd_id i get the required, but cannot use it in cpp code.

Please suggest how to achieve this.

Thanks in advance
Old 06-17-2010, 12:25 AM   #2
LQ Newbie
Registered: Oct 2009
Location: Venezuela
Distribution: Slackware
Posts: 7

Rep: Reputation: 2
Easy. Redirect the output to a transient file like this:
system("hdparm -I /dev/hda | grep -i Serial > /tmp/t123");
Then read the file, and don't forget to erase it.
Old 06-17-2010, 12:28 AM   #3
Registered: Feb 2005
Location: Maryland, USA
Distribution: Arch x86_64
Posts: 69

Rep: Reputation: 18
I stink at C++, but I'll give this a shot at answering it. Otherwise, is probably the best place to get a clever answer.

The quick and dirty way would be to simply pipe the command's output to a text file, then use cpp to open and read the output. For instance:
system("hdparm -I /dev/hda | grep -i Serial > output.txt");
fp = fopen("output.txt", r");
if(fp != NULL){
/* Do stuff */
Otherwise, try using IPC:
Old 06-17-2010, 12:31 AM   #4
Registered: Feb 2005
Location: Maryland, USA
Distribution: Arch x86_64
Posts: 69

Rep: Reputation: 18
Perromuerto beat me to it. The IPC stuff is still relevant though.


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
Problems with command substitution and whitespaces integrale16 Linux - Newbie 11 10-15-2009 09:53 PM
[SOLVED] echoing whitespace from a command substitution GahseyFan Linux - General 2 05-16-2009 04:58 AM
Bash Command Substitution dakensta Programming 5 11-30-2006 03:10 PM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
command substitution: ^ rhxk Linux - General 2 04-06-2006 09:51 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:31 PM.

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