Transfer variable to an expect function

Hi There,

I try to transfer a variable from the script to a function which use expect, but I don't succed.

#!/bin/sh

HPPASS1="$2"


send_command()
{
        echo "spawn ssh login@10.10.10.10"
        echo 'set password [lindex $argv 0]'

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $password'
        ...
        echo 'expect oet'

}
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

The device say me it's not the good password.

Thanks by advance.

#!/bin/sh
send_command()
{
      password=$1
        echo "spawn ssh login@10.10.10.10"
        echo 'set password [lindex $argv 0]'

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $password'
        ...
        echo 'expect oet'

}
HPPASS1="$1"
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

---------- Post updated at 09:44 AM ---------- Previous update was at 09:42 AM ----------

#!/bin/sh
send_command()
{
      password=$1
        echo "spawn ssh login@10.10.10.10"
        echo 'set password [lindex $argv 0]'

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $password'
        ...
        echo 'expect oet'

}
HPPASS1="$1"
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

what menas $arg in your code? i said yoou because yo don't hava this variable in the shell.

Thanks chipcmc for your answer, finnally I find another solution; I don't use the expect way of define variable and use the classical way to send variable to a function:

#!/bin/sh

HPPASS1="$2"

send_command()
{
        echo "spawn ssh login@10.10.10.10"

        echo 'sleep 1'
        echo 'expect "*assword:*"'

        echo 'sleep 1'
        echo 'send  $1'
        ...
        echo 'expect oet'

}
conf_=`send_command "$HPPASS1" | /usr/bin/expect >> temp_log`

After I don't know if it's the better way but it's shorter and it works!

the are one think i don't understand

in main of your shell:
HPPASS1="$2"
send_command "$HPPASS1

this means that you call you script with 2 parameter?i think that only call with one paremeter for this reason i put HPPASS1=$1 xD....

in the fuction i put the varible password with the propouse that you see the relation betwen the call and the parameters of the function.

The are not great diferences:
password=$1 ........ echo "send $password"

than
.......................... echo "send $1"
the principal diferent is that one is more easy to read and to manteiniment of the shell, this is important in big shell.

In you case i think not diferent to use a temp variable or use de $1,$2 ....

Haha, yes I'm sorry about this :slight_smile:

Exactly, I works with 2 parameters, but for the example I delete one because it was not more interesting to explain with 2 on the forum.

So in my first message, the code should be

HPPASS1="$1"

and not

HPPASS1="$2"

Thanks for interesting :wink:

please ignore