2014-10-09 56 views
-1

我有一个bash脚本从Heroku获取信息,以便我可以将我的数据库的一个副本。该脚本在cygwin中正常工作。但是在cron中运行它会停止,因为它使用的shell通过Heroku Toolbelt作为Heroku的身份验证暂停。期望失败,但我不明白为什么

这里是我的crontab:

SHELL=/usr/bin/bash 
5 8-18 * * 1-5 /cygdrive/c/Users/sam/work/push_db.sh >>/cygdrive/c/Users/sam/work/output.txt 

我已经阅读了谷歌和Cygwin中的手册页想出这个加法:

#!/usr/bin/bash 
. /home/sam.walton/.profile 
echo $SHELL 
curl -H "Accept: application/vnd.heroku+json; version=3" -n https://api.heroku.com/ 
#. $HOME/.bash_profile 
echo `heroku.bat pgbackups:capture --expire` 
#spawn heroku.bat pgbackups:capture --expire 
expect { 
    "Email:" { send -- "$($HEROKU_LOGIN)\r"} 
    "Password (typing will be hidden):" { send -- "$HEROKU_PW\r" } 
    timeout { echo "timed out during login"; exit 1 } 
} 
sleep 2 
echo "first" 
curl -o latest.dump -L "$(heroku.bat pgbackups:url | dos2unix)" 

下面是从output.txt的

输出
/usr/bin/bash 
{ 
    "links":[ 
    { 
     "rel":"schema", 
     "href":"https://api.heroku.com/schema" 
    } 
    ] 
} 
Enter your Heroku credentials. Email: Password (typing will be hidden): Authentication failed. Enter your Heroku credentials. Email: Password (typing will be hidden): Authentication failed. Enter your Heroku credentials. Email: Password (typing will be hidden): Authentication failed. 

正如你所看到的,看起来输出没有得到send命令的结果,因为它是ap梨正在等待。我已经完成了许多有关证书和期望陈述的实验。一切都停在这里。我看过几个例子,并试图尝试这些,但我得到模糊的眼睛,这就是为什么我在这里发布。我不了解什么?

感谢您的意见,我想起明确地把我的ENV变量在.bashrc中:每@迪内希的很好的例子是低于

[[ -s $USERPROFILE/.pik/.pikrc ]] && source "$USERPROFILE/.pik/.pikrc" 
export HEROKU_LOGIN=myEmailHere 
export HEROKU_PW=myPWhere 

我的修订后的脚本:

. /home/sam.walton/.bashrc echo $SHELL echo $HEROKU_LOGIN curl -H "Accept: application/vnd.heroku+json; version=3" -n https://api.heroku.com/ 

expect -d -c " spawn heroku.bat pgbackups:capture --expire --app gw-inspector expect { 
    "Email:" { send -- "myEmailHere\r"; exp_continue} 
    "Password (typing will be hidden):" { send -- "myPWhere\r" } 
    timeout { puts "timed out during login"; exit 1 } } " sleep 2 echo "first" 

这应该工作但尽管变量的回显失败,给我提供了一个线索,说明该变量没有被调用,但我正在测试直接对变量进行硬编码以将其作为变量消除。但正如你可以通过我的输出所看到的那样,不仅回声没有产生任何效果,也没有任何线索表明任何诊断正在通过,这让我怀疑脚本是否被称为从期望运行,以及产生的结果命令。重申一下,heroku.bat命令在预期关闭之外工作,但结果在上面。直接在上面的命令的结果是:

/usr/bin/bash 

{ 
    "links":[ 
    { 
     "rel":"schema", 
     "href":"https://api.heroku.com/schema" 
    } 
    ] 
} 

我做错了什么,会告诉我诊断说明?

+0

'HEROKU_LOGIN'和'HEROKU_PW'是否是'.bashrc'文件中声明的环境变量? – Dinesh 2014-10-10 08:38:34

回答

0

如果您打算在您的bash脚本中使用expect代码,而不是单独调用它,那么您应该使用-c标志选项。

从你的代码中,我假设你已经在bashrc文件中声明了环境变量HEROKU_LOGIN和HEROKU_PW。

#!/usr/bin/bash 

#Your code here 

expect -c " 
      spawn <your-executable-process-here> 
      expect { 
        # HEROKU_LOGIN & HEROKU_PW will be replaced with variable values. 

        "Email:" { send -- "$HEROKU_LOGIN\r";exp_continue} 
        "Password (typing will be hidden):" { send "$HEROKU_PW\r" } 
        timeout { puts"timed out during login"; exit 1 } 
      } 
" 
#Your further bash code here 

expect代码,则不应使用echo命令。改为使用puts。在expect代码中产生进程的选项将比在外部产生更强大。请注意,expect -c标志使用双引号。如果您使用单引号,则bash脚本不会执行任何形式的替换。所以,如果你需要bash变量替换,你应该使用-c标志的expect的双引号。

了解-c标志的使用,看看here

如果您还有什么问题,您可以通过以下方式追加-d调试。

expect -d -c " 
       our code here 
     " 
+0

是的,我敢肯定你是对的,但我必须做错事,因为我的例子没有显示输出。 – sam452 2014-10-13 15:56:20

相关问题