-2

I have a c program that accepts as argument a string. The string input as argument is a single line of a file, so for example one of my files contains the following:

0410000340000230
1111111111111111
1800400700032050

So far I've writen a bash script to automate the work:

#!/bin/bash
read -p "make clean"
make clean
read -p "make"
make

file=$1
while read line; do
echo $line
done < $file

So far, so good. Right now, I want to use the output from the echo $line command as argument to my program. I've tried:

echo $line | ./program
echo $line > ./program
echo $line < ./program
./program < echo $line

Sometimes it will just give me an error that there are no arguments for my program and at other times it will arrive at a segmentation fault.

Can someone tell me what I'm doing wrong?

The C code of the main program:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "functions.h"

int main(int argc, char * argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./Sudoku <Sudoku en une seule ligne. Represantation des cases vides avec 0>\n");
        return 0;
    }
    FILE *f, *g;
    int n, n_clauses, ** Sudoku;
    n = (int) sqrt((double) strlen(argv[1]));
    if (ceil(sqrt((double) strlen(argv[1]))) == sqrt((double) strlen(argv[1])))
    {
        Sudoku = (int **)malloc(n * sizeof(int *));
        for (int i = 0; i < n; i++)
        {
            Sudoku[i] = (int *)malloc(n * sizeof(int));
        }
        remplir_sudoku(argv[1], n, Sudoku);
    }
    else
    {
        n = (int) sqrt((double) find_sudoku_length(argv[1]));
        printf("\n %d\n", n);
        Sudoku = (int **)malloc(n * sizeof(int *));
        for (int i = 0; i < n; i++)
        {
            Sudoku[i] = (int *)malloc(n * sizeof(int));
        }
        remplir_sudoku_v2(argv[1], n, Sudoku);
    }
    afficher_sudoku(Sudoku, n);

    n_clauses = nb_clauses(n);
    n_clauses = n_clauses + nb_remplis(Sudoku, n);
    f = fopen("CNF", "w");
    fprintf(f, "c CNF\n");
    fprintf(f, "p cnf %d %d\n", n*n*n, n_clauses);
    transformer_en_cnf(f, Sudoku, n);
    fclose(f);
    return 0;
}
  • Just do `./program $line`. Of course this depend on how your C program processes command line arguments. You may want to post that part of your C program. – Red Cricket May 05 '19 at 23:27
  • "*the output from the echo $line command*" is just `$line`. No need to go through `echo`. – melpomene May 05 '19 at 23:29
  • @RedCricket I've also tried that and it gives me a segmentation fault – Sotiris Kettenis May 05 '19 at 23:32
  • 2
    `./program "$line"` will put the contents of `line` in `argv[1]`. – Charles Duffy May 05 '19 at 23:41
  • @CharlesDuffy tried it. when my file has no extension I get a segmentation fault and when my file is a txt I get an ambiguous redirect – Sotiris Kettenis May 05 '19 at 23:46
  • 2
    @SotirisKettenis, the segfault means there's a bug in your C program. Doesn't mean the shell usage is wrong. As for the ambiguous redirect, `./program "$line"` will never do that, so you'd need to show me what you *were* doing to cause that error. – Charles Duffy May 05 '19 at 23:46
  • (You can get an ambiguous redirect from `./program <$line`, for example, but that's *wrong*, so you shouldn't be writing it in the first place). – Charles Duffy May 05 '19 at 23:52
  • @CharlesDuffy there is absolutely no bug. I debugged my program before writing the script. If I manually give the argument, it works. As for the ambiguous redirect, when i try to execute ./program "$line" it says $file: ambiguous redirect – Sotiris Kettenis May 05 '19 at 23:55
  • 2
    Absolutely not. `./program "$line"` does not have any redirection operator in it at all. It cannot *possibly* trigger the relevant pieces of the shell parser. – Charles Duffy May 05 '19 at 23:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192873/discussion-between-sotiris-kettenis-and-charles-duffy). – Sotiris Kettenis May 05 '19 at 23:56
  • 1
    As an aside, why not the first `malloc` be `Sudoku = malloc(n * sizeof Sudoku);` ? See [this](https://stackoverflow.com/a/605858/1620779) on why not cast the result of `malloc` – sjsam May 06 '19 at 00:04
  • 1
    strongly suggest read/understand the MAN pages for those C library functions that you are using in your code. for instance the function: `sendto()` does NOT contain a file descriptor for the data to be sent. – user3629249 May 06 '19 at 00:10

1 Answers1

3

The segfault was caused by the input file containing carriage return literals, without the C program being written to handle the case.

The "ambiguous redirect" was caused by passing a filename with spaces without correct quoting. Always quote your expansions: <"$file", not <$file.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441