Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:46:11+00:00 2026-06-17T23:46:11+00:00

I want to run my program in C with bash script, also, I want

  • 0

I want to run my program in C with bash script, also, I want my bash script to pass some values to my program in C. This is my C code (very simple, it reads as input math operations, for example: 2 + 3, saves it to file, and thats all):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int howMany = 0, i = 0;
    float num1, num2;
    char sign;

    FILE *fp;
    if((fp=fopen("operations.txt", "w"))==NULL)
    {
        exit(-1);
    }

    printf("How many math operations would you like to pass?\n> ");
    scanf("%d", &howMany);

    for(i=0; i<howMany; i++)
    {
        printf("Pass %d operations like this: {num1 sign num2}:\n> ", i+1);
        scanf("%f %c %f", &num1, &sign, &num2);

        fprintf(fp, "%f ", num1);
        fprintf(fp, "%c ", sign);
        fprintf(fp, "%f", num2);
        if(i < howMany-1)
            fprintf(fp, "\n");
    }

    fclose(fp);
    return 0;
}

Then, I have my bash script, I would like it to run my program in C and give it 9 math operations: 1+2, 3+4, … 9+10. I did it like this:

#!/bin/bash

n=9
echo "$n" | ./app

for (( i=1; $i < 10; i++ )) ; do
       let "c=$i+1" 
       echo $i "+" $c | ./app
done

but theres a problem it doesnt work as I want it to. Please, help – only with this bash script, my C program works just great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:46:12+00:00Added an answer on June 17, 2026 at 11:46 pm

    The problem is that you’re executing your ./app several times, each time feeding it a small part of the whole.

    You can group the commands and then pipe it all to one instance of your app like this:

    #!/bin/bash
    { 
    n=9
    echo "$n" 
    
    for (( i=1; $i < 10; i++ )) ; do
           let "c=$i+1" 
           echo $i "+" $c
    done
    } | ./app
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a program from inside a bash script, such that it
I have writen a simple bash program. I want to run it as a
I have written php program and uploaded on server. I want run this program
I want run a script as follows: runner: ssh 'java program &' ssh 'java
I want to run script file on android Shell using Native C program. I
I want to run my java algorithm from bash script. When i run my
I want to run a shell script from my program below but it doesn't
I want to run a shell script that runs a python program and shutdowns
I want to run some commands via a remote program. I've tried it using
I would like a bash script to run a program (taking several variables and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.