I have the following code, which creates children process. Children process is a shell script on unix platform, which prints two lines to stdout. Parent process reads that output via the pipe:

char *shell_command = "cd /home/user; for infofile in *.info ; do grep -q 'PID: 10631$' ${infofile} ; if [ $? = 0 ]; then grep 'Trace File:' ${infofile} | awk '{ print $3 }' ; grep 'Trace Signal:' ${infofile} | awk '{ print $3 }' ; fi; done";
char *buf = new char[255];

cout<<"1"<<endl;
FILE *cmd_ptr=popen(shell_command, "r");
if (cmd_ptr != (FILE *) NULL){
    fgets(buf, 254, cmd_ptr);
    if ((feof(cmd_ptr) || (strlen(buf) == 0))){
        exit 1;
    }
    cout<<"2"<<endl;
}

After performing that code I expect to get in stdout digits "1" and "2", but the following output is appeared:

1
Not a terminal
2

What means "Not a terminal" and which point in the code generates this phrase? Is it is from popen() function or is it from that children shell script?

Moreover, I expect the buffer contain the line:
/xtz/sysp/SysPilot/instances/tst/err/xtz_11.0-0.neis_0001
But actually it is like the following:
<thrash>/xtz/sysp/SysPilot/instances/tst/err/xtz_11.0-0.neis_0001

where <thrash> is 97 symbols. Why that thrash was appeared in the beginning of the buffer? If you are interesting in that thrash, below are HEX codes of that 97 symbols. It is interesting that the first and the last thrash-symbols have code 0D (13):

0D 1B 5B 33 67 0D 20 20 20 20 20 20 20 20 1B 48 20 20 20 20
20 20 20 20 1B 48 20 20 20 20 20 20 20 20 1B 48 20 20 20 20
20 20 20 20 1B 48 20 20 20 20 20 20 20 20 1B 48 20 20 20 20
20 20 20 20 1B 48 20 20 20 20 20 20 20 20 1B 48 20 20 20 20
20 20 20 20 1B 48 20 20 20 20 20 20 20 20 1B 48 0D

thanks for any your suggestions.

> 1B 5B 33 67
= ESCAPE [ 3 g
> 1B 48
= ESCAPE H
http://clusty.com/search?query=escape+sequence+ANSI&sourceid=Mozilla-search
And an ASCII table if you can't do that stuff in your head yet.

I'm guessing your "cd /home/user" is an alias for something which outputs a bunch of escape sequences and then changes directory.
All fine and dandy if you're using a terminal, but utter crap if it's in a pipeline.

You're going to be a lot better off putting chdir( "/home/user" ); in your C code.

I'm somewhat surprised that such a complicated shell command passed to popen actually does something reasonable.

I'm guessing your "cd /home/user" is an alias for something which outputs a bunch of escape sequences and then changes directory.
All fine and dandy if you're using a terminal, but utter crap if it's in a pipeline.

You're going to be a lot better off putting chdir( "/home/user" ); in your C code.

I'm somewhat surprised that such a complicated shell command passed to popen actually does something reasonable.

I changed using changing directoty in my code, but problem still happens:

char *shell_command = "for infofile in *.info ; do grep -q 'PID: 10631$' ${infofile} ; if [ $? = 0 ]; then grep 'Trace File:' ${infofile} | awk '{ print $3 }' ; grep 'Trace Signal:' ${infofile} | awk '{ print $3 }' ; fi; done";

chdir( "/home/user" );

So, the problem is not in "cd" command. I googled that escape sequences. They mean:
<EOL><remove all lead tabs><EOL><many spaces and backspaces><EOL>

I cannot understand which command exactly in the shell generates these sequences. This shell commands are performed under terminal vt100. Is it possible that changing terminal to a xterm will solve a problem?

Do you have other crap in your .bashrc (for example)?

Do you get the same crap if you have a really simple command like say "echo hello"?

Does for infofile in *.info ; do grep -q 'PID: 10631$' ${infofile} ; if [ $? = 0 ]; then grep 'Trace File:' ${infofile} | awk '{ print $3 }' ; grep 'Trace Signal:' ${infofile} | awk '{ print $3 }' ; fi; done | od -Ax -t x1z show only the characters you expect, and nothing else?

Does sh -c echo hello | od -Ax -t x1z show the junk characters?

If so, then I'd say it was something in your initial shell startup which is just printing something.

If you do find it, then either remove it, or put it inside something like

if [ "$PS1" != "" ]; then
  # do interactive shell specific stuff here
fi

Thank you, Salem

Issue is solved. The problem was in some extra output from user's ~/.profile

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.