2

I have been asked to write a script that interacts with another one - but I'm stuck. The script that I interact with echos the following and I need to send a "1" back. But I can't quite get there...

echo -e "Select option 1"
echo -e "? \c"

So far I have tried - for as far as I remember:

expect "?"
send "1" 

expect "? "
send "1"

expect "? \n"
send "1"

expect "? \c"
send "1"

It all doesn't seem to work. Can somebody please give me a nudge in the right direction...? :)

P.S.: I assume I'll need to add a \r to the 1 once I take the first hurdle...

  • 1
    Try expect -ex "? " – Mark Plotnick Dec 17 '14 at 19:41
  • 2
    ? is a wildcard that matches any character, so yes, you need -ex, or expect "\\?" or expect {\?} – Stéphane Chazelas Dec 17 '14 at 21:47
1

In echo -e "? \c", the \c part is not anything that gets printed out, it's a directive to the echo command to not print a newline after the string passed as an argument¹. So in expect, you need to expect the string "? " (question mark, space). Since the argument of the expect command is a pattern where ? is a wildcard, you need to interpret the question mark literally:

expect -ex "? "
send "1\r"

¹ Some other implementations of echo, such as the bash builtin, use the syntax echo -n "?" for this.

0

You're almost there. Consider using the read built in (From TDLP: Catching User Input):

Read Example

cat leaptest.sh

#!/bin/bash
# This script will test if you have given a leap year or not.

echo "Type the year that you want to check (4 digits), followed by [ENTER]:"

read year

if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 !=
"0") )); then
  echo "$year is a leap year."
else
  echo "This is not a leap year."
fi

Notice Line 6. The variable year is created on the fly, by BASH to hold the output for the echo statement.

Testing User Input Example

cat friends.sh

#!/bin/bash

# This is a program that keeps your address book up to date.

friends="/var/tmp/michel/friends"

echo "Hello, "$USER".  This script will register you in Michel's friends database."

echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your gender and press [ENTER]: "
read -n 1 gender
echo

grep -i "$name" "$friends"

if  [ $? == 0 ]; then
  echo "You are already registered, quitting."
  exit 1
elif [ "$gender" == "m" ]; then
  echo "You are added to Michel's friends list."
  exit 1
else
  echo -n "How old are you? "
  read age
  if [ $age -lt 25 ]; then
    echo -n "Which colour of hair do you have? "
    read colour
    echo "$name $age $colour" >> "$friends" 
    echo "You are added to Michel's friends list.  Thank you so much!"
  else
    echo "You are added to Michel's friends list."
    exit 1
  fi
fi

In your particular case, you would replace Line 5 with the list of options from the script, and starting at line 17 modify the if to match the option which you passed as ANS. If the option matches the if, execute your script, as in sh myscript.sh --option ANS

  • The actual script I'm talking to has a READ statement: echo -e "Select option 1" echo -e "? \c" read ANS But my script is calling that script and mine then tires to "play" user... Sorry if that was unclear... – Christian Dec 17 '14 at 18:03
  • You're forgetting that you can pass ANS to your script as an argument. – eyoung100 Dec 17 '14 at 18:12

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.