2016-08-11 76 views
0

我做了一個小小的Bash腳本讓我的生活更輕鬆。但是我遇到了一個我無法修復的問題。輸出顯示得太早

我想
我做了一個小腳本將在一個文件中每個文件被保存/更改時間檢查PHP的錯誤。這是在我不需要每次運行命令的情況下完成的。所以我在第二個屏幕上運行一次Bash腳本,並且每次將我的PHP文件保存在屏幕上時;我自動獲得屏幕2上顯示的最終錯誤。

基本算法

  1. 獲取文件
  2. 比較它給它的散列以前的哈希
  3. 如果不同,則文件被更改/保存:檢查是否存在使用錯誤php -l指令
  4. 打印出來的結果php -l

問題: php -l的結果會在我的代碼要求輸出之前打印出來。

代碼

#!/bin/bash 

#Declaring basic variables here 
fileToCheck="$1" 

oldHash=("") 
checksum=("") 

#Function to get a striped line as long as the terminal width 
function lineAmount { 
    cwidth=`tput cols` 
    lines="" 
    for i in $(seq $(expr $cwidth - 33)); do lines="$lines-";done 
    echo $lines 
} 


#Function to show the actual error 
function showError { 
    msg=$1 
    time=`date +"%c"` 
    l=$(lineAmount) 

    if [ "$msg" == "No" ] 
     then 
      msg="No errors detected." 
    fi 

    printf "\n\n$time $l \n$msg\n" 
} 

#Start-screen------------------------------------------------ 
printf "Starting session for $1 at $time \n$(lineAmount)\n" 
if [ ! -f $1 ] 
    then 
     echo "[Error] File $1 not found." 
     exit 
fi 
printf "\n\n\n" 
#------------------------------------------ 

#Printing out error when file changed 
while true 
    do 
     sleep 0.6 
     checksum=($(sha256sum $fileToCheck)) 
     checksum=${checksum[0]} 

     if [ "$checksum" != "$oldHash" ] 
      then 
       error=$(php -l $fileToCheck) 
       oldHash=$checksum 

       showError $error 
     fi 
done 

測試文件(test.php的):

<?php 
    function foo() { 

    } 
?> 

腳本的輸出:

Starting session for /home/name/Desktop/test.php at 
----------------------------------------------- 





Thu 11 Aug 2016 08:16:15 PM CEST ----------------------------------------------- 
No errors detected. 

現在, test.php的我刪除線4:

<?php 
    function foo() { 


?> 

這當然會給出一個錯誤,我的腳本顯示錯誤:

Starting session for /home/name/Desktop/test.php at 
----------------------------------------------- 





Thu 11 Aug 2016 08:16:15 PM CEST ----------------------------------------------- 
No errors detected. 
PHP Parse error: syntax error, unexpected end of file in /home/name/Desktop/test.php on line 6 


Thu 11 Aug 2016 08:19:37 PM CEST ---------------------------------------------------------------------------------- 
Parse 

但是就像你所看到的,這不是一個很好的輸出。 PHP Parse error: syntax error, unexpected end of file in /home/name/Desktop/test.php on line 6應打印在第二條虛線下方。不低於「找不到錯誤」。 (第一個輸出)。

預期輸出:

Starting session for /home/name/Desktop/test.php at 
----------------------------------------------- 





Thu 11 Aug 2016 08:16:15 PM CEST ----------------------------------------------- 
No errors detected. 


Thu 11 Aug 2016 08:19:37 PM CEST ---------------------------------------------------------------------------------- 
PHP Parse error: syntax error, unexpected end of file in /home/name/Desktop/test.php on line 6 

我嘗試了很多,我試圖改變我的算法了一下,搜索了很多;但它不起作用。 我想這個問題在第51行或第29行。但我真的看不出有什麼問題。

謝謝!

+0

你是如何編輯php文件的?您的編輯器是否可以爲您自動保存? –

+0

@EricRenouf我正在使用Vim,並使用:w函數保存。據我所知,沒有自動保存功能。我也使用Linux Mint上的默認文本編輯器嘗試了它,並得到了相同的結果。 –

回答

4

這是你的問題的精簡和簡化版本:

Why does this print an error message immediately instead of assigning it to the variable?

$ error=$(php -l test.php) 
PHP Parse error: syntax error, unexpected end of file in test.php on line 5 

php -l打印錯誤消息到stderr是一個很好的Unix的公民應該。 $(..)只捕捉標準輸出。

如果你想捕獲輸出和錯誤在一起,你可以使用:

error=$(php -l $fileToCheck 2>&1) 

你也應該引用您的變量,這樣的消息作爲一個參數傳遞,由於您目前已經丟掉最它(shellcheck是有幫助的):

showError "$error" 

做一個好公民,php也返回一個有用的退出代碼,這樣反而試圖匹配爲「否」,看它是否是成功的,你可以檢查狀態直接:

if error=$(php -l $fileToCheck 2>&1) 
then 
    echo "No problems" 
else 
    echo "It failed with these messages: $error" 
fi 
+0

感謝您的幫助和提示!它現在工作完美。不過,你能否再解釋一下'2&1'部分? –

+1

http://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean –