2017-10-16 43 views
0

我想在後臺運行的bash腳本中使用notify-send來通知用戶腳本的進度。更具體地說,這是一個當插入USB閃存驅動器並使用ClamAV掃描時自動運行的腳本。通知 - 從一個bash腳本內發送

具體在第30行和第66行。到目前爲止,我沒有任何運氣。有人能給我一些建議/幫助嗎?謝謝。

#!/bin/bash 
#doOnUSBinsert_0.2.sh 
#Author : Totti 
# Make it executable by running 'sudo chmod x doOnUSBinsert_0.2.sh' 


if ! [ -f /etc/udev/rules.d/80-doOnUSBinsert.rules ] 
then  # rule not added 
    cp "$0" /usr/bin/doOnUSBinsert 
    chmod u x /usr/bin/doOnUSBinsert 

# echo 'SUBSYSTEM=="usb", ACTION=="add", RUN ="/path/to/script.sh"' | sudo tee  /etc/udev/rules.d/80-clamscan.rules 
    echo 'SUBSYSTEM=="usb", ACTION=="add", RUN ="/usr/bin/doOnUSBinsert & "' | tee  /etc/udev/rules.d/80-doOnUSBinsert.rules 
    if [ $? -eq 0 ] 
    then 
    echo 'Rule Successfully added. See file "/usr/bin/doOnUSBinsert" if you wish to edit the command' 
    exit 0 
    else 
    echo 'ERROR while adding rule' 
    exit 1 
    fi 
fi 



lfile="/tmp/doOnUSBinsert.log"  # udev 
lfile2="/tmp/clamscanFromUdev.log" # clamscan 
lfile3="/tmp/doOnUSBinsert_mount.log" # mount 

notify-send "USB SCAN ON INSERT" "Currently scanning with ClamAV" 

main() 
{ 
sleep 12 # let the partitions to mount 

    #cat /proc/$$/environ | tr '�' 'n' >> /tmp/udevEnvirn.txt 
echo "found $ID_SERIAL" >> "$lfile" 
    cat /etc/mtab | grep "^$part_c" >> "$lfile.3" 

if [ "$ID_SERIAL"x = 'x' ] 
then 
echo "Exiting on empty ID_SERIAL" >> "$lfile" 
exit 1 
fi 

#Eg: ID_SERIAL --> /dev/disk/by-id/usb-sandisk....42343254343543 
#i=0 
echo 'searching partitions' >> "$lfile" 

for partitionPath in $(find /dev/disk/by-id/ -name "*$ID_SERIAL*part*") 
do 
    echo "current partition = $partitionPath" >> "$lfile" 
# part[i ]="$(readlink -f "$partition")"  # Eg Output: /dev/sdb1  , /dev/sdb2 
    part_c="$(readlink -f $partitionPath)" 
    mpoint="$(cat /etc/mtab | grep "^$part_c" | awk '{print $2}')" 

    echo "partitionPath= $partitionPath, part = $part_c, mountpoint= $mpoint" >>  "$lfile" 

    echo "Scaning --> $mpoint" >> "$lfile.2" 
    ############################################ 
    clamscan -r --bell "$mpoint"/* >> "$lfile.2" 
    ############################################# 
done 
} 

notify-send "USB SCAN ON INSERT" "Finished scanning with ClamAV" 

main & 
echo ______________________________________ >> "$lfile" 
exit 0 

回答

0

根據您如何運行腳本,它可能無法訪問顯示變量。在命令之前嘗試運行export DISPLAY=:0.0

如果你以不同的用戶身份運行腳本,例如root,那麼你可能還需要運行它作爲su - <logged in user> -c notify-send ...(我通常不需要這樣做,但我記得不得不在一點 - 但我不能回想起當時的發行版或版本。)

+0

好的,謝謝 - 它在Ubuntu 16.04.3 LTS上運行Unity:export DISPLAY =:0.0 su - $ USER -c「/ usr/bin /通知發送'USB掃描''當前使用ClamAV掃描USB'「 –

+0

太棒了,很高興它爲你工作。 :) – sotI