Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6944099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:14:31+00:00 2026-05-27T13:14:31+00:00

i coded a basic shell in C for executing basic commands it’ll execute commands

  • 0

i coded a basic shell in C for executing basic commands it’ll execute commands ls, ls -al , ls -al | more etc.

i want to execute following command in my shell.
like ;

ls -al > a.txt

this will give me a.txt file which contain output of ls -al process.
i found a one solution, it’s changing the command in my shell like [command1] | tee [filename]. in this instance it’ll change ls -al > a.txt to ls -al | tee a.txt. but this process gives the output to file and terminal as well. how to stop printing output in terminal.

or are there are any better solution rather than using tee command.
thanks in advance…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:14:32+00:00Added an answer on May 27, 2026 at 1:14 pm

    This is the result of my testing things out with dup2

    The more subtle point is remembering fflush at the right times 🙂 Otherwise, you’ll get very surprising results.

    Also, prefer fileno instead of hardcoding 1 (stdout) 2 (stderr).

    Redirecting stdin was left as an exercise for the reader

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc, const char *argv[])
    {
        int out = open("cout.log", O_RDWR|O_CREAT|O_APPEND, 0600);
        if (-1 == out) { perror("opening cout.log"); return 255; }
    
        int err = open("cerr.log", O_RDWR|O_CREAT|O_APPEND, 0600);
        if (-1 == err) { perror("opening cerr.log"); return 255; }
    
        int save_out = dup(fileno(stdout));
        int save_err = dup(fileno(stderr));
    
        if (-1 == dup2(out, fileno(stdout))) { perror("cannot redirect stdout"); return 255; }
        if (-1 == dup2(err, fileno(stderr))) { perror("cannot redirect stderr"); return 255; }
    
        puts("doing an ls or something now");
    
        fflush(stdout); close(out);
        fflush(stderr); close(err);
    
        dup2(save_out, fileno(stdout));
        dup2(save_err, fileno(stderr));
    
        close(save_out);
        close(save_err);
    
        puts("back to normal output");
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basic pseudo code looks like this: void myFunction() { int size = 10;
The following code is visual basic, .NET, ASP... all of the above? Can anybody
I created a button. My basic requirements are rounded thicker border, with more than
I have changed title slightly because I thought this is more appropriate question. Would
I have coded a basic game in JavaScript and am working on a high
I'm working on a basic shell in C. In my implementation of pipes ,
I'm creating a shell script to a basic program. One option is to delete
Is there a jquery shell/parser emulation online where i can input simple jquery commands
I've grown fond of using a generator-like pattern between functions in my shell scripts.
I have a very basic understanding of shell scripting, but what I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.