来自bash脚本

时间:2017-10-16 19:17:59

标签: bash shell notify-send

我想在后台运行的bash脚本中使用notify-send来通知用户脚本的进度。更具体地说,这是一个插入USB闪存驱动器并使用ClamAV进行扫描时自动运行的脚本。

特别是在第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

2 个答案:

答案 0 :(得分:0)

根据您运行脚本的方式,它可能无法访问显示变量。尝试在命令之前运行export DISPLAY=:0.0

如果您以不同的用户(即root)运行脚本,那么您可能还需要将其作为su - <logged in user> -c notify-send ...运行(我通常不需要这样做,但我记得必须在一点 - 但我不记得当时我在哪个发行版或版本。)

答案 1 :(得分:0)

我对linux世界还很陌生,但是在寻找类似项目的解决方案时,我发现THIS

提示:有关可用图标的概述,请参见此处。发送 来自以root用户身份运行的后台脚本的桌面通知(替换 X_user和X_userid,其中user和userid分别运行X):

sudo -u X_user DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/X_userid/bus notify-send 'Hello world!' 'This is an example notification.'

希望这对其他人有帮助。