The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to? launch command with string of command line options TinCanFury Shell Programming and Scripting 5 04-28-2008 06:06 PM
find command sort options? mavsman UNIX for Dummies Questions & Answers 6 04-01-2008 03:20 PM
Associated array from command line options jperret Shell Programming and Scripting 1 01-10-2008 05:16 PM
Split Command options mohdtausifsh UNIX for Advanced & Expert Users 7 10-04-2006 07:28 AM
Find command with prune and exec options Sebarry UNIX for Dummies Questions & Answers 2 06-19-2006 04:07 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-26-2002
Safia Safia is offline
Registered User
  
 

Join Date: Jun 2002
Posts: 1
Executing command line options

Can someone please tell me how to modify/add to this code so that it recognizes UNIX command options (all beginning with "-") and executes the command with options?



Code:
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{

     int i;

    system("stty -echo");

    for(i=1; i<argc; i++)
    {
        printf("The command %s is executed.\n", argv[i]);
        system(argv[i]);
     }

    system("stty echo");
    return 0;
}
  #2 (permalink)  
Old 07-01-2002
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
Re: Executing command line options

Hi Safia,

I think you will have to prepare a string of the unix command first and then pass this string to the system function. This string manipulation should take care of the "-" sign. I think you can also try ftok.

Hope this hints you to the solution.
  #3 (permalink)  
Old 07-02-2002
hell666 hell666 is offline
Registered User
  
 

Join Date: Jul 2002
Location: void
Posts: 53
There's a function called getopt(), check to see if this function is supported in your compiler, this will help you.
  #4 (permalink)  
Old 07-03-2002
sharuvman sharuvman is offline
Registered User
  
 

Join Date: Mar 2002
Location: Bangalore,Karnataka State,India
Posts: 18
try this one

Can you try this one ??
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void main(int argc, char **argv)
{
char str[30];
if (argv[2][0] == '-')
{
strcpy(str,argv[1]);
strcat(str," ");
strcat(str,argv[2]);
system (str);
}

}//end of main

or

void main(int argc, char **argv)
{
system(argv[1]);
}

for second case, you need to give input as say "ls -l" within double quotes.
You can have any combination for this.
Like let us assume "a.out" is the binary then

./a.out "who | wc -l" also works for the second option.,....


bye,
sharath
  #5 (permalink)  
Old 07-03-2002
hell666 hell666 is offline
Registered User
  
 

Join Date: Jul 2002
Location: void
Posts: 53
>void main(int argc, char **argv)
void main() is wrong. main() should return a value.
  #6 (permalink)  
Old 07-03-2002
sharuvman sharuvman is offline
Registered User
  
 

Join Date: Mar 2002
Location: Bangalore,Karnataka State,India
Posts: 18
void can be there

If main is not intended to return anything.......we must have its return type "void".

You can compile the source code using ansi compliance,


bye,
sharath
  #7 (permalink)  
Old 07-04-2002
hell666 hell666 is offline
Registered User
  
 

Join Date: Jul 2002
Location: void
Posts: 53
ANSI C says 'int main()'. Ofcourse, to avoid compiler warnings you can use 'void main()', but that is incorrect. The OS might be wanting a return value from the terminated program, and when you are not returning any value to it, a junk value might be returned to your OS, and the results could be unpredictable.

See this
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0