Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW2
Last active September 14, 2021 10:14
Show Gist options
  • Save Hermann-SW2/209918befa911209e06c5812e438dc37 to your computer and use it in GitHub Desktop.
Save Hermann-SW2/209918befa911209e06c5812e438dc37 to your computer and use it in GitHub Desktop.
C script demo application, demoing pipeing input onto two pipes
#!/bin/bash
exc=/tmp/headtail.c
if [ $0 -nt $exc ]; then sed -n "/^\/\*\*$/,\$p" $0 | gcc -x c - -o $exc; fi
cat | $exc
exit
/**
*/
#include <stdio.h>
#define max 1000
int main(int argc, char *argv[])
{
char line[max+1];
FILE *pip1 = popen("bash -c \"head -3\"", "w");
FILE *pip2 = popen("bash -c \"tail -2\"", "w");
fgets(line, max, stdin);
while (! feof(stdin))
{
fputs(line, pip1);
fputs(line, pip2);
fgets(line, max, stdin);
}
pclose(pip1);
pclose(pip2);
return 0;
}
@Hermann-SW2
Copy link
Author

https://www.raspberrypi.org/forums/viewtopic.php?f=33&t=319473#p1913176

$ echo -e "1\n2\n3\n4\n5\n6\n7" | ./headtail.c 
1
2
3
6
7
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment