1

If I have a script that does the following

    [alice]$ math
    Enter a number: 5

    Number squared: 25
    Number divided: 1
    Number to power of 3: 125

And I want to write another script that organizes the output from the math script into a table, but can also take multiple arguments and does not show the "Number divided". For example

   [alice]$ newprogram 5 10

   *
   *Number    squared    power of 3
    5         25         125
    10        100        1000     

Also, the math script is located in /home/alice and the newprogram is going to be located in /home/user. How do I approach this?

  • 2
    much simpler to just write a new version of the script. parsing dross verbiage like that is a major PITA. – cas Oct 27 '15 at 6:00
1

See my answer to your previous question for info on how to pipe arguments into your existing script. Parsing the output of this one will be harder than the last one.

It's much simpler to just write a new version of the script. Parsing dross verbiage like that is a major PITA.

Here's a version that does that, and also doesn't repeat the original mistake of printing excess output (like headers).

#! /bin/bash

for i in "$@" ;  do
   printf "%i\t%i\t%i\n" $i $(($i * $i)) $(($i * $i *$i))
done

And here's a version that optionally prints a header if you use the -v (verbose) option:

#! /bin/bash

verbose=0

while getopts "v" opt ; do
  case "$opt" in
      v) verbose=1 ;;
   esac
done

shift "$((OPTIND-1))"

[ "$verbose" = "1" ] && printf "%s\t%s\t%s\n" 'Number' 'Squared' 'Power of 3'

for i in "$@" ;  do
   printf "%i\t%i\t%i\n" $i $(($i * $i)) $(($i * $i *$i))
done
1

As the question uses the word 'approach' it implies the answer to be given is to be 'sketchy' or somewhat 'general' thus I am stating the following steps one could use to approach this:

  • Write an expect-script which takes an argument which is to be send to /home/alice/math.
  • Write a BASH-script (e.g. /home/user/newprogram) which invokes the expect-script in a loop with the arguments given to your BASH-script and with command substitution to catch its output.
  • Use your favourite text processing tool, e.g. grep to extract the data needed and store those in variables (for this you need to pipe the output you catched to grep, again using command substitution to catch the data the grep command delivers and storing this in a variable).
  • Output the variables you parsed in the step before this step in a table.

These steps do assume having file access (e.g. execute and read bit set) to /home/alice/math. Of course, if that is not the case one could e.g.:

  • Change permissions or owner for /home/alice/math only with chmod or chown.
  • Put alice and user in a common group with enough rights so that both users have access to /home/alice/math.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.