You could use the standard library command system() to run your bash command (it is a bash command, because you have redirection), but...
There is a performance overhead in using a shell, since you now have two child processes. Using a shell can be open to security holes like shell shock.
While using system() might be OK for a "quick and dirty", you might wish to consider using the native APIs instead, which does not rely on a shell. It is a lot of work though, which explains why people wimp out and use system()!
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main (int argc, char *argv[])
{
pid_t pid;
switch (pid=fork())
{
case -1: perror("fork failed");
exit(1);
case 0:
// In the child
// redirect stdout (fd 1) to file
{
int fd = open("holaOrdenado", O_CREAT|O_WRONLY, 0);
if (fd < 0)
perror("Failed to open holaOrdenado");
else
dup2(fd, 1);
execlp ("sort", "sort", "hola", NULL);
perror ("sort exec failed");
exit(2);
}
default:
// In the parent
{
int status;
if (wait(&status) > 0 && WIFEXITED(status)) {
fprintf(stderr, "Child exited with status %d\n",
WEXITSTATUS(status));
}
}
}
return 0;
}
There is a lot of code there, and probably a lot of APIs that you have not seen before. Use the man pages for a full explanation, for example: man 2 write.
sortis not bash function, it's a binary. Surely you can have a look atsystemfunction inC.system("sort filename");in C. You can also read the file and sort in yourself than usingsortcommand.