0

This is my first bash script to sync my contents to the server. I want to pass the folder to be synced as a parameter. However, it does not work as expected.

This is my script (saved as sync.sh):

echo "STARTING SYNCING... PLEASE WAIT!"
var="$1" ;
echo "parameter given is $var"

if [ "$var"=="main" ] || [ "$var"=="all" ] ; then
    echo "*** syncing  main ***" ;
    rsync -Paz /home/chris/project/main/ user@remote_host:webapps/project/main/ 
fi

if [ "$var"=="system" ] || [ "$var"=="all" ] ; then
    echo "*** syncing  system ***" ;
    rsync -Paz /home/chris/project/system/ user@remote_host:webapps/project/system/ 
fi

if [ "$var"=="templates" ] || [ "$var"=="all" ] ; then
    echo "*** syncing  templates ***" ;
    rsync -Paz /home/chris/project/templates/ user@remote_host:webapps/project/templates/ 
fi

And this my output:

chris@mint-desktop ~/project/ $ sh ./sync.sh templates
STARTING SYNCING... PLEASE WAIT!
parameter given is templates
*** syncing  main ***
^Z
[5]+  Stopped                 sh ./sync.sh templates

Although I gave "templates" as an argument, it ignores it. Why?

2 Answers 2

2

You need a space on either side of the == operator. Change it to:

if [ "$var" == "main" ] || [ "$var" == "all" ] ; then
    echo "*** syncing  main ***" ;
    rsync -Paz /home/chris/project/main/ user@remote_host:webapps/project/main/ 
fi
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I used to have spaces around == but I was getting this: STARTING SYNCING... PLEASE WAIT! parameter given is templates ./sync.sh: 5: [: templates: unexpected operator ./sync.sh: 5: [: templates: unexpected operator ./sync.sh: 10: [: templates: unexpected operator ./sync.sh: 10: [: templates: unexpected operator ./sync.sh: 15: [: templates: unexpected operator ./sync.sh: 15: [: templates: unexpected operator ./sync.sh: 20: [: templates: unexpected operator ./sync.sh: 20: [: templates: unexpected operator
0

Based on the comments below, this is my suggested corrected script:

#!/bin/bash
echo "STARTING SYNCING... PLEASE WAIT!"
var="$1" ;
echo "parameter given is $var"

if [ "$var" == "main" -o "$var" == "all" ] ; then
    echo "*** syncing  main ***" ;
    rsync -Paz /home/chris/project/main/ user@remote_host:webapps/project/main/ 
fi

if [ "$var" == "system" -o "$var" == "all" ] ; then
    echo "*** syncing  system ***" ;
    rsync -Paz /home/chris/project/system/ user@remote_host:webapps/project/system/ 
fi

if [ "$var" == "templates" -o "$var" == "all" ] ; then
    echo "*** syncing  templates ***" ;
    rsync -Paz /home/chris/project/templates/ user@remote_host:webapps/project/templates/ 
fi

6 Comments

[ "$var" == "main" || "$var" == "all" ] should be changed to [ "$var" == "main" -o "$var" == "all" ]
or it can be changed to use double brackets i.e. [[ "$var" == "main" || "$var" == "all" ]]
You are correct, I didn't test my script (I was at the wrong computer). I'll make sure to test before I post in the future. I have corrected my solution.
Thanks, I tried that too. But I still get a similar error as the one I get when I apply @dogbane's suggestion: STARTING SYNCING... PLEASE WAIT! parameter given is templates ./sync.sh: 5: [: templates: unexpected operator ./sync.sh: 10: [: templates: unexpected operator ./sync.sh: 15: [: templates: unexpected operator ./sync.sh: 20: [: templates: unexpected operator
Are you really running bash? Did you put #!/bin/bash at the top of the script or run it as bash sync.sh?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.