-   Linux - Software (
-   -   Pass text to variable (

Zed 05-10-2003 09:24 PM

Pass text to variable
Im having trouble executing a system command and having it pass the text to a variable, anyone have any idea how to do it?

Whenever i try i get "Invalid Initializer"

help would be appreciated - thanks

Something like
system("uname -r") /*returns the hostname*/
and setting a variable as that instead of having to call system("uname -r") every time.

td3201 05-10-2003 11:34 PM

I am assuming this is perl....which isnt stated anywhere:

chomp( $hostname = `uname -r` );
print "$hostname\n";

Zed 05-11-2003 04:55 AM

sorry about that, i meant C

:rolleyes: Ive been a little absent minded lately

td3201 05-11-2003 10:01 AM

Well, you did use a c-style comments.... :)

Zed 05-11-2003 01:05 PM

Any idea? I mean ive tried something like

char blah = system("uname -n");

but like, its not setting blah as the command, instead it executes "uname -n" as a seperate process, then when you call blah, it equals 0.


#include <stdio.h>

#include <string.h>

void sysinfo();

int main()



void sysinfo()
char host = system("uname -n");
printf("blah blah blah %d", host);

It returns this:
blah blah blah 0

and not:
blah blah blah localhost.localdomain

any Ideas?

cYbORg 05-12-2003 02:48 PM

Well - of course it happens like that, cause system() returns zero on success and thatfore you receive the zero-character '\0' into your host-variable. If (what is definately not the case) uname returned the host, the returnvalue would be c-string (a pointer to the first char of the string) and therefore you'd have needed a char*. Actually, uname prints any output to STDOUT which here is the console.
You can force the output to be written in a textfile and then read it's content into the variable. Here is an yet untested example:

#include <stdio.h>
#ifndef MAX_PATH
#define MAX_PATH 256

void sysinfo(){
        if(system("uname -r >/tmp/MyTempHostFile") == 0){
                FILE *HostFile;
                if(!(HostFile = fopen("/tmp/MyTempHostFile", "r")))
                        char szHostName[MAX_PATH];
                        int nBytesRead = fread(szHostName, MAX_PATH, 1, HostFile);
                        // Don't forget the null-termination!
                        szHostName[nBytesRead] = '\0';
                        printf("blah blah blah %s", szHostName);

Hope that might help you out so far.

Greetz, Gary

cYbORg 05-12-2003 03:02 PM

Well, another way would be, you use socket functions to return your hostname without that file-stuff. How about that (it's also untested:

#ifndef MAX_PATH
#define MAX_PATH 256

void getsysinfo(const char *szHostName);

int main(){
        char host[MAX_PATH];
        printf("blah blah blah %s", host);
        return 0;

void getsysinfo(const char *szHostName){
                char *szIP = "";
                struct HOSTENT *pHostName = gethostbyaddr(szIP, sizeof szIP, AF_INET);
                strcpy(szHostName, pHostName->h_name);

Think, that might be more useful. And: it's portable ;)
Sorry about not including the headers, but I do not exactly know which ones to include. But there are sources...

So far, Gary

All times are GMT -5. The time now is 12:15 PM.