1

The whole code is too long to post here, and the whole thing isn't really relevant. I will do my best at explaining this though and using snippets of code/code examples where I can.

I have a script that generates a user driven menu from a command line on a Raspberry Pi. The script walks a user through selecting some options to parse through "Show Tech" files from Cisco Switches. This allows the user to process many files "at once" and generate output relevant to their needs.

I have the script working, however, I would like to be able to track the processing time of the files. I could simply use "time ./script_name" and then when it finishes I get the typical "real, user, sys" timestamps, but this would capture idle time and any delays caused by the user. As such, it would not be accurate for my needs.

I've broken most of my processes out into functions to simplify my own editing of the code and to reduce code when calling it in different areas. So imagine I have:

function_one () {
     steps for f1
     more steps for f1
}
function_two () {
     steps for f2
     more steps for f2
}
function_three () {
     steps for f3
     more steps for f3
}
function_four () {
     steps for f4
     more steps for f4
}

Now in my menu system, a user could select a task that would call three of the four functions, as whichever as needed to complete the task. An example would be:

elif [ "$menu_choice" == 2 ];
then
     function_one
     function_two
     function_four
elif [ "$menu_choice" == 3 ];
then
     function_two
     function_three
     function_four
elif ...

What I would like is to capture the execution time for it to run through the group of functions. In playing around with a few "tests" I know I can do things like:

time {
command_one_to_run
command_two_to_run
}

and this would give me the results when complete, but it doesn't let me capture the time reference into a variable that I can use to export to a collective "tracking" file.

Sorry for being lengthy, but in short... 1) User starts menu system 2) User selects menu item that runs three functions 3) Script should capture the time it takes to execute the three functions and store that as a variable (exec_time) 4) I can then redirect that time to an external file or use it however (for instance - echo $exec_time > ~/.myfile_name)

Hope that makes sense.

Barmar
  • 741,623
  • 53
  • 500
  • 612
pdxwarrior
  • 23
  • 5
  • 1
    `time bash -c 'command_one_to_run; command_two_to_run` ? `but it doesn't let me capture the time reference into a variable that I can use to export to a collective "tracking" file.` I don't understand, it does. `variable=$(time command 2>&1)` ? gnu time has also option to redirect output to a file. – KamilCuk Aug 23 '19 at 23:41
  • Possible duplicate of [How to redirect the output of the time command to a file in Linux?](https://stackoverflow.com/questions/13356628/how-to-redirect-the-output-of-the-time-command-to-a-file-in-linux) – KamilCuk Aug 23 '19 at 23:51

0 Answers0