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