我使用下面的腳本通過使用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