Implementing a shell


 
Thread Tools Search this Thread
Top Forums Programming Implementing a shell
# 1  
Old 11-13-2005
Implementing a shell

I'm implementing a shell in C that supports piping, output redirection, and background processing, and a few other commands. I was wondering how I'd go about implementing the output redirection. So, I'd open a file and I'd fork and execute the command. But how would I get stdout into the file? Any help is greatly appreciated.
# 2  
Old 11-13-2005
Close stdin and then open some other file.
close()
open()
Both have man pages.
# 3  
Old 11-13-2005
i too have a question on the topic.
how do i read from keyboard if stdin is actually stdout of another program. an example is less/more: keyboard input still works when they're piped.
# 4  
Old 11-13-2005
Quote:
Originally Posted by fdarkangel
i too have a question on the topic.
how do i read from keyboard if stdin is actually stdout of another program. an example is less/more: keyboard input still works when they're piped.
This is more of a separate question. You probably should have started a new thread.

If you type "tty", you will see the name of your controlling terminal. If it is, say, /dev/pty/j2, then you could simply open that file to gain access to the keyboard. But it is a lot of work to get the name of your particular terminal, so there is a second way. In the unix environment, a process with a controlling terminal can open the special file /dev/tty and get their own controlling terminal. This won't work if there is no crontrolling terminal such as with a cron job. Even though there is only one file on the system called /dev/tty, each user that opens it will get his or her own keyboard.

However, this may not be a great move to make. Suppose that I have a complicated pipeline of 4 processes. If they all do this at the same time it will be pretty hopeless to get it all straight. And look at all the threads we get where folks need to script the su command and must "expect" to do it. Users tend to dislike programs that do this.
# 5  
Old 11-13-2005
Closing stdin gives me an infinite loop.
# 6  
Old 11-13-2005
Is there a way to maybe modify the file descriptor table so that I replace stdout with the file I've just opened? How would I access the file descriptor table though?
# 7  
Old 11-13-2005
Quote:
Originally Posted by Perderabo
This is more of a separate question. You probably should have started a new thread.
well, i've came across with that problem when i attemped to write a shell. i thought, programs' output should be filtered/processed by shell interpreter first, and then sent to shell screen-output program (in case they're seperate programs) and still be able to catch keyboard input to send programs ctrl+c etc. i've given less & more as examples since they're more common to Unix folks...

however, thanks for the reply.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Implementing linked list in shell scripting

Hello Experts, Is it possible to implement linked list in shell scripting? is yes then how can we do it? Any working example is highly appreciated. Thanks in advance. (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. Shell Programming and Scripting

Implementing Listagg like function in shell

Hi, Basically what I am trying to do is making multiple fields of the same type comma-separated. i.e. for a data like this: B00000 abc B00001 abc,def B00001 ghi B00001 jkl B00002 abc B00002 def B00003 xyz Output should be like: B00000 abc B00001 abc,def,ghi,jkl... (20 Replies)
Discussion started by: prohank
20 Replies

3. Shell Programming and Scripting

Need help implementing a timout in my Shell Script for RHEL6

Hey Guys, My problem: I have a script that will be querying the database every minute to see if it gets a response, the response its querying for is "UP" in a table i made called dbup in the database. Now, I am trying to add the component to implement a timeout if the script does not get a... (2 Replies)
Discussion started by: mo_VERTICASQL
2 Replies

4. Shell Programming and Scripting

Need help in implementing logic

i have following input file... 00290002STDR000000000000000000000000000EOD END TRANSACTION ^@^@^@^@^@^@^@^@^@^@^@^@^ 00299998STDR070000000007000000000000000STANDING DEBITS ^@^@^@^@^@^@^@^@^@^@^@^@^... (1 Reply)
Discussion started by: sagarrd
1 Replies

5. Shell Programming and Scripting

Implementing Queue Using Shell scripts

HI I want to implement a control mechanism using Shell scripts .The intention is to have controlled number of jobs running in parallel External process will kickstart 40 jobs in parallel .All the 40 jobs will call the same generic script with different parameter values .But at a... (4 Replies)
Discussion started by: police
4 Replies

6. Homework & Coursework Questions

implementing mkdir, chdir, mv, pwd inside a shell !

1. The problem statement, all variables and given/known data: need to implement mkdir, chdir, mv, pwd given a shell.cpp directory.cpp and some other files this shell missing these commands, and i need to implement them inside the shell 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: evantheking
0 Replies

7. IP Networking

implementing ftp

i have a client server connection steady and running... but the problem here is that the file transfer is very crude and succeptible to risks... so i want to implement ftp.. can anybody suggest a way to implement it or any book to read? (4 Replies)
Discussion started by: damn_bkb
4 Replies

8. Programming

Implementing the redirection

Hi all I am facing a problem with redirection. Its somewhat related to parsing. I am following the following steps. 1. take the command and tokenize it. 2. if redirection is there then give it to redirection unit 3. if pipe is there give it to piping unit. 4. do until the command ends ... (0 Replies)
Discussion started by: mobile01
0 Replies

9. Programming

Implementing a shell in C

Hi, I am implementing a shell in C, with the following problem... Suppose the shell is invoked from the command line as >> myshell < test.in > test.out 2>&1 I have to execute the commands in test.in and redirect them to test.out How does one detect in the main function that the shell... (1 Reply)
Discussion started by: jacques83
1 Replies

10. Programming

need help in implementing simple interactive shell in C

hello all, i hv attached herewith my program to implement a simple interactive shell in C. no matter hw hard I try, I keep getting some errors. i need help - urgently !! proj1test7.c: In function `parseCommand': proj1test7.c:102: warning: assignment makes pointer from integer without a cast... (2 Replies)
Discussion started by: nix1209
2 Replies
Login or Register to Ask a Question