2017-10-18 22 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 
}