Ray
Ray

Reputation: 21

Unix. Call a variable inside another variable

Currently I have a script like this. The intended purpose of this script is to use the function Getlastreport and retreive the name of lastest report in a folder. The folders name are typical a random generated number every night. I want to call the variable Getlastreport and put it inside Maxcashfunc.

Example :

Getlast report = 3473843. 

Use MAXcashfunc grep -r "Max*" /David/reports/$Getlastreport[[the number 3473843 should be here ]]/"Moneyfromyesterday.csv" > Report`

Script:

#!bin/bash

Getlastreport()
{
cd /David/reports/ | ls -l -rt | tail -1 | cut -d' ' -f10-
}

MAXcashfunc()
{
grep -r "Max*" /David/reports/$Getlastreport/"Moneyfromyesterday.csv" > Report
}

##call maxcash func
MAXcashfunc

Upvotes: 0

Views: 85

Answers (2)

Elliott Frisch
Elliott Frisch

Reputation: 201439

If I follow your question, you could use

function Getlastreport() {
  cd /David/reports/ | ls -l -rt | tail -1 | cut -d' ' -f10-
}
function MAXcashfunc() {
  grep -r "Max" /David/reports/$(Getlastreport)/"Moneyfromyesterday.csv" > Report
}

Upvotes: 0

anubhava
anubhava

Reputation: 785058

You can use:

MAXcashfunc() {
    grep -r "Max" /David/reports/`Getlastreport`/"Moneyfromyesterday.csv" > Report
}

`Getlastreport` - Call Getlastreport and get its output.

Upvotes: 1

Related Questions