How to Implement Redirection and Piping in a C Shell Script on UNIX?

  • Thread starter Thread starter pouchito
  • Start date Start date
  • Tags Tags
    Shell Unix
AI Thread Summary
The discussion revolves around creating a C shell code in UNIX that can execute user commands while supporting redirection and piping. The initial success in executing single commands has led to challenges in handling multiple commands with redirection, such as combining input and output redirection in a single execution. Participants suggest using system calls like fork, pipe, and dup2 for managing standard streams. They emphasize the importance of correctly ordering the code statements to ensure proper execution of commands. Resources like man pages and online documentation are recommended for further guidance. The conversation highlights the need for careful parsing of commands and the potential complexity involved in combining processes effectively.
pouchito
Messages
20
Reaction score
0
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
 
Technology news on Phys.org
How are you parsing the commandline?
 
Well, i m tokenized using the strtok ==> replacing all the ' ' with zeros
and it worked but i m stuck in z other part
 
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
 
thank you
i will buy the book and check it
 
i checked the Advanced Programming in the Unix Environment".
but didn't help me that much :(
 
Go look up the man pages for pipe(), dup2(), close(), fork(), and exec().
 
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 ?
 
  • #10
Can you post your code?
You may need to parse things at numerous levels.
You may need some fancy escaping or parentheses.
 
  • #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 :'(
 
  • #13
Why combine them into one "process"? Why not just do both processes?
 
  • #14
ok, how can i do it
i need to take the resutls of the first one (sort < source) which logically going to be on the screen and send it to a file name called (> destination)
How can this b done?
 
  • #15
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.
 
  • #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);

?
 
  • #17
pouchito said:
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);

?

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!
 
  • #18
TRUE

i need to solve the following
command < input > output

can you tell me the steps to implement it?
 
  • #19
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.
 
  • #20
Thanks Again :D
 
Back
Top