Passing argument to command in C


 
Thread Tools Search this Thread
Top Forums Programming Passing argument to command in C
# 1  
Old 09-25-2011
Passing argument to command in C

Hello all,

New to C and I'm trying to write a program which can run a unix command. Would like to have the option of giving the user the ability to enter arguments e.g for "ls" be able to run "ls -l".

I would appreciate any help.

Thanks
Code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

main()
{
   int menuchoice;
int execv(const char* path, char* const argv[]);

   do
 {
   
      printf("  Select an action to perform:\n");
      printf("  1. This will execute the sleep command\n");
      printf("  2. This will execute the ps command\n");
      printf("  0. This will exit the script\n");
    
                
     scanf("%d", &menuchoice);
      switch (menuchoice)
        { 
         case 1:
               if (fork())
                  wait(0);
                else
        char* args[] = {"sleep","30","60","90", (char* ) NULL};
          printf("Please enter any of the following sleep time\n");
            printf("30,60,90\n");
        execv("sleep", args);
                       break;
          case 2:
               if (fork())
                  wait(0);
               else
                  system("ps -ef", "ps -ef", (char *)NULL);
                  break;
          case 0:
        printf("Goodbye\n");
return 0;
               break;
          default:
               printf("Please enter only 0-6\n");

   }
  }

   while (menuchoice != 0);
}

# 2  
Old 09-25-2011
You need to take advantage of the complete definition of the main function declarion:
Code:
main() // this wont work for your application

int main(int argc, char *argv[]); // this is what you need


the argc is the argument count, this always has at least one argument passed to it "The program name", so you will need to check if the user entered an argument with something like
Code:
if (argc > 1)
// handle this

I will leave it up to you to research argv

---------- Post updated at 11:50 AM ---------- Previous update was at 11:43 AM ----------

ah misunderstood your question

you need something like this:


put you need to parse your array of strings
Code:
pid_t  pid;
     int    status;

     if ((pid = fork()) < 0) {     /* fork a child process           */
          printf("*** ERROR: Child failedd\n");
          exit(1);
     }
     else if (pid == 0) {          /* for the child process:         */
          if (execvp(*argv, argv) < 0) {     /* execute the command  */
               printf("*** ERROR: exec failed\n");
               exit(1);
          }
     }
     else {                                  /* for the parent:      */
          while (wait(&status) != pid)       /* wait for completion  */
               ;
     }


Last edited by Scott; 09-25-2011 at 01:08 PM.. Reason: Please use code tags
# 3  
Old 09-25-2011
Many Thanks @ Zacharoni.

Not sure i follow you though, perhaps i wasnt clear with my problem statement.

If you look at case 2 of my code, i have the "ps -ef" command hardcoded into my program, i do not want to do that - i want the user to have the ability to use the "ps" command with any argument [a, e, f , l, x] if selected from the menu.
# 4  
Old 09-26-2011
Assemble the command into a character array. Put in different strings depending on what the user wants.

Code:
char str[64];
strcpy(str, "ps ");
strcat(str, " -ef");
system(str);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating file and passing argument to a command

Hi All, I am having command to run which will take argument as input file. Right now we are creating the input file by cat and executing the command ftptransfer -i input file cat >input file file1 file2 cntrl +d Is there a way I can do that in a single command like ... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

2. Shell Programming and Scripting

Passing an argument using alias to piped command

Hi. I'm trying to do a "simple" thing. grep -rls grepped_exp path | xgs where xgs is an alias to something like: xargs gvim -o -c ":g/grepped_exp" now the problem is that I want to pass the "grepped_exp" to the piped alias. I was able to do something like what I want without the... (4 Replies)
Discussion started by: hagaysp
4 Replies

3. Shell Programming and Scripting

Passing an argument to cut command

Can we pass an argument to cut command as below Suppose cut command is used in for or while loop and we need to pass the incremental counter cut -f$i Here $i is an argument. Like wise it has to come cut -f1 cut -f2 Where i=1,2,3,.... (1 Reply)
Discussion started by: bashamsc
1 Replies

4. Shell Programming and Scripting

Passing argument on find command

Hi, I'm trying to pass the variable in the find command like below a=log.20111114 find /apps/file3_logs/env3/ -name '$a' -exec ls -lrt {} \; but it's not working thanks in advance. Regards Thelak (3 Replies)
Discussion started by: ajithbe
3 Replies

5. UNIX for Dummies Questions & Answers

Passing command output as an argument to a shell script

Hi, I have a very small requirement where i need to pass command output as an argument while invoking the shell script.. I need to call like this sh testscript.sh ' ls -t Appl*and*abc* | head -n 1' This will list one file name as ana argument.. I will be using "$1" in the shell... (2 Replies)
Discussion started by: pssandeep
2 Replies

6. UNIX for Dummies Questions & Answers

Passing command line argument between shell's

Hi, I am facing a problem to pass command line arguments that looks like <script name> aa bb "cc" dd "ee" I want to pass all 5 elements include the " (brackets). when I print the @ARGV the " disappear. I hope I explain myself Regards, Ziv (4 Replies)
Discussion started by: zivsegal
4 Replies

7. Shell Programming and Scripting

Need Help with the argument passing Through Command line

$$$$$ (5 Replies)
Discussion started by: asirohi
5 Replies

8. Shell Programming and Scripting

passing argument from Cshelll to awk command

Hi all I have got a file digits.data containing the following data 1 3 4 2 4 9 7 3 1 7 3 10 I am writing a script that will pass an argument from C-shell to nawk command. But it seems the values in the nawk comman does not get set. the program does not print no values out. Here is the... (1 Reply)
Discussion started by: ganiel24
1 Replies

9. Shell Programming and Scripting

passing a command line argument

I have a shell script which does the encryption of a file where i am passing the file name as a command line argument,but later on the script waits on the screen to enter Y or N what is the command i should be using on the shell script #!/bin/bash -x outfilename=file.out echo... (8 Replies)
Discussion started by: rudoraj
8 Replies

10. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies
Login or Register to Ask a Question