如何将shell脚本变量传递到Expect脚本中?
我想将$SPACE变量传递到一个Expect脚本中,获得输出,然后再次传递到shell脚本中如何将shell脚本变量传递到Expect脚本中?,shell,expect,Shell,Expect,我想将$SPACE变量传递到一个Expect脚本中,获得输出,然后再次传递到shell脚本中 #!/bin/bash -x SPACE=$(df -h | awk '{ print $5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1) #set user "root" #set ip "192\.168\.53\.197" #echo $SPACE expec
#!/bin/bash -x
SPACE=$(df -h | awk '{ print $5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1)
#set user "root"
#set ip "192\.168\.53\.197"
#echo $SPACE
expect<<EOF
spawn ssh "root\@192\.168\.53\.197"
expect "Password:"
send "Karvy123$\r";
expect "prompt"
send "$SPACE\r";
expect "prompt"
EOF
#/bin/bash-x
空格=$(df-h | awk'{print$5}'| grep-v Use | sort-n | tail-1 | cut-d%-f1)
#设置用户“根”
#设置ip“192\.168\.53\.197”
#echo$SPACE
希望哦,我明白了。您需要$SPACE来保存命令,而不是值。此构造将执行命令并将输出保存在变量中:x=$(cmd arg arg)
。因此,您正在查找本地主机上使用的空间,并将该值发送到远程主机
你需要这样的东西:
#!/bin/bash
export cmd="df -h | awk '{ print \$5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1"
remote_usage=$(
expect <<'EOF'
log_user 0
spawn -noecho ssh "root@192.168.53.197"
expect "Password:"
send "*****\r"
expect "prompt"
send "$env(cmd)\r"
expect -re {(?n)^(\d+)$.*prompt}
send_user "$expect_out(1,string)\n"
send "exit\r"
expect eof
EOF
)
echo "disk usage on remote host: $remote_usage"
它有用吗?如果不是,你会得到什么错误?我会得到给定的错误[root@localhost~]#47-bash:47:command-not-found47
从哪里来?我认为该值来自$space,请注意,结束时的“EOF”不能有前导空格:粘贴到此处的shell脚本将无法工作。感谢您的回复…@glenn我收到以下错误-->无法读取“expect_out(buffer)”:执行“send_user”$expect_out(buffer)\n“”时没有此类变量/bin/bash export cmd=“df-h | awk'{print\$5}'| grep-v Use | sort-n | tail-1 | cut-d%-f1”remote_usage=$(expect Useexpect_out(1,string)
而不是expect_out(buffer)
--前者仅为命令输出的数字;后者包括命令、提示等。我使用的代码与您在相同错误后提供的代码相同--->无法读取“expect\u out(1,string)”:执行“send\u user”$expect\u out(1,string)\n时没有此类变量
remote_usage=$( ssh root@192.168.53.197 sh -c "df -h | awk '{ print \$5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1" )