Expect not working

Im trying to write a script that will change user to cyrus then reconstruct mailbox for the user defined in the parsed argument. If i was loggin in to terminal to do this manually for wendys mailbox i would do this

sudo su cyrus
/usr/bin/cyrus/bin/reconstruct -r user/wendy

That would reconstruct her mailboxes. Now i tried to do in a script using expect which i have never tried before heres my code

#! /usr/bin/expect -f

if [ ! $1 ]
then
    echo "You must enter a user name!";
    echo "Aborting!!";
    exit 1;
fi



echo "changing to user cyrus"

spawn "su cyrus"
expect "#"
send "/usr/bin/cyrus/bin/reconstruct -r user/$1"

echo "reconstructed mailbox" $1
echo "finished"
exit 1;

However this gives me the following errors

why is spawn not found? isnt it a unix command or something? What shall i do next?

How did you execute your script?

sudo sh test.sh wendy

i found out that i should be calling the script with expect

heres the new code.

! /usr/bin/expect -f
set username [lindex $argv 0]

spawn su timgolding
expect "Password:"
send "secret\r"

expect "timgolding$"
send "sudo su cyrus\r"

expect "Password:"
send "secret\r"

expect "cyrus$"
send "/usr/bin/cyrus/bin/reconstruct -r user/$username\r"

expect eof

i save the script to /home/scripts/reconstruct_mailbox.sh
to call the script and refconstruct wendys mailbox i call

expect /home/scripts/reconstruct_mailbox.sh wendy

and it works :slight_smile:

Have you tried..?

 su cyrus -c /usr/bin/cyrus/bin/reconstruct -r user/<username>