【发布时间】:2013-02-24 18:46:22
【问题描述】:
我有一个要点,我总是用它来在新服务器上安装我需要的软件包。
http://gist.github.com/4372049
我需要做的就是通过 ssh 在新服务器中输入以下内容
bash -c "$(curl -fsSL https://raw.github.com/gist/4372049)" <mysqlPassword>
我会很高兴的。
现在我有了这一系列的步骤,我总是需要在全新安装的 ubuntu 上执行这些步骤。
first at root i did a
echo $SHELL
I saw that I have /bin/bash
then i switch to www-data
sudo su www-data
then i do a
echo $SHELL
I saw that I had
/bin/sh
instead.
So I did a
chsh -s /bin/bash
I was prompted for my www-data password so I gave it.
Password:
after that I switch back to root
exit
then i log back into www-data
sudo su www-data
I checked the $SHELL again
echo $SHELL
I saw that now it is
/bin/bash
在https://askubuntu.com/a/232663/10591这里列出
有没有办法编写我可以放在 gist.github.com 中的 bash 脚本以类似的方式执行?
如果是这样,我该如何编写 bash 脚本?
更新:
我意识到我获得了结束这个问题的投票,因为它被认为过于本地化。
让我把这个改写成
我如何编写一个可以放在 gist 中并在我的 linux 控制台中使用的 bash 脚本,以便它可以接受用户名和密码的参数,从而执行命令
chsh -s /bin/bash
并正确提供密码?
这是我的尝试:https://gist.github.com/simkimsia/5126919
su 有效,但 chsh 命令无效
更新 2:
我已将脚本更改为
EXPECTEDARGS=1
if [ $# -ne $EXPECTEDARGS -o "x$0" == "x" -o $0 == "bash" ]; then
echo "Usage:"
echo " Parameter 1: your username"
echo " Parameter 2: your password"
exit 1
fi
CHANGESHELL_FOR_USER=$0
PASSWORD_OF_USER=$1
########################################
## switch to another user
## read this https://stackoverflow.com/a/1988255/80353
########################################
sudo -u $CHANGESHELL_FOR_USER -H sh -c "chsh -s /bin/bash"
expect "*?assword:*" {send -- "$PASSWORD_OF_USER\r";}
expect eof
看完how to use a shell script to supply a password when the interface asks for it
和
https://stackoverflow.com/a/1988255/80353
现在问题是在提示输入时以某种方式发送密码
Password:
【问题讨论】:
-
您是否以 root 身份运行脚本? sudo 作为 root 不应该提示输入密码...
-
@Kimvais 谢谢,您的评论为我指出了正确的答案。但直到今天我才把这些点联系起来。我会根据你的评论写出答案。