LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-07-2003, 03:16 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
How do I pass a C variable to a Bash command ?


How do I pass a C variable to a Bash command ?

The program code below

*** system("du -ks /home/$user_name");

user_name is a C variable. I would like to have this variable
use in a bash command.
The program will run, but the above code will give
the hard drive space for /home/ not for /home/$user_name.

========================================

#include <stdio.h>

int main(void)
{
FILE *in;
char buffer[255];
char user_name[32];
int n;
int i = 0;
int j = 1;

if((in = fopen("/etc/samba/smbpasswd","r")) == NULL)
{
perror("Couldn't open file");
return 1;
}

do
{
/* fgets doesn't change the buffer if it doesn't read */
/* anything in. This means that the string from the */
/* last read is still there and the program will print */
/* the last line twice; therefore use the line below. */
buffer[0] = '\0';

fgets(buffer, 255, in);
n = strlen(buffer) - 1;
if (n != 0)
{
if(buffer[n] == '\n')
buffer[n] = '\0';
printf("buffer = %s\n", buffer);

for (i = 0; i <= 254; i++)
if (buffer[i] != ':')
user_name [i] = buffer[i];
else
{
user_name [i] = '\0';
printf ("j = %d\n", j);
printf ("user_name[] = %s\n", user_name);
printf ("\n");
j++;

*** system("du -ks /home/$user_name");

break;
}
}
else printf("nothing read from file\n");
}
while(!feof(in));

fclose(in);
return 0;
}

system("du -ks /home/$user_name");
 
Old 07-07-2003, 03:23 PM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
system("du -ks /home/"user_name[]); maybe?

Last edited by Proud; 07-07-2003 at 03:24 PM.
 
Old 07-07-2003, 03:29 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Take a look at sprintf() (man sprintf).

What you will need to do is create a string variable to hold the command, and a string to hold the user's name. Something like this:
Code:
int main(void)
{
  char system_command[100];
  char user_name[20];

  // Get the user name
  scanf("Enter a user name: %s", user_name);

  // Now, put 'du -ks /home/' followed by the user's name
  // in the system_command variable.
  sprintf(system_command, "du -ks /home/%s", user_name);

  // Execute the command
  system(system_command);
}
EDIT: The scanf above should be split; it should be a printf followed by a scanf, but that's probably not that important. You already had the code to read the user's name anyway...

Last edited by Dark_Helmet; 07-07-2003 at 03:47 PM.
 
Old 07-07-2003, 03:31 PM   #4
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
Thumbs up Thank you all for helping

Dark_Helmet you got it. Thank you.


Proud

Space may be the final frontier, but it's made in a Hollywood basement.

Very True.



Last edited by Linh; 07-07-2003 at 03:46 PM.
 
Old 07-07-2003, 03:39 PM   #5
DrOzz
Senior Member
 
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185

Rep: Reputation: 59
now try what dark said
 
Old 07-07-2003, 03:40 PM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Linh:

See dark_helmet's post for the correct method, with using sprintf(and if you're interested in hearing me rant about the negative aspects of using scanf, please ask!!). Also, please surround code posts with &#91;code&#93; &#91;/code&#93; tags, for the sake of readability.
 
Old 07-07-2003, 04:12 PM   #7
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Linh, sorry about the bad idea.

That line's from the title track of Californication by the Red Hot Chilli Peppers fyi.
 
  


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 12:39 PM
How to pass mysql query to a variable? chynna_v Programming 4 09-03-2004 06:09 AM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 10:22 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 02:45 AM
Pass text to variable Zed Linux - Software 6 05-12-2003 04:02 PM


All times are GMT -5. The time now is 04:04 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration