1

我对 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
4

1 回答 1

1

我跑select的有点不一样。也许不是“正确”的方式,但对我有用。例子

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

资源

于 2013-07-09T00:57:21.193 回答