Bash脚本不会导航到文件夹,仅在手动键入“退出”时接受输入

时间:2019-07-01 13:57:01

标签: bash ubuntu

我有以下尝试使用的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时它仍然没有执行。

1 个答案:

答案 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