gpt4 book ai didi

bash - 从 bash 脚本中通知发送

转载 作者:行者123 更新时间:2023-12-04 13:03:30 25 4
gpt4 key购买 nike

我想在后台运行的 bash 脚本中使用通知发送来通知用户脚本的进度。更具体地说,这是一个在插入 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

最佳答案

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

Tip: An overview on the available icons can be found here. To senddesktop notification from a background script running as root (replaceX_user and X_userid with the user and userid running X respectively):

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.'


希望这会帮助其他人。

关于bash - 从 bash 脚本中通知发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46777581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com