0

这是我第一个将我的内容同步到服务器的 bash 脚本。我想将要同步的文件夹作为参数传递。但是,它没有按预期工作。

这是我的脚本(保存为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

这是我的输出:

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

尽管我给出了“模板”作为参数,但它忽略了它。为什么?

4

2 回答 2

2

您需要在操作员的两侧留出一个空间==。将其更改为:

if [ "$var" == "main" ] || [ "$var" == "all" ] ; then
    echo "*** syncing  main ***" ;
    rsync -Paz /home/chris/project/main/ user@remote_host:webapps/project/main/ 
fi
于 2012-11-08T08:44:07.530 回答
0

根据以下评论,这是我建议的更正脚本:

#!/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
于 2012-11-08T08:42:17.230 回答