0

I have the following expect statement within my bash script:

/usr/bin/expect << EOF
spawn -noecho lucli users add -username user -role admin -email 
[email protected]
expect "password:" { send "password\n" }
expect "password:" { send "password\n" }
expect eof
EOF

I want the expect script to validate that the correct output is returned from the CLI command after it passes the passwords and creates the user.

The message I want to validate that gets returned is "added to the system successfully"

I can't figure out how to do that from within the bash script using expect.

Can anyone help?

1
  • 1
    Is there a reason you're using expect here at all, rather than passing a pre-crypted copy of the password on the command line? Expect isn't part of bash -- it's a completely separate programming language derived from TCL, and is otherwise a big hammer to pull in. Commented Jan 8, 2018 at 19:09

1 Answer 1

1

You could try something like this:

# note the quoted here-doc word
status=$(/usr/bin/expect << 'EOF'
    spawn -noecho lucli users add -username user -role admin -email 
    [email protected]
    expect "password:" { send "password\r" }
    expect "password:" { send "password\r" }
    expect eof
    set status [string match "*added to the system successfully*" $expect_out(buffer)]
    # $status will be the C-like boolean 0 or 1
    exit $status
EOF
)
if [[ $status -eq 1 ]]; then
    echo "user added OK"
else
    echo "user not added"
fi

ref: https://tcl.tk/man/tcl8.6/TclCmd/string.htm

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.