继续/中断循环,继续执行

时间:2013-07-08 20:53:29

标签: bash

我对bash很新意,所以任何提示/改进/代码抨击都是非常受欢迎的。我非常想提高自己的技能。

我遇到的问题是一旦选择了选项,则不执行选择块下面的任何代码。我尝试了一些替代方案,但显然我错过了一些东西。

#!/bin/bash
PROJECT="$1"
USER=sgosselin
LOCAL_DIR=/mnt/
REMOTE_PROJECTS_PATH=/var/test_www
REMOTE_HOST="test-sandbox-01.dev1.in.example.com"
WEB_USER=www-data

declare -A projects
projects=([proj1]=proj1_dir [proj2]=proj2_dir [quit]=quit)
PROJECT_LIST="proj1 proj2 quit"

# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run with sudo." 1>&2
   exit 1
fi

 # Should not have anything to change below
echo "/nSelect project you wish to rsync\n\n"

select PROJECT in $PROJECT_LIST
do
  if [ "$PROJECT" = "quit" ]; then
    echo
    echo  "Quitting $0"
    echo 
    exit
  fi
  for k in "${!projects[@]}"
  do 
    if [ "$k" = "$PROJECT" ]
    then
      PROJECTDIR=${projects[$k]}
      continue
    fi
  done
  echo "Rsynching $PROJECT from $REMOTE_HOST into" $LOCAL_DIR$PROJECTDIR
  rsync -azrP $USER@$REMOTE_HOST:$REMOTE_PROJECTS_PATH/$PROJECT/files/ $LOCAL_DIR$PROJECTDIR

  # Switch to files dir, chown to web user
  cd $LOCAL_DIR
  chown -R $WEB_USER:$WEB_USER . $PROJECTDIR
  break
done
# This never gets executed!
echo "Rsync done."
exit

1 个答案:

答案 0 :(得分:1)

我运行select有点不同。也许不是“正确”的方式,但对我有用。实施例

select foo in higher lower
do
  break
done
if [ $foo = higher ]
then
  (( lb = k ))
else
  (( ub = k ))
fi

Source