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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
19 replies · 5K views
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
 
Physics news on Phys.org
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 ?
 
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 :'(
 
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?
 
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);

?
 
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!
 
TRUE

i need to solve the following
command < input > output

can you tell me the steps to implement it?
 
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.