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

  • Thread starter pouchito
  • Start date
  • Tags
    Shell Unix
In summary, Tim was able to write the codes but it will only execute one command. He needs to combine the commands into one process.
  • #1
pouchito
20
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
  • #2
How are you parsing the commandline?
 
  • #3
Well, i m tokenized using the strtok ==> replacing all the ' ' with zeros
and it worked but i m stuck in z other part
 
  • #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
 
  • #5
thank you
i will buy the book and check it
 
  • #7
i checked the Advanced Programming in the Unix Environment".
but didn't help me that much :(
 
  • #8
Go look up the man pages for pipe(), dup2(), close(), fork(), and exec().
 
  • #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 ?
 
  • #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
 

1. What is shell scripting in C (unix)?

Shell scripting in C (unix) is the process of writing shell scripts, which are programs written in the C programming language that are executed by the Unix shell. These scripts can be used to automate tasks, manipulate files and directories, and perform other system operations.

2. What are the benefits of using shell scripting in C (unix)?

Using shell scripting in C (unix) allows for more complex and efficient scripting compared to using the Unix shell alone. It also provides access to C language features such as variables, loops, and functions, making it easier to write reusable and maintainable scripts.

3. How do I write a shell script in C (unix)?

To write a shell script in C (unix), you will need a text editor and a C compiler installed on your system. First, create a new file with a .c extension and write your script using C syntax. Then, use the C compiler to compile the script into an executable file, which can then be executed by the Unix shell.

4. Can I use shell scripting in C (unix) for system administration tasks?

Yes, shell scripting in C (unix) is commonly used for system administration tasks, such as performing backups, managing user accounts, and monitoring system resources. Its ability to access low-level system functions makes it a powerful tool for managing and automating system tasks.

5. Are there any limitations to using shell scripting in C (unix)?

One limitation of using shell scripting in C (unix) is that it may not be as portable as other scripting languages, as it relies on specific Unix system functions. Additionally, it may have a steeper learning curve for those not familiar with the C language. However, these limitations can be overcome with proper knowledge and practice.

Similar threads

  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Replies
6
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
5
Views
2K
Replies
3
Views
8K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
27
Views
6K
  • Programming and Computer Science
Replies
3
Views
305
Back
Top