LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I pass a C variable to a Bash command ? (https://www.linuxquestions.org/questions/programming-9/how-do-i-pass-a-c-variable-to-a-bash-command-70756/)

Linh 07-07-2003 02:16 PM

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");

Proud 07-07-2003 02:23 PM

system("du -ks /home/"user_name[]); maybe?

Dark_Helmet 07-07-2003 02:29 PM

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...

Linh 07-07-2003 02:31 PM

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.



DrOzz 07-07-2003 02:39 PM

now try what dark said :D

TheLinuxDuck 07-07-2003 02:40 PM

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 [code] [/code] tags, for the sake of readability.

Proud 07-07-2003 03:12 PM

Linh, sorry about the bad idea.

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


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