LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-10-2003, 09:24 PM   #1
Zed
LQ Newbie
 
Registered: May 2003
Location: Florida
Distribution: Redhat 9/Slackware 8.1/SuSE 6.something...
Posts: 5

Rep: Reputation: 0
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

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

Last edited by Zed; 05-10-2003 at 09:26 PM.
 
Old 05-10-2003, 11:34 PM   #2
td3201
Member
 
Registered: Jan 2002
Location: Omaha, NE US
Distribution: Red Hat/CentOS
Posts: 226

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

chomp( $hostname = `uname -r` );
print "$hostname\n";
 
Old 05-11-2003, 04:55 AM   #3
Zed
LQ Newbie
 
Registered: May 2003
Location: Florida
Distribution: Redhat 9/Slackware 8.1/SuSE 6.something...
Posts: 5

Original Poster
Rep: Reputation: 0
sorry about that, i meant C

Ive been a little absent minded lately

Last edited by Zed; 05-11-2003 at 05:00 AM.
 
Old 05-11-2003, 10:01 AM   #4
td3201
Member
 
Registered: Jan 2002
Location: Omaha, NE US
Distribution: Red Hat/CentOS
Posts: 226

Rep: Reputation: 30
Well, you did use a c-style comments....
 
Old 05-11-2003, 01:05 PM   #5
Zed
LQ Newbie
 
Registered: May 2003
Location: Florida
Distribution: Redhat 9/Slackware 8.1/SuSE 6.something...
Posts: 5

Original Poster
Rep: Reputation: 0
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.

Example:
Code:
#include <stdio.h>

#include <string.h>

void sysinfo();



int main()

{
sysinfo();
return(0);

}

void sysinfo()
{
char host = system("uname -n");
printf("blah blah blah %d", host);
}
It returns this:
localhost.localdomain
blah blah blah 0

and not:
blah blah blah localhost.localdomain


any Ideas?

Last edited by Zed; 05-11-2003 at 01:09 PM.
 
Old 05-12-2003, 02:48 PM   #6
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
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:
Code:
#include <stdio.h>
#ifndef MAX_PATH
#define MAX_PATH 256
#endif

void sysinfo(){
	if(system("uname -r >/tmp/MyTempHostFile") == 0){
		FILE *HostFile;
		if(!(HostFile = fopen("/tmp/MyTempHostFile", "r")))
			return;
		else{
			char szHostName[MAX_PATH];
			int nBytesRead = fread(szHostName, MAX_PATH, 1, HostFile);
			// Don't forget the null-termination!
			szHostName[nBytesRead] = '\0';
			fclose(HostFile);
			printf("blah blah blah %s", szHostName);
		}
	}
}
Hope that might help you out so far.

Greetz, Gary
 
Old 05-12-2003, 03:02 PM   #7
cYbORg
LQ Newbie
 
Registered: May 2003
Location: Frankfurt/Germany
Distribution: Normally Caldera 3.2, but currently WinXP only
Posts: 14

Rep: Reputation: 0
Well, another way would be, you use socket functions to return your hostname without that file-stuff. How about that (it's also untested:
Code:
#ifndef MAX_PATH
#define MAX_PATH 256
#endif

void getsysinfo(const char *szHostName);

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

void getsysinfo(const char *szHostName){
	if(!szHostName)
		return;
	else{
		char *szIP = "127.0.0.1";
		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
 
  


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
Pass a variable as an agruement for cut chipmanchu Programming 1 05-25-2005 11:39 AM
Pass object variable to anothor class in JAVA hus Programming 2 05-11-2005 04:04 AM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 05:09 AM
How to pass env variable to KDE? koyi Linux - General 0 09-03-2003 03:29 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:17 AM.

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