2017-09-01 84 views
1

我一直在試圖編寫一個shell腳本來檢測虛擬linux = Ubuntu 16.0.4機器上的作曲家和混帳,然後在需要時安裝它們。 +如果機器準備好了,則克隆所需的存儲庫。 現在,這是我第一次嘗試編寫任何類型的腳本,也很抱歉,如果我以某種方式搞砸了這個問題本身,我現在也在使用stackoverflow。在Linux上運行.sh腳本時出現語法錯誤。我究竟做錯了什麼?

任何幫助表示讚賞,在此先感謝。

這是我最初接收到的原始任務說明:

-check如果要是這樣安裝在服務器 混帳,克隆回購與代碼庫

-check如果要是這樣安裝在服務器 作曲家,作曲安裝在laravel應用程序的根目錄

- 最後,PHP工匠遷移--seed

現在,這是我是如何努力實現這一目標:

#!/bin/bash 
echo "The installation process is about the begin..." 


if ! which git; 
    then echo "Git has been located on the destination system. Cloning begins..." 
git clone <link to the gitlabe repo> 

else echo "There is no Git installed on the system. Git installation  commences..." 

     sudo apt-get update 
     sudo apt-get upgrade 
     sudo apt-get install git 

     echo "Cloning begins..." 
     git clone <link to the gitlabe repo> 
fi 

if ! which composer; 
then 
    echo "Composer has been located on the destination system." 
else 
    echo "There is no Composer installed on the system. Composer installation commences..." 
     sudo apt-get update 
     sudo apt-get upgrade 
     sudo apt-get install composer 
fi 

sudo apt-get update 
sudo apt-get install curl php5-cli git 
curl -sS https://getcomposer.org/installer | sudo php -- --install- dir=/usr/local/bin --filename=composer 





composer global require "laravel/installer" 


sudo apt-get update 

'Now the preferred version in the vagrantfile has to be edited to be 1.8.1    instead of 1.9' 

'Generate a ssh key' 
ssh-keygen -t rsa -b 4096 -C "< e-mail adress that I used >" 

'Start ssh agent eval $' 
ssh-agent -s 

'Add your SSH private key to the ssh-agent' 
ssh-add -K ~/.ssh/id_rsa 


php artisan migrate --seed 

錯誤消息我收到:

sudo sh ./testscript.sh 
[sudo] password for linuxtest: 
The installation process is about the begin... 
: not foundt.sh: 3: ./testscript.sh: 
: not foundt.sh: 4: ./testscript.sh: 
: not foundt.sh: 5: ./testscript.sh: 
./testscript.sh: 72: ./testscript.sh: Syntax error: end of file unexpected (expecting "then") 
+1

在hashbang中添加「-x」可以讓你更深入地瞭解正在發生的事情('#!/ bin/bash -x')。其餘部分,「[」和「]」需要空格,如'if [!哪個git];那麼...'。但是這可能是由此處的格式造成的 – Ronald

+1

代碼中存在很多錯誤。嘗試使用https://www.shellcheck.net。 – pacholik

+0

我添加了空格和「-x」但沒有變化,看起來好像是 –

回答

1

,幫助我解決我的問題被張貼查爾斯·達菲在評論答案:

這看起來像你的文件有DOS,而不是UNIX換行。這將防止識別fi這樣的語法,因爲它被讀作fi $'\ r',這不是關鍵字。

0
#!/bin/bash 
echo "The installation process is about the begin..."; 
if [ -x "$(command -v git)" ]; then 
    echo "Git has been located on the destination system. Cloning; begins..." 
    git clone <link to the gitlabe repo>; 
else 
    echo "There is no Git installed on the system. Git installation commences..."; 
    sudo apt-get update && sudo apt-get upgrade && sudo apt-get install git; 

    echo "Cloning begins..."; 
    git clone <link to the gitlabe repo>; 
fi 

if [ -x "$(command -v composer)" ]; then 
    echo "Composer has been located on the destination system."; 
else 
    echo "There is no Composer installed on the system. Composer installation commences..."; 
    sudo apt-get update && sudo apt-get upgrade && sudo apt-get install composer; 
fi 

我想這應該解決您如果條件問題。如果你想知道更多關於如何檢查程序存在的情況,請看這裏: Check if a program exists from a Bash script 它是quickfix的第二個答案。

一定要記住通過「& &」取決於上述命令成功的鏈接命令。這保證瞭如果前面的命令沒有失敗,就會執行下一個命令。

我建議使用ssh命令以相同的方式執行此操作。

@edit 還確保您以分號結尾每個命令。

希望我能幫上忙。

+0

感謝您的答覆,並提供建議 我會盡力按照您的建議進行更正。 –

+0

現在它說文件結束意想不到(期待:fi) –

+0

你試過像這樣啓動它:sudo ./testscript.sh sudo後沒有sh?我首先必須先回家,然後才能做另一個分析unfortunatley:/ – JSStift

相關問題