2017-10-18 46 views
0

我想为我的bash脚本创建一个更新函数,以便将来我所提交给存储在github上的脚本的任何更改都可以由最终用户更新。我找不到任何bash更新函数的例子。如何为bash脚本创建更新功能

,如果我只是做一个函数,像这样:

function update(){ 
    git clone https://path/to/my/repo.git 
} 

只是创建在当前目录下我的回购协议的另一个副本我在直接添加脚本路径,或者没有按 将不起作用。 。我怎样才能做到这一点?

+0

'function'和'()'是多余的。只要写'update(){...}'。 (省略“功能”一词。) –

+0

那个人。即时在我的家庭文件夹。 git update不起作用,因为'update'不是git命令。 @William Pursell。我不能删除单词函数,因为它是我的脚本中的一个函数。我删除了字功能和更新功能不起作用。 – Red5tar

+0

你在找什么可能是'git pull'。或者'git push'。不太清楚你实际要问什么。 –

回答

0

这应该可以正常工作,但正如前面提到的,不要在同一个目录下使用dev和user!

function update { 
    git pull 
} 

最后,如果你需要的情况下,执行特定命令的更新可用,您可以使用类似

function update { 
    if [[ "$(git pull|grep Already)" = "" ]]; then 
     echo "script has just been updated" 
    else 
     echo "no update was available" 
    fi 
} 

此外,有一两件事是可以派上用场,如果你这样做是为了有一个单独的设置文件,你从你的脚本和“.gitignore”源 - 所以人们不必重置他们的设置在每次更新

+0

更好的是编写'if! git pull | grep -q已经; then' – janos

0

如果更新功能需要最终用户在那里更新脚本的副本,这可能是您要查找的内容。

update(){ 
    cd /tmp 
    git clone https://path/to/my/repo.git 
    # cd into the cloned directory if need to reach script 
    mv script.sh /to/location/of/old/script 
    # or 
    rm /location/of/old/script.sh 
    mv script.sh /to/location/of/old/script 
    } 
0

也许你需要git -f pull origin a_branch:tmp_branch强制每次从a_branch新的变化到tmp_branch我第二。

function update { 
    cd the/repo/path 
    git pull -f origin a_branch:tmp_branch 
}