Hi there,
In an attempt to understand getopts better, I wrote a simple function that is a wrapper for rsync. basic stuff.
Now i am trying to make my "-s" a required switch no matter what,
Now when i run a script that calls my below function, the first run goes fine, but then the second instance of my function errors out with the "You have not set the required -s option".
Now if you look in the function, I do have spot that checks for the existence of the -s option..... but from looking at the calling script, you can see that it is there.
Ideas?
FUNCTION
Code:
function dosync() {
SRC=
d=0
p=0
s=0
while getopts "hdp:s:" OPTION
do
case $OPTION in
d)
DEL=$OPTARG
d="1"
;;
h)
usage
exit 1
;;
p)
PORT=$OPTARG
p="1"
;;
s)
SRC=$OPTARG
s=1
;;
?)
usage
exit 1
;;
*)
usage
exit 1
;;
esac
done
if [ $s -ne "1" ]; then
echo "You have not set the required -s option"
exit 1
fi
if [[ "$d" -eq "1" && "$p" -eq "1" ]]; then
rsync -av --delete -p $PORT -e ssh $ID@$IP:$SRC $BUFLD
elif [[ "$d" -eq "1" && "$p" -ne "1" ]]; then
rsync -av --delete -e ssh $ID@$IP:$SRC $BUFLD
elif [[ "$d" -ne "1" && "$p" -eq "1" ]]; then
rsync -av -p $PORT -e ssh $ID@$IP:$SRC $BUFLD
elif [[ "$d" -ne "1" && "$p" -ne "1" ]]; then
rsync -av -e ssh $ID@$IP:$SRC $BUFLD
else
echo "There seems to be an error with your command"
exit 1
fi
}
CALLING (the function) SCRIPT
Code:
dosync -s /some/path
dosync -s /some/other/path
dosync -s /some/path2
dosync -s /some/other/path2