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

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

How do I go about this?

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

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.

  • 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 Oct 27 '15 at 0:52

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.