3

I wrote a program that simulate the bash command in Linux, in C. It works perfectly with inputs from the keyboard, meaning:

  • application > file: redirect stdout of app to file(write output to file)
  • application < file: redirect stdin of app from file (read input from file)
  • application >> file: redirect stdout of app to file (append output to file)
  • app1 | app2: redirect stdout of app1 to stdin of app2
  • app &: means that app should be executed in the background

All these work, when I enter the command from the keyboard, as mentioned above.

In order to complete my assignment, I need to add one more element, which is redirection from a file. Meaning, if my program is called bashSimulator, then if I do this:

bashSimulator < fileWithCommands

then my program needs to get all the commands from the fileWithCommands and execute them.

I have no idea how to do the redirection from a file.

10
  • You probably need to be able to run app < inputfile from your bashSimulator and not bashSimulator < inputfile? Commented May 25, 2012 at 13:38
  • @UlrichDangel: No , I need to take all the commands from inputfile and execute them with bashSimulator . I really do need to do : bashSimulator < inputfile . I need to read all the commands in inputfile , each command is separated from the other with \n ... I thought about using the argv and argc of main but that didn't work . Commented May 25, 2012 at 13:42
  • 7
    you don't need to do anything special as the inputfile is provided as stdin. If you are already reading your commands from stdin then everything should already work Commented May 25, 2012 at 13:47
  • 1
    I don't understand what you are missing. Emulating a shell and doing those redirects is much more complicated than reading strings from stdin, which you are aleadry doing if what you state above is true. bashSimulator doesn't do the redirection in the command you state; the shell that runs it does. Commented May 25, 2012 at 13:56
  • 3
    No, i won't do your homework, but as i said (and as you already mentioned in your question) app < inputfile means redirect stdin of app from file (read input from file). This also means bashSimulator will receive the commands via stdin. In other words: If bashsimulator will always read the commands from STDIN everything should be fine Commented May 25, 2012 at 13:58

1 Answer 1

5

You read the commands from stdin instead of from a batch file or interactively from the user. Use isatty(3) to figure out if this is the case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.