-2

My question is an enhancement for the following one passing arguments to an interactive program non interactively.

I am using a script which will ask me to choose an option from the list:

1 - ABC
2 - CDE
3 - EFG

I know which option I should choose based on letters, but script expect to provide a number.

Is there any way to do it in a non-interactive way? I mean to pass a "parameter" which will find a number based on the given letters.

[EDIT 1]

I see that my description is not very clear, therefore I am adding a test case. I have a script: test.sh. It is the interactive script, so question are asked when I run it:

./test.sh
Are you sure you want to run it? (y/n): y
Please choose option:
1 - ABC
2 - CDE
3 - EFG
1

To run this script in a non-interactive way I am using:

echo "y
1" | test.sh

The problem is that when I am running the script I don't know which number is assigned to the option that I would like to choose.

kpater87
  • 1,190
  • 12
  • 31

2 Answers2

0

Your use-case is not really clear but i can guess what you're looking for could be implemented with grep and sed.

Example :

cat choices.txt
1 - ABC
2 - CDE
3 - EFG

Find EFG in choices.txt then sed to get the number (find any group of digits, and output only this group) :

cat choices.txt | grep EFG | sed 's/^\([0-9]*\).*/\1/'
3
jseguillon
  • 393
  • 1
  • 10
0

Posing this question I was looking for any hint how to start with this task. After spending hours on searching finally I managed to implement sth that meets my requirements using expect script.

If you have any idea how to improve this script, any comments are welcome.

#!/usr/bin/expect -f

proc parse_output {search_in to_find} {
    set lines [split $search_in \n]
    foreach line $lines {
        if {[regexp "$to_find" $line]} {
            puts $line
            set i [string first "-" $line]
            set n [string range $line 0 [expr {$i-1}]]
            set nn [string trim $n]
            send -- "$nn\r"
        }
    }
}

set PARAM1 [lindex $argv 0]

spawn deploy
expect "?choose change to deploy:"
parse_output $expect_out(buffer) "$PARAM1\r"
expect "$" {send "\r"}
kpater87
  • 1,190
  • 12
  • 31