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
Click For Summary

Discussion Overview

The discussion revolves around implementing redirection and piping in a C shell script on UNIX. Participants share their experiences and challenges with parsing commands and executing them, particularly focusing on how to handle multiple input and output redirections simultaneously.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes successfully executing a user command but struggles with implementing redirection and piping.
  • Another participant suggests using standard streams and subprocesses, mentioning functions like fork and popen for handling pipes.
  • There are discussions about using system calls such as dup2 for redirection, with participants sharing code snippets and asking for clarification on combining processes.
  • Some participants express uncertainty about the order of operations in their code, particularly regarding how to manage file descriptors for input and output redirection.
  • One participant asks for steps to implement a command that combines input and output redirection, indicating a desire for a clearer understanding of the process.

Areas of Agreement / Disagreement

Participants generally agree on the need to use file descriptors and system calls for redirection and piping, but there is no consensus on the best approach to combine multiple commands or the correct order of operations in the code.

Contextual Notes

Participants mention various functions and system calls, but there are unresolved questions about the specific implementation details and potential errors in the code logic.

Who May Find This Useful

This discussion may be useful for individuals learning about shell scripting in UNIX, particularly those interested in command execution, redirection, and piping in C shell scripts.

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
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
4K
Replies
3
Views
8K
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
12K
Replies
81
Views
8K
Replies
2
Views
4K
  • · Replies 27 ·
Replies
27
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K