Create a shell file with the name of rootpass.sh and add below codes, then give execute permission to that file.

# chmod +x rootpass.sh

Set root password directly in shell script, assume that the root password is: rpasswd


#!/usr/bin/expect -f

spawn sudo -s <<EOF
expect "password:"
send -- "rpasswd\r"
expect eof


If you want to pass arguments(root password)create a file name rootpass.sh and add below code in it.


#!/usr/bin/expect -f

set rpasswd [lindex $argv 0]
spawn su - root
expect  "password:"
send  "$rpasswd\r"
interact


I hope, the above scripts is functional.