Redirect input and output to a shell script?


 
Thread Tools Search this Thread
Top Forums Programming Redirect input and output to a shell script?
# 1  
Old 04-19-2010
Redirect input and output to a shell script?

Dear All:

I am trying to do something that (I thought) was relatively straightforward, but my code snippet does not seem to work.

Any suggestions?

Thank you

Sincerely yours
Misha Koshelev

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>

int INPUT=0;
int OUTPUT=1;

int main(int argc, char *argv[]) {
  int childin[2],childout[2]; /* half-duplex pipes */
  int nbytes;
  char buff[80];

  if (pipe(childin)||pipe(childout)) { /* error */
    perror("pipe");
    exit(127);
  }

  switch (fork()) {
  case -1: /* error */
    perror("fork");
    exit(128);
    break;

  case 0: /* child */    
    signal(SIGHUP,SIG_IGN); /* ignore hup */

    /* close wrong end of pipes */
    close(childin[OUTPUT]);
    close(childout[INPUT]);

    /* duplicate pipes to stdio and stdout */
    dup2(STDIN_FILENO,childin[INPUT]); 
    /*
    dup2(STDOUT_FILENO,childout[OUTPUT]);
    */

    /* start NX */
    /*    execl("/home/misha/nx/nxserver","nxserver",NULL); */
    execl("/bin/bash","bash",NULL);
    exit(129);

    /* never get here*/
    break;

  default: /* parent */
    /* close wrong end of pipes */
    close(childin[INPUT]);
    close(childout[OUTPUT]);

    /* pass I/O */
    while (1) {      
      nbytes=read(STDIN_FILENO,buff,sizeof(buff));
      if (nbytes>0) {
    write(childin[OUTPUT],buff,nbytes);
      }
      /*
      if (nbytes=read(childout[INPUT],buff,sizeof(buff))) {
    write(STDOUT_FILENO,buff,nbytes);
    }*/
    }

    exit(130);
  }
}



---------- Post updated at 05:24 PM ---------- Previous update was at 04:54 PM ----------

Seems to work for a while, then bash goes into background.

Am I missing something?

Thank you
Misha

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>

int INPUT=0;
int OUTPUT=1;

int main(int argc, char *argv[]) {
  int childin[2],childout[2]; /* half-duplex pipes */
  int nbytes;
  char buff[1000];

  if (pipe(childin)||pipe(childout)) { /* error */
    perror("pipe");
    exit(127);
  }

  switch (fork()) {
  case -1: /* error */
    perror("fork");
    exit(128);
    break;

  case 0: /* child */    
    signal(SIGHUP,SIG_IGN); /* ignore hup */

    /* close wrong end of pipes */
    close(childin[OUTPUT]);
    close(childout[INPUT]);

    /* duplicate pipes to stdio and stdout */
    dup2(STDIN_FILENO,childin[INPUT]); 
    /*
    dup2(STDOUT_FILENO,childout[OUTPUT]);
    */

    /* start NX */
    /*    execlp("ed","ed",NULL); */
    /*    execl("/home/misha/nx/nxserver","nxserver",NULL); */
    execl("/bin/bash","bash",NULL);
    exit(129);

    /* never get here*/
    break;

  default: /* parent */
    /* close wrong end of pipes */
    close(childin[INPUT]);
    close(childout[OUTPUT]);

    /* pass I/O */
    while (1) {      
      printf("a\n");
      nbytes=read(STDIN_FILENO,buff,sizeof(buff));
      printf("b\n");
      if (nbytes>0) {
    buff[nbytes]=0;
    printf("c %d %d %s\n",childin[OUTPUT],nbytes,buff);
    write(childin[OUTPUT],buff,nbytes);
      }
      /*
      printf("d\n");
      nbytes=read(childout[INPUT],buff,sizeof(buff));
      printf("e\n");
      if (nbytes>0) {
    printf("f\n");
    write(STDOUT_FILENO,buff,nbytes);
    }*/
    }

    exit(130);
  }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect output to the same input file in awk

Hi, I want to compare a value from test file and redirect the o/p value to the same file input file 250 32000 32 128 Below is my code awk '{ if ($1 < "300") print $1 > /tmp/test}' test want to compare 250 < 300 then print 300 to the same place below is the... (24 Replies)
Discussion started by: stew
24 Replies

2. Programming

How to redirect the output of a shell script to a file?

hi, i have a html form which call a perl program, this perl program calls a shell script. <html> <head> <title>demo</title> </head> <body> <form name="frm1" action="/cgi-bin/perl_script.pl" method="post"> <input type="text" name="fname"> ... (1 Reply)
Discussion started by: Little
1 Replies

3. Shell Programming and Scripting

Shell Script - File Input/Output in C

This is part of my code: for in_file in $1/*.in # list of all .in files in working directory. do $c_file < $in_file > "$tempFile.out" if diff "$tempFile.out" $out_file >/dev/null 2>&1 ; then ... (6 Replies)
Discussion started by: spider-man
6 Replies

4. Shell Programming and Scripting

Redirect the output in shell script for tftp

I've been using tftp in one of my file #!/bin/bash filename1="config1h.txt" filename2="config15.txt" hostname="test.com" tftp $hostname <</dev/null get $filename1 get $filename2 quit EOF My output looks like this # ./test3.sh tftp> Received 1262 bytes in 0.0 seconds tftp> Received... (2 Replies)
Discussion started by: LavanyaP
2 Replies

5. UNIX for Advanced & Expert Users

Complex Input/Output Redirect

Hi All, Sorry if the title is not good but I did not know how to explain with only some words! What I meant is: I have a unix command built from a private application vendor that when executed it prompts for two entries by the keyboard, let's say, for example: ... (1 Reply)
Discussion started by: felipe.vinturin
1 Replies

6. Shell Programming and Scripting

redirect an awk string output to a script input with pipes

Hi, I have a function in a bash script that returns a string after some operations using awk. The following code returns 555 $VARIABLE="EXAMPLE" get_number $VARIABLE this value I'd like to pass it as a second argument of another script with the following usage myscript.sh <param1>... (7 Replies)
Discussion started by: rid
7 Replies

7. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies

8. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

9. Shell Programming and Scripting

Shell script getting input from output

I have a program that can be run in terminal, when its run it either returns SSH OK or CRITICAL, how do i use the output in my script? good ./check_sh myserver SSH OK bad ./check_sh myserver CRITICAL I want to store it in a variable btw, SSH OK will give the variable $SSH=1 and if its... (1 Reply)
Discussion started by: aspect_p
1 Replies

10. Shell Programming and Scripting

Asking about shell script input output redirection

Hi, Can anyone please tell me what these lines do? ls >& outfile ls outfile 2>&1 Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies
Login or Register to Ask a Question