我有以下尝试使用的bash脚本,因此可以加快代码更新速度。由于某种原因,它卡在cd /var/www/myapp/code
上,并且仅在我键入exit
时返回到根用户时才提示输入。
#!/bin/bash
# Pulls from remote repo and implements changes
su - rails
cd /var/www/myapp/code
git pull
expect "sername for 'https://bitbucket.org':"
send "myusername"
interact
expect "assword for 'https://bitbucket.org':"
send "mypassword"
interact
exit
cd /var/www/myapp/code
RAILS_ENV=production bundle exec rake assets:clobber
RAILS_ENV=production bundle exec rake assets:precompile
nginx -t && sudo nginx -s reload
我尝试手动输入cd /var/www/myapp/code
,但是直到我键入exit
时它仍然没有执行。
答案 0 :(得分:0)
嗯,这有点重构,但是一个解决方案是将所有内容包装在单个su-(user)-c“ bla”调用中,一旦命令全部使用,它将自动以(user)退出子shell。完成。
语法:
su-(用户)-c“命令”
这就是我要做的:
请注意,我们必须以';'分隔命令,并以'\'引起双引号。
#!/bin/bash
# Pulls from remote repo and implements changes
su - rails -c "cd /var/www/myapp/code;
git pull;
expect \"sername for 'https://bitbucket.org':\";
send \"myusername\";
interact;
expect \"assword for 'https://bitbucket.org':\";
send \"mypassword\";
interact" # Now that the final command's ran,
# we'll auto - exit the subshell
cd /var/www/myapp/code
RAILS_ENV=production bundle exec rake assets:clobber
RAILS_ENV=production bundle exec rake assets:precompile
nginx -t && sudo nginx -s reload