Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Shell scripting in C (unix)

  1. Nov 16, 2006 #1
    Hello Dudes,

    I m writing a C shell code in UNIX that takes a user command and executes it, my shell must support the redirection and piping too.

    Well i succeeded in writing the first one (takes a user command and executes it ) but i m stucked in the other parts :(

    Can anyone help ?

    Thanks in advance
     
  2. jcsd
  3. Nov 16, 2006 #2

    verty

    User Avatar
    Homework Helper

    How are you parsing the commandline?
     
  4. Nov 17, 2006 #3
    Well, i m tokenized using the strtok ==> replacing all the ' ' with zeros
    and it worked but i m stuck in z other part
     
  5. Nov 17, 2006 #4
    It shouldn't be too crazy. Std streams are basically file descriptors to an app. You start sub-processes with fork. You start subprocesses with a pipe via popen. Don't know how to redirect in C, I'm sure there is some absurdly simple system call.

    Maybe try a simple thing first, like "date | less". You start date (probably fork, eh?), start less (popen), and then redirect (???) date's stdout to the pipe to less's stdin.

    THE book on this is "Advanced Programming in the Unix Environment".

    Good luck,
    Tim
     
  6. Nov 17, 2006 #5
    thank you
    i will buy the book and check it
     
  7. Nov 17, 2006 #6

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

  8. Nov 17, 2006 #7
    i checked the Advanced Programming in the Unix Environment".
    but didn't help me that much :(
     
  9. Nov 19, 2006 #8
    Go look up the man pages for pipe(), dup2(), close(), fork(), and exec().
     
  10. Nov 22, 2006 #9
    Well I was able to write the codes
    but it will execute one single command
    ex:
    <
    or
    >
    or
    |


    but it cannot execute 2 at the same time
    for ex if i input
    sort>sorted<unsorted
    it cannot b executed
    i ve to combine them
    is there a quick way ?
     
  11. Nov 22, 2006 #10

    robphy

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    Can you post your code?
    You may need to parse things at numerous levels.
    You may need some fancy escaping or parentheses.
     
  12. Nov 22, 2006 #11

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

  13. Nov 23, 2006 #12
    my target is to do the following

    sort < source > destination

    I know how to do sort < source:

    fd = open(source, O_RDWR | O_CREAT, mode);
    if(dup2(fd, 0) < 0)
    close(fd);


    I know how to do sort > destination:

    fd = open(source, O_RDWR | O_CREAT, mode);
    if(dup2(fd, 1) < 0)
    close(fd);


    I read the man
    it s daying dup2(old file, new file)


    How to cobine the 2 processes together , it is not working with me :'(
     
  14. Nov 23, 2006 #13

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Why combine them into one "process"? Why not just do both processes?
     
  15. Nov 24, 2006 #14
    ok, how can i do it
    i need to take the resutls of the first one (sort < source) which logically gonna be on the screen and send it to a file name called (> destination)
    How can this b done?
     
  16. Nov 24, 2006 #15

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You know how to do one. You know how to do the other. Why can't you do one and do the other? I don't see what problem you're having.
     
  17. Nov 24, 2006 #16
    you mean :

    fd = open(source, O_RDWR | O_CREAT, mode);
    if(dup2(fd, 0) < 0)
    fd1 = open(source, O_RDWR | O_CREAT, mode);
    if(dup2(fd1, 1) < 0)
    close(fd);
    close (fd1);

    ???
     
  18. Nov 24, 2006 #17

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    That's roughly what I was thinking. I don't see why this idea wouldn't do exactly what you want.

    Now, you've gotten your statements out of order. The way you wrote it, you

    (1) open fd
    (2) call dup2 with fd
    (3) if (2) failed, then open fd1
    (4) call dup2 with fd1
    (5) if (4) failed, then close fd
    (6) close fd1

    which is presumably not what you intended!
     
  19. Nov 24, 2006 #18
    TRUE

    i need to solve the following
    command < input > output

    can you tell me the steps to implement it?
     
  20. Nov 24, 2006 #19

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    You have the right idea. Just get your code in the right order. You want to, I presume,

    (1) open fd
    (2) call dup2 with fd
    (3) if (2) failed, close fd
    (4) open fd1
    (5) call dup2 with fd1
    (6) if (5) failed, then close fd1

    there is some freedom in rearranging these statements. But the most important thing is that you have the right thing as the "then" part of your if statements.
     
  21. Nov 24, 2006 #20
    Thanks Again :D
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Shell scripting in C (unix)
  1. Unix Shell Script (Replies: 3)

  2. UNIX scp script (Replies: 3)

  3. Unix / scripting help (Replies: 3)

Loading...