Bash whiptail 在更改目录时死亡

Bash whiptail dies when changing directory

我正在尝试在我的 whiptail 对话框屏幕中切换到一个目录和 运行 其他命令,但是在克隆我的 git 存储库之后,我的脚本似乎快要死了尝试更改目录时:

STATUS=0
touch log.txt
while [ $STATUS -lt "100" ]; do
  echo "cloning Repo" >> log.txt
  git clone git@git.bitbucket.org:abc/repo.git /repo >> log.txt 2>&1
  echo "changing directory" >> log.txt
  cd /repo >> log.txt 2>&1
  echo `pwd`
  echo "installing bundler" >> log.txt
  gem install bundler --no-ri --no-rdoc >> log.txt 2>&1
  (( STATUS += 99 ))
  echo $STATUS
done | whiptail --gauge "Setting Up Neo (THIS WILL TAKE SOME TIME)..." 40 78 0
;;

使用 set -x 记录命令,这看起来像:

:66+STATUS=0
:67+touch log.txt
:149+whiptail --gauge 'Setting Up (THIS WILL TAKE SOME TIME)...' 40 78 0
:68+'[' 0 -lt 100 ']'
:71+echo 'apt-get update'
:73+((  STATUS += 15  ))
:74+echo 15
:77+echo 'apt-get upgrade'
:79+((  STATUS += 15  ))
:80+echo 30
:83+echo 'apt-get -y git-all'
:85+((  STATUS += 15  ))
:86+echo 45
:111+((  STATUS += 30  ))
:112+echo 75
:115+rm -rf /repo
:116+echo 'cloning Repo'
:117+git clone git@git.bitbucket.org:abcd/repo.git /repo
:118+echo 'changing directory'
:119+cd /repo
::120+pwd
:120+echo /repo
:121+echo 'installing bundler'
:122+gem install bundler --no-ri --no-rdoc
:148+echo 100
:68+'[' 100 -lt 100 ']'
:5+true
::11+whiptail --title 'Configuration Menu' --menu 'Choose an option' 40 78 30 1 'Show current configuration.' 2 'Setup Wizard.' 0 Exit

log.txt 的输出在 changing directory 停止,我的 whiptail 菜单返回主页(好像设置完成了,但不是因为我还应该看到 pwd 和日志中的 installing bundler):

 cloning Repo
 Cloning into '/repo'...
 changing directory

我没有收到任何错误,所以诊断正在发生的事情对我来说是个问题。感谢您的帮助!

因为您在每个命令上使用 >>log.txt,所以每个命令都会重新打开该日志文件——这意味着使用 cd 会更改创建该日志文件的目录.

要解决此问题,在脚本顶部,使用:

exec 3>log.txt

...并且,每当您想写入该文件时,将 >&3 附加到每个命令以写入先前打开的文件描述符。