[英]how to use expect inside shell script
I have a bash+expect script which has to connect normal user, i want to read the specific file and store into the variable to be used after while that specific file in root user.
我有一个 bash+expect 脚本,它必须连接普通用户,我想读取特定文件并将其存储到变量中,以便在 root 用户中的特定文件之后使用。 How can i get the value ?
我怎样才能获得价值? My script is:
我的脚本是:
#!/bin/bash
set prompt ">>> "
set command ls /root/test1
expect << EOF
spawn su root
expect "password:"
send "rootroot\r"
expect "$prompt\r"
send "$command\r"
expect "$prompt\r"
expect -re "(.*)\r\n$prompt\r\n"
EOF
echo "$command"
if [ ! -f "$command" ]; then
echo "file is not exist"
else
echo "file is exist"
fi
whenever i'm execute my shell script it show following output:
每当我执行我的 shell 脚本时,它都会显示以下输出:
ls: /root/: Permission denied
ls: /root/: 权限被拒绝
file is not exist
文件不存在
basically test is there but it is showing "file is not exist"
基本上测试在那里,但它显示“文件不存在”
This question is very old but i hope someone gets help from this answer.这个问题很老了,但我希望有人能从这个答案中得到帮助。
--> You should use #!/usr/bin/expect
or #!/bin/expect
to use expect
properly, expect<<EOF
might work but thats not conventional way to write script. --> 您应该使用
#!/usr/bin/expect
或#!/bin/expect
来正确使用expect
, expect<<EOF
可能会起作用,但这不是编写脚本的常规方法。
--> You script should end with EOF
statement . --> 你的脚本应该以
EOF
语句结束。 Ex.前任。
#!/usr/bin/expect << EOF
<some stuff you want to do>
EOF
--> Some basic thing about spawn
. --> 关于
spawn
一些基本知识。 Whatever you write in spawn
will execute but it will not have effect on entire script.您在
spawn
编写的任何内容都会执行,但不会对整个脚本产生影响。 Its not like environment variables.它不像环境变量。
In short, spawn
will start new process and your command is not under spawn process.简而言之,
spawn
将启动新进程并且您的命令不在 spawn 进程中。 Ex.前任。
#!/usr/bin/expect
spawn bash -c "su root '<your-cmd-as-root>'"
<some-expect-send-stuff-etc>
Now in your script, $command
should be write inside spawn
like i showed in above example.现在在你的脚本中,
$command
应该写在spawn
里面,就像我在上面的例子中展示的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.