Question

Write a bash script question.sh that accepts one command line argument which is supposed to be...

  1. Write a bash script question.sh that accepts one command line argument which is supposed to be a positive integer n. The script should print all odd integers from 1 through n. Write a C or C++ program question.c(pp) to read from stdin as many integers as there are available and then print the squares of all these integers. You should test your code by “./question.sh <n> | ./question” assuming the compiled C/C++ program is question. See the following for a sample run.

[@computer][~/temp]$bash question.sh 10 | question

1    9    25   49   81

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Name your files correctly as mentioned in the question, compile the cpp program, and then execute the command mentioned in question to redirect the input from bash script to the cpp program which displays the squares of each values. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Required bash script

#!/bin/bash

#initializing counter from 1

counter=1

#looping until counter exceeds the value given by first command line argument

while [ $counter -le $1 ]

do

    #if counter is an odd number, printing it

    if (($counter%2!=0))

        then

            echo $counter

        fi

    #advancing counter

    ((counter++))

done

Required C++ program.

#include<iostream>

using namespace std;

int main(){

                //declaring an integer variable

                int input;

                //looping as long as an integer value is successfully read from standard input

                //when there is no input, cin>>input will return false / 0

                while(cin>>input){

                                //displaying square of the input

                                cout<<input*input<<endl;

                }

                return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a bash script question.sh that accepts one command line argument which is supposed to be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT