I have a C program that takes 2 arguments, a file name and text. I want to write a script in bash that also takes 2 arguments, a path and a file extension, will iterate over all the files in the specified path and provide my C program as argument files with givenextension and text only.
Here is my C program, nothing special:
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc < 3) { fprintf(stderr, "Give 2 args!\n"); exit(-1); } char *arg1 = argv[1]; char *arg2 = argv[2]; fprintf(stdout, "You gave: %s, %s\n", arg1, arg2); return 0; }
and bash script:
#!/bin/bash path=$1 ext=$2 text=$3 for file in $path/*.$ext do ./app | { echo $file echo $text } done
I use it as follows: ./script /tmp txt hello and it should specify as arguments all txt files from /tmp and 'hello' as text to my C program. No, it only shows Give 2 args! :( Please, help.
Katie source share