1

Let's say I have a bash script located in /home/user/examples that works as follows (it simply takes the user inputted number and adds it to itself ) :

[user]$ add 
Enter a number: 10

results:
entered value - 10
new value - 20

And I want to create another bash script located in /home/user/other that takes an argument that is given and uses it as the number for the add script from above, but only prints the new value. For example:

[user]$ organize 10

New Value
20

How do I go about this?

1
  • Just a side note, you are using too many spaces in your example. Fixing again. Commented Oct 27, 2015 at 1:07

1 Answer 1

2

The simple answer is

printf '%s\n' "$1" | /home/user/examples/scriptname.sh | sed -n -e '$ s/^new value - // p'

The more thoroughly correct answer is to write an expect script that looks for the prompt (Enter a number:), sends "$1" and Enter, and then extracts the result.

1
  • 2
    FYI, this is a good object lesson on why unix CLI tools tend to be very terse and why they tend to take input on the command line (and/or stdin) rather than asking questions of the user, and why they print just the bare results (without any excess or non-essential verbiage) as output: so it can be easily re-used by other tools.
    – cas
    Commented Oct 27, 2015 at 0:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .