2015-08-08 59 views
0

我想在電池電量耗盡之前關閉PC並複製一些文件。關閉計算機並使用BASH之前執行某些操作

#!/bin/bash 
LOW=11460 
BAT=`/bin/cat /proc/acpi/battery/BAT1/state | /bin/grep remaining | /usr/bin/awk '{print\$3}'` 
if ["$BAT" \< "$LOW"] 
then 
echo "Turning off" 
rsync folder/ otherfolder/ 
shutdown -h now 
fi 

但它不起作用!

+0

您正在使用哪種發行版? –

+0

即時通訊使用linuxmint 17.1 – Padawan

回答

0

您的語法不正確。在使用[構造時,您不必要地轉義部分代碼,並且測試表達式需要圍繞變量和數字比較的空格。例如: -

#!/bin/bash 
LOW=11460 
BAT=`/bin/cat /proc/acpi/battery/BAT1/state | /bin/grep remaining | /usr/bin/awk '{print $3}'` 
if [ "$BAT" -lt "$LOW" ] 
then 
    echo "Turning off" 
    rsync folder/ otherfolder/ 
    shutdown -h now 
fi 

。假定兩者/bin/usr/bin在你的路,我會做如下修改:

BAT=`cat /proc/acpi/battery/BAT1/state | grep remaining | awk '{print $3}'` 

還可以考慮使用(())作爲測試表達。例如

if ((BAT < LOW)) 

注意:使用(())測試結構時沒有必要取消引用與$您的可變內(())除非使用括號擴展或數組語法空間周圍BATLOW不是必需的,和。例如((${#array[@]} < something))

此外,因爲您要撥打的要求root權限調用shutdown一個腳本,你應該在開始測試root EUID

if ((EUID != 0)); then 
    printf "error: script must be run by root, EUID: '%s' can't.\n" $EUID 
    exit 0 
fi 

,或者如果你喜歡正常[測試結構:

if [ $EUID -ne 0 ]; then 
... 
相關問題