xpanta
xpanta

Reputation: 8418

bash scripting if condition error

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?

Upvotes: 0

Views: 166

Answers (2)

Joakim Nohlgård
Joakim Nohlgård

Reputation: 1852

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

Upvotes: 0

dogbane
dogbane

Reputation: 274532

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

Upvotes: 2

Related Questions