0

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
4
  • Did you really mean to pipe the output of cd into ls? s/|/\&\&/ Commented Jul 9, 2014 at 15:04
  • And why bother with -l followed by cut to remove everything that -l added? Try the simpler: cd /David/reports && ls -tr | tail -1 Commented Jul 9, 2014 at 15:09
  • Why are you doing a recursive grep on a file? Recursion is only useful for directories, but if Getlastreport is not doing what you think (eg if it introduces whitespace) then you are potentially doing a recursive grep that you do not want. Commented Jul 9, 2014 at 15:10
  • Given both the text of your question and the code sample, I think there is a confusion here between a variable (i.e.: $SOMETHING) that hold a value, and a function which is callable (i.e.: `cmd arg1 arg2 arg3`)... Commented Jul 9, 2014 at 22:44

2 Answers 2

1

You can use:

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

`Getlastreport` - Call Getlastreport and get its output.
Sign up to request clarification or add additional context in comments.

1 Comment

Nope. Not working. It just prints out /David/reports/Getlastreport/"Moneyfromyesterday.csv which leds me to a no file. 'Getlastreport' is suppose to print out the last file that was modify and get me a number like 343432
0

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
}

2 Comments

This seems to send my script into a waiting for command phase. This was the first way I tried it actually and the way I learn it. Not sure why it isn't working.
I don't think your cut command is correct. That function doesn't return just filenames here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.