Stephane Gosselin
Stephane Gosselin

Reputation: 9148

Continue / break out of a loop, continue execution

I am very newbish with bash, so any tips/improvements / code-bashing is totally welcome. I am very much intent on improving my skillset.

The issue I have is once option is selected, whatever code is below the select block is not executed. I tried a few alternatives but obviously I am missing something.

#!/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

Upvotes: 1

Views: 281

Answers (1)

Zombo
Zombo

Reputation: 1

I run select a little different. Perhaps not the "right" way, but works for me. Example

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

Source

Upvotes: 1

Related Questions