Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW2
Last active Sep 14, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@Hermann-SW2 Hermann-SW2 commented Sep 14, 2021

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