0
# function_1 extract lines with word Mark from file.

function_1() {
    file=$1
    grep "Mark" ${file}
}

Suppose output of funtion_1:

  • Question 1: Mark :5:
  • Question 2: Mark :1:
  • Question 3: Mark :3:

Suppose function_2 needs to extract integer from function_1 output to calculate total mark.

How to give output of function_1 to funtion_2?

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

0

Try this code:

FILE_NAME=marks.txt

function_1() {
    echo $(grep "Mark" $1)
}

function_2() {
    var="$@"
    echo $var
    #TODO: extract integers
}

ret=$(function_1 $FILE_NAME)

function_2 ${ret}
Homaei
  • 177
  • 1
  • 1
  • 10
  • echo is giving output in single line. – Fellin husk Nov 25 '19 at 01:31
  • Yes, it is. So what? In `function_2` you can perform your expected activity which is commented as `#TODO`. For example, if you want to extract numbers from string, it is possible to use `sed` and regex as explained in [https://stackoverflow.com/a/30490390/8809012 ] – Homaei Nov 25 '19 at 05:02
0

How to give output of function_1 to funtion_2?

Pipe output from function_1 to function_2.

function_1 | function_2
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Not working for me. Or maybe I am using in wrong way. – Fellin husk Nov 25 '19 at 03:18
  • The "not working for me" is very broad and not specific. What _exactly_ is not working for you? What have you tried? What source, exactly? Can you post it on some online site like [repl bash](https://repl.it/languages/bash) and test it? In what way is it "not working"? How do you detect the "not working state"? Is it not running at all? Is it setting your pc on fire? It is crashing your pc? Or just it doesn't print anything? Please, try to be specific. – KamilCuk Nov 25 '19 at 12:00
  • Sorry! Its works. Just made some few typing mistake. – Fellin husk Nov 25 '19 at 21:25