0

I executed the below code as .sh file and getting the result as expected. Now I need to store the output of the "send" command and do things further.

I have done the below bash code and now I am keeping the output in a file:

#!/usr/bin/expect -f

spawn iroot
expect ".* password for"
sleep 3
send "password\r"
sleep 5
send "dmidecode -t system | grep Manufacturer > /tmp/manfacdetails.txt\r"
send "exit\r"
interact

How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sandy
  • 81
  • 1
  • 9
  • More than `variable=$(cat /tmp/manfacdetails.txt)`? Or are you looking for some other output? – tripleee Jan 29 '19 at 09:52
  • Thanks for your response. The way you told is one way. what i was thinking is it possible like `variable = send "dmidecode -t system | grep Manufacturer`. i am just trying to avoid creating a file and then again reading it. – Sandy Jan 29 '19 at 09:57
  • This is not a `.sh` file (or well, you can call it anything you like, Unix doesn't care; but it's not a shell script). Do you need help with `expect` actually? – tripleee Jan 29 '19 at 10:00
  • Possible duplicate of https://stackoverflow.com/questions/45036988/expect-store-output-of-a-spawn-command-into-variable – tripleee Jan 29 '19 at 10:01
  • @tripleee: i have saved this code as .sh file and executing. only thing i am looking for is based on the output of the mentioned command, need to give some condition and execute further commands. so if we can store it in a variable then it will be easy instead of creating a file and reading it – Sandy Jan 29 '19 at 10:05
  • take a look at [sexpect (Expect for Shells)](https://github.com/clarkwang/sexpect). it's much easier than Tcl/Expect. – pynexj Jan 29 '19 at 10:20
  • I still don't understand if you want to extend your existing `expect` script, or call it from an actual shell script. Either way, it's probably a duplicate. – tripleee Jan 29 '19 at 10:30
  • Yes need to extend. e.g. say `var = send "dmidecode -t system | grep Manufacturer" if [var -eq "ABC"];then do something else do something` Something of this kind – Sandy Jan 29 '19 at 10:35
  • That looks like shell script, but with multiple syntax problems. If you don't put the output from your current `expect` script to a file, does it print to standard output? What does `iroot` do? What's with the `interact` at the end, do you want to actually interact with `iroot`? After you `exit`? – tripleee Jan 29 '19 at 14:51

1 Answers1

1

It seems that you are using iroot to obtain root access to a phone and issuing a command as root.

I am assuming here that the command you already have is noninteractive, and produces the output you want if you take out the redirection to a file, without any human interaction.

Then, the simple matter of capturing its output is covered by the common FAQ How to set a variable to the output of a command in Bash?

#!/bin/bash

manufacturer=$(expect <<\____HERE)
    spawn iroot
    expect ".* password for"
    sleep 3
    send "password\r"
    sleep 5
    send "dmidecode -t system | grep Manufacturer\r"
    send "exit\r"
    interact
____HERE

if [ "$manufacturer" = "Motor Ola" ]; then
    ola=1
fi

# Maybe you'll prefer case over if, though
case $manufacturer in
    "Samsung" | "LG" ) korean=1 ;;
    "Apple")
        echo "$0: You're kidding, right?" >&2
        exit 127;;
    *)  echo "$0: Unknown manufacturer $manufacturer" >&2
        exit 1;;
esac

If this doesn't work for you then Use expect in bash script to provide password to SSH command has some variants you might want to try.

You also seem to be confused about the nature of scripts. Any executable file which has a shebang as its first line will be executable by whatever interpreter is specified there. Mine has /bin/bash so this is a shell script and more specifically a Bash script, while yours has expect as its interpreter, so it's an Expect script. You also commonly have Awk scripts and Perl scripts and Python scripts (and less commonly but not at all uncommonly scripts in many, many other languages).

As already mentioned, Expect is also a scripting language, and it is possible that you would like for yours to remain an Expect script, rather than a shell script with an embedded Expect script snippet. Perhaps then see expect: store output of a spawn command into variable

The name of the file which contains the script can be anything, but the standard recommendation is to not give it an extension -- Unix doesn't care, and human readers will be confounded if your .sh file is an Expect script (as it currently is).

Perhaps tangentially see also Difference between sh and bash as well as http://shellcheck.net/ which you can use to diagnose syntax errrors in your (shell) scripts.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • None of the outputs will actually print anything because the current output contains `Manufacturer`. Perhaps you want to trim it with https://stackoverflow.com/questions/10358547/how-to-grep-for-contents-after-pattern – tripleee Jan 30 '19 at 06:50