Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

I am not able to use variables in system command in a C program

UNIX for Dummies Questions & Answers


Tags
unix

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-24-2013
dhanda2601 dhanda2601 is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 7 June 2013, 3:28 AM EDT
Posts: 5
Thanks: 6
Thanked 0 Times in 0 Posts
Linux I am not able to use variables in system command in a C program

this method is not working.I am having a problem to use variables in system command. i cannot use the variables in system command.
this how i was did

Code:
system("whereis command");

this method works very fine. but, i want use the commands as variable.
that means i want only pass the variables.
like this

Code:
scanf("%s",variable);
system("whereis variable");

but,

Last edited by Scott; 01-24-2013 at 06:08 AM.. Reason: Code tags
Sponsored Links
    #2  
Old Unix and Linux 01-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 July 2015, 1:02 PM EDT
Location: Aachen, Germany
Posts: 6,394
Thanks: 112
Thanked 1,763 Times in 1,649 Posts
You should allocate a char variable, fill it with the string constant "whereis " (e.g. sprintf) and the contents of your user input variable (e.g. strcpy), and supply that to the system call.
The Following User Says Thank You to RudiC For This Useful Post:
dhanda2601 (01-30-2013)
Sponsored Links
    #3  
Old Unix and Linux 01-24-2013
dhanda2601 dhanda2601 is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 7 June 2013, 3:28 AM EDT
Posts: 5
Thanks: 6
Thanked 0 Times in 0 Posts
i have already allocate the variable for that my code is

Code:
char cmd[20];
printf("enter you string);
scanf("%s",cmd); 
system("whereis cmd");


this cmd is not working for example.
if give the input as "ls" to cmd variable .
but, that variable is not working in the system command this is my problem

thanks in advance

Last edited by Scott; 01-24-2013 at 06:11 AM.. Reason: Code tags
    #4  
Old Unix and Linux 01-24-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 29 July 2015, 1:02 PM EDT
Location: Aachen, Germany
Posts: 6,394
Thanks: 112
Thanked 1,763 Times in 1,649 Posts
Pls use code tags as advised.
You did not read what I wrote. Put everything into a new variable and supply that (I think by reference) to the system call.
The Following User Says Thank You to RudiC For This Useful Post:
dhanda2601 (01-30-2013)
Sponsored Links
    #5  
Old Unix and Linux 01-24-2013
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 25 June 2015, 3:24 AM EDT
Posts: 3,060
Thanks: 34
Thanked 661 Times in 640 Posts
try this.. not tested

Code:
 
 
char str[10];
char cmd[20];
printf("Enter the string : ");
scanf("%s",str);
sprintf(cmd,"whereis %s",str);
system(cmd);

The Following User Says Thank You to itkamaraj For This Useful Post:
dhanda2601 (01-24-2013)
Sponsored Links
    #6  
Old Unix and Linux 01-24-2013
dhanda2601 dhanda2601 is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 7 June 2013, 3:28 AM EDT
Posts: 5
Thanks: 6
Thanked 0 Times in 0 Posts
thanks a lot Mr.Kamaraj great job..
Sponsored Links
    #7  
Old Unix and Linux 01-24-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 29 July 2015, 3:05 PM EDT
Location: Saskatchewan
Posts: 20,772
Thanks: 901
Thanked 3,704 Times in 3,460 Posts
You should not use scanf to read lines, scanf has a list of problems that are too long to get into. (sscanf is safer.)


Code:
char buf[512];

fgets(buf, 512, stdin); // Read a line
if(strchr(buf, '\n')) (*strchr(buf, '\n'))=0; // Get rid of newline

// You now have a string in 'buf'.

In general, if you want to use scanf, you should use fgets to get a whole line, then use sscanf to scan the string rather than the file to avoid buffering problems.
The Following User Says Thank You to Corona688 For This Useful Post:
dhanda2601 (01-24-2013)
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Small Program with variables gingburg UNIX for Dummies Questions & Answers 1 05-19-2009 04:11 PM
Script to open program and send/execute command in program tuathan UNIX for Dummies Questions & Answers 1 11-19-2008 08:59 PM
using system cp command in C program mridula Programming 2 09-20-2005 01:36 AM
Dummie: How do I get variables mid program RichardB UNIX for Dummies Questions & Answers 1 05-06-2002 02:07 PM
Dump program variables rejise UNIX for Advanced & Expert Users 1 03-19-2002 12:37 PM



All times are GMT -4. The time now is 03:59 PM.