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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-07-2003, 02:16 PM
|
#1
|
Member
Registered: Apr 2003
Posts: 178
Rep:
|
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");
|
|
|
07-07-2003, 02:23 PM
|
#2
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
system("du -ks /home/"user_name[]); maybe?
Last edited by Proud; 07-07-2003 at 02:24 PM.
|
|
|
07-07-2003, 02:29 PM
|
#3
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
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 02:47 PM.
|
|
|
07-07-2003, 02:31 PM
|
#4
|
Member
Registered: Apr 2003
Posts: 178
Original Poster
Rep:
|
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 02:46 PM.
|
|
|
07-07-2003, 02:39 PM
|
#5
|
Senior Member
Registered: May 2003
Location: Sydney, Nova Scotia, Canada
Distribution: slackware
Posts: 4,185
Rep:
|
now try what dark said 
|
|
|
07-07-2003, 02:40 PM
|
#6
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
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.
|
|
|
07-07-2003, 03:12 PM
|
#7
|
Senior Member
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794
Rep: 
|
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 04:29 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|