4

I have a C program and the inputs are given through command line. Now I want to pass the inputs from a text file. There are two inputs, a and b, written line by line like this:

a    b
2   423
4    56.9
7   83.5

Now I want to call this txt file in a loop and pass each a and b value line by line so that the output of the C program is generated for each a and b value.

To do so I want to write a shell script which will call the txt file in read mode and after getting the data it will call the C program and then the C program will do the rest including the output.

As I am new to this shell scripting I am confused about how to open a text file and then read data line by line and store them in some variable in the shell script.

Please suggest how can I open the text file in read mode and then read data (which may not be always integers) and store them in some other variables.

  • 4
    why don't you read the txt file with your C program? – damadam Jul 5 '18 at 6:21
  • you can also try accessing the file directly from your C program using various file handling methods. – Jrct Jul 5 '18 at 6:22
  • 2
    because I am calling a pre- written C program that and there is no provision to modify it as I have the access of exe file only. So to incorporate this program in my program I want to run a shell script. – Atrayee Hazra Jul 5 '18 at 8:09
  • Can you add examples, based on the example file contents you already provided, how exactly your "C application" needs to be called? – Byte Commander Jul 5 '18 at 11:22
12

Assuming "inputs are given through command line" means that the program accepts command-line arguments, then you should be able to do this with xargs

Ex. given a minimal C program

#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("Running %s\n", argv[0]);
  for(int i=1; i<argc; i++) printf("arg %d = %s\n", i, argv[i]);
  printf("\n");
}

compiled with gcc -o someprog someprog.c, and a file of whitespace-separated input arguments inputs.txt

$ cat inputs.txt 
2   423
4    56.9
7   83.5

Then

$ xargs -L1 ./someprog < inputs.txt
Running ./someprog
arg 1 = 2
arg 2 = 423

Running ./someprog
arg 1 = 4
arg 2 = 56.9

Running ./someprog
arg 1 = 7
arg 2 = 83.5

If your input file has a header, you will need to remove it (using tail for example).


If parallel processing would be computationally advantageous for your task, then you could do a similar thing with GNU Parallel

parallel --colsep '[ \t]+' ./someprog {1} {2} :::: inputs.txt
5

Here is a simple Bash example of such script:

#!/bin/bash
tail -n +2 "input-file" | sort -u | while read -a args
do 
    echo "${args[0]}" "${args[1]}"
done
  • The first command tail -n +2 input-file will output the content of the file ./input-file, but without the first line. Here is assumed that the Bash script and the input file are in the same directory - ./ is the path to the current directory.

  • This output will be piped | to the standard input of the command sort (with -u only the unique lines will be outputted), that will sort this input by the first column (use man sort to find out more options).

  • The output of the command sort -u is piped to the STDIN of the while-do-done loop, where each line will be read as an array -a.

  • Further the echo command will output the first and the second element of the array, named args. You should tweak this line - change echo with the path and the name of your C program.

The one line syntax of this example is:

tail -n +2 "./input-file" | sort -u | while read -a ar; do echo "${ar[0]}" "${ar[1]}"; done

Another example:

#!/bin/bash
INPUT_FILE="/path/to/input-file"
tail -n +2 "$INPUT_FILE" | sort -u | while read -a args
do 
    echo "${args[@]}"
done
  • Here the name of the file input-file is changed with the variable $INPUT_FILE, which value is assigned preliminary.
  • Also echo "${args[@]}" will output the whole content of the array args simultaneously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.