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"); |
system("du -ks /home/"user_name[]); maybe?
|
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) |
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. |
now try what dark said :D
|
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. |
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. |