Skip to content
Advertisement

How to introduce an input in a C program through shell script?

When I execute the program in console I just do this:

./c1 2500
textfile.txt

and it just print a integer. The thing here is that I want to introduce 1000 textfiles as input so I made this script:

c=1
while [ $c -le 1000 ]
do
    ./c1 2500 >> sal.txt
    $c.txt 
    (( c++ ))
done

The trouble here is that the script is not putting the output in the file text because is not iterating as it should, I think the problem is when the name of the filetext is introduced as $c.txt, how can i solve this? Thanks for reading

Advertisement

Answer

$c.txt is not a command and the bash interpreter can’t understand what that means
if you want to create a file, use touch [file]
or you want to copy a existing file to the destination, use cp [src_file] [dst_file]
so the code may like this:

    ./c1 2500 > $c.txt

or you may want to append the result to a file:

    ./c1 2500 > $c.txt
    cat $c.txt >> sal.txt

ps:
> and >> both of these operators represent output redirection
> writes the output to the file
>> appends the output to the file
cat concatenates files and print on the standard output

9 People found this is helpful
Advertisement