executing bash commands in C with strings

This is a discussion on executing bash commands in C with strings within the C Programming forums, part of the General Programming Boards category; Hi all!!! I'm new to C and am doing an assignment far beyond my capabilities (oh unfair teachers ) I ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Unhappy executing bash commands in C with strings

    Hi all!!!
    I'm new to C and am doing an assignment far beyond my capabilities (oh unfair teachers ) I have to write my own shell in C. Now I don't have to write my own functions but I could merely use usual bash functions in /bin/. I cannot use system() command.
    Now I figured out forking and exec and few things like that but now I want to pass my arguments to the functions. This is where I face my problem. I don't know how!!!
    You can give optional tags or you can omit them. Do I always have to pass a fixed number of arguments to the command, giving NULL values to the absent options? If so, how many options one command could hold?(i.e. how many arguments must I pass) I've been searching enough but couldn't find answers. Please can somebody help? Some references, links for me please? My shell must execute ls, ps, pwd, cp, echo, sleep commands with all possible options.
    Another thing is I have to cut all options given by the user. I plan to store them in an array. I can break the string from the spaces but I really go wild when trying to store them in an array, especially because I don't know how many options are given before they are given. Another problem is that I understand that string itself is an array of characters. So storing few strings in an array?? Do I have to use a multi-dimensional array? Please help. Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    HarryProudy, welcome to the forum!

    May I request you double space your paragraphs? No white space like that, and eyes do start to glaze over.

    If you'll post the start, the "backbone" of your code, then we can take one issue at a time, and work with it. 20 questions and no code, suggests ignore, rather than helpful response.

    Code or pseudo code will work, but "something" to get that important starting point.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Accessing bash commands using C - Dev Shed
    Give that forum a chance to work.

    So long as you put some effort in, people will help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    On prompt try
    Code:
    man execvp
    We can execute ls -l, as in
    Code:
    #include <unistd.h>
    
    int main (int argc, char** argv)
    {
    	execl("/bin/ls", "ls", "-l", NULL);
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Replies: 2
    Last Post: 03-30-2009, 12:25 AM
  3. executing commands
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-24-2008, 02:59 AM
  4. Executing DOS commands from inside an image
    By Moony in forum C Programming
    Replies: 6
    Last Post: 03-16-2008, 12:40 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21