我做了一個小小的Bash腳本讓我的生活更輕鬆。但是我遇到了一個我無法修復的問題。輸出顯示得太早
我想
我做了一個小腳本將在一個文件中每個文件被保存/更改時間檢查PHP的錯誤。這是在我不需要每次運行命令的情況下完成的。所以我在第二個屏幕上運行一次Bash腳本,並且每次將我的PHP文件保存在屏幕上時;我自動獲得屏幕2上顯示的最終錯誤。
基本算法
- 獲取文件
- 比較它給它的散列以前的哈希
- 如果不同,則文件被更改/保存:檢查是否存在使用錯誤
php -l
指令 - 打印出來的結果
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行。但我真的看不出有什麼問題。
謝謝!
你是如何編輯php文件的?您的編輯器是否可以爲您自動保存? –
@EricRenouf我正在使用Vim,並使用:w函數保存。據我所知,沒有自動保存功能。我也使用Linux Mint上的默認文本編輯器嘗試了它,並得到了相同的結果。 –