我只想编写一个shell脚本,在运行命令时自动提供用户名和密码提示, 我试过这个,但它不起作用。请帮助我解决任何问题。
#!/bin/bash
#!/usr/bin/expect
ls -ltr
spawn sudo git pull
expect "[sudo] password for sandeep:"
send "sandeep123\n"
expect "Username :"
send "sandeep\n"
expect "Password :"
send "sandeep121\n"
我得到以下输出:
sandeep@K2-Sandeep-08:~$ ./checkout.sh
total 64
./checkout.sh: line 8: spawn: command not found
couldn't read file "Username:": no such file or directory
./checkout.sh: line 16: send: command not found
couldn't read file "Password:": no such file or directory
./checkout.sh: line 20: send: command not found
答案 0 :(得分:2)
expect的语法是
expect -c 'spawn sudo git pull; expect assword; send "password\n"; interact'
spawn sudo git pull;
是您的命令,expect assword;
等待assword
出现以及何时expect
发送password
。
答案 1 :(得分:1)
你有两个#!行,但您的脚本只能由其中一个程序一次运行。由于您的大多数命令都是Expect命令,因此请删除#!/bin/bash
行,以便脚本由Expect运行。但是,行ls -ltr
不是Expect命令,会导致错误,但您可以通过将其转换为puts [exec ls -ltr]
来解决此问题。有关详细信息,请参阅the Expect manual page