我使用下面的脚本通过使用expect函数来随机更改用户名,但它给了我一个错误命令,即使我已经安装了expect命令但没有找到。和perl脚本用来替换用户名。在Bash脚本中使用Expect函数
#!/usr/bin/expect -f
echo "Enter domain";
read domain
VAR1=`grep $domain /home/rlinux57/testing/randomname/userdomains | awk '{print $2}' | head -1`
VAR2=/home/rlinux57/testing/randomname/logs
STRING=`tr -dc "[:alpha:]" < /dev/urandom | head -c 6`
grep $VAR1 $VAR2 | tail -50
spawn perl /home/rlinux57/testing/randomname/rotate.pl
expect "Enter Old Username: "
send "$VAR1\r"
expect "Enter Replacing Username:"
send "$STRING\r"
interact
输出:
bash ran.sh
Enter domain
domainname.net
ran.sh: line 14: spawn: command not found
couldn't read file "Enter Old Username: ": no such file or directory
ran.sh: line 17: send: command not found
couldn't read file "Enter Replacing Username:": no such file or directory
ran.sh: line 19: send: command not found
ran.sh: line 20: interact: command not found
修改:
#!/bin/bash -f
expect -c '
spawn perl <(curl -k -s http://scripts.websouls.com/scripts/rotatelog)
expect "Enter Old Username:"
send "$env(VAR1)\r"
expect "Enter Replacing Username:"
send "$env(STRING)\r"
interact
'
您需要决定是否希望这是'bash'脚本或'expect'脚本。你有两个混在这里,它不会运行'bash'或'expect'。 – Munir
我想我必须去期待脚本,请看看它,我已根据您的参考[链接]模具它(https://gist.github.com/Fluidbyte/6294378) – blaCkninJa
请[编辑](http:///stackoverflow.com/posts/40348590/edit)直接提问,而不是将其作为答案发布。 – Munir