1

I am writing script to change user from root to poletst (another user ). When I change user server ask me among three areas from which I have to select by pressing 1,2 or 3

I am writing a shell script to automate some step and there is a step which involve changing user and selecting area 1 by default.

su - poletst
1

It is not working. It takes me to the user but doesn't change the area. How to perform this?

4

1 回答 1

2

你可以这样做:

expect -c 'spawn su - poletst; send "1\r"; interact'

或者如果它不起作用,试试这个:

expect -c 'spawn su - poletst; expect "prompt"; send "1\r"; interact'

只需替换prompt为当您被要求提供该区域时显示给您的消息的最后一行中的一个字符串。

您也可以将其作为脚本放置

#!/usr/bin/env expect -f
spawn su - poletst
expect "prompt"
send "1\r"
interact

将其保存到文件中,su-poletst.exp然后运行expect -f su-poletst.exp​​.

于 2013-09-16T13:09:27.453 回答