首页 > 解决方案 > 发送选项后有没有办法让zenity窗口不退出?

问题描述

我有一个 zenity 的 bash 脚本,我最近开始以 root 身份运行。

当以普通用户身份运行时,我没有费心试图让窗口保持不变,因此对于每个选定的选项,我只需"$0" &在命令前添加一个,然后 zenity 窗口将始终重新打开。

但是现在当以root身份运行时,无论我尝试什么,窗口都不会重新打开。包括像“sh -c sudo bash $0”这样的轮播仍然无法正常工作。

剧本是这样的。它在获取 root 的快捷方式中使用“kdesu -c sudo”执行。是的,里面有很多肮脏的黑客:)

#!/bin/bash

#runs by default as root

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

user=$(who | awk 'FNR == 1 {print $1}')

h=$(realpath /home/$user)

width=$(xrandr | fgrep '*' | awk '{print $1}' | awk -Fx '{print $1}')

s1="--width=$(($width/2)) --height=$(($width/2))"

s="--width=$(($width/3)) --height=$(($width/3))"

dt=$(date '+%d-%b-%Y-%H:%M:%S')

files="$h/.local $h/.config $h/.ipfs $h/.Osmos $h/.MuWire $h/Documents/Notes $h/Documents/Programs $h/Pictures $h/.asoundrc $h/.ssr/settings.conf /tmp/packages.txt /root/.config/bleachbit /root/.stereo_tool.rc /etc/pulse /etc/modules-load.d /etc/default/grub"

selection=$(zenity --list "System - Rerun IPSets & Hosts" "System - Reset IPSets & Hosts" "System - Mount ISO" "Hardware - Zenmonitor" "Hardware - AMDGPU Monitor" "Hardware - IO Monitor" "Hardware - SMART SSD" "Hardware - SMART HDD" "Hardware - Turn off HDD" "Memory - Enable Swap" "Memory - Free up cached memory" "Memory - Disable swap" "CPU - Set PState 4.0 GHz" "CPU - Set PState 3.8 GHz" "CPU - Set PState 3.6 GHz" "CPU - Set PState 3.3 GHz" "CPU - Set PState 2.9 GHz" "CPU - Set PState 1.7 GHz" "Diagnostics - RAM Info" "Diagnostics - Run Memtester" "Maintenance - Backup home dir using tar/pigz (fast)" "Maintenance - Backup home dir using tar/lrzip (best compression)" "Maintenance - Backup home dir using 7z (old, more compatible)" "Misc - PcManFM (root)" "Misc - Hardware Info (root)" "Misc - Bleachbit" "Misc - Bleachbit (root)" "Misc - Restart pulseaudio" "Misc - Turn off display" --column="Sort" --text="Choose desired option:" --title="System Utilities" $s)

if test "$selection" = "System - Rerun IPSets & Hosts"
then
"$0" & sleep 0.5s && bash "$DIR/IPsets & Hosts.sh" | zenity --text-info $s

elif test "$selection" = "System - Reset IPSets & Hosts"
then
"$0" & iptables -F & ip6tables -F && ipset flush && ipset destroy ipmaster && ipset destroy ipmaster6 & rm -f /etc/hosts && systemctl restart nscd.service

elif test "$selection" = "System - Mount ISO"
then
if test -d /mnt/iso
then
:
else
mkdir /mnt/iso
fi
if ls -1qA /mnt/iso | grep -q .
then
! umount /mnt/iso
else
:
fi
user=$(who | awk 'FNR == 1 {print $1}')
file=$(zenity --file-selection --title="Select ISO File")
if test -z "$file"
then
exit
else
mount -o loop ${file} /mnt/iso
fi

elif test "$selection" = "Hardware - Zenmonitor"
then
"$0" & sleep 0.5s && zenmonitor

elif test "$selection" = "Hardware - AMDGPU Monitor"
then
"$0" & sleep 0.5s && konsole --nofork -e watch -t head -n 15 /sys/kernel/debug/dri/0/amdgpu_pm_info

elif test "$selection" = "Hardware - IO Monitor"
then
"$0" & sleep 0.5s && konsole --nofork -e iotop

elif test "$selection" = "Hardware - SMART SSD"
then
"$0" & sleep 0.5s && smartctl -a /dev/nvme0 | zenity --text-info $s1

elif test "$selection" = "Hardware - SMART HDD"
then
"$0" & sleep 0.5s && smartctl -a /dev/sda | zenity --text-info $s1

elif test "$selection" = "Hardware - Turn off HDD"
then
"$0" & umount /dev/sda1 && udisksctl power-off -b /dev/sda

elif test "$selection" = "Memory - Enable Swap"
then
"$0" & sleep 0.5s && fallocate -l $(zenity --scale --value="2" --min-value="2" --max-value="16" --text='Choose Swap size in GB' --title='Swap')G ~/.swapfile && chmod 600 ~/.swapfile && mkswap ~/.swapfile && swapon ~/.swapfile

elif test "$selection" = "Memory - Free up cached memory"
then
"$0" & sync && su -c "sysctl -w vm.drop_caches=3"

elif test "$selection" = "Memory - Disable swap"
then
"$0" & swapoff ~/.swapfile && chmod 777 ~/.swapfile && rm -f ~/.swapfile

elif test "$selection" = "CPU - Set PState 4.0 GHz" #1.306
then
"$0" & sleep 0.5s && $DIR/zenstates.py -p0 --enable -f a0 -d 8 -v 27 | zenity --text-info $s

elif test "$selection" = "CPU - Set PState 3.8 GHz" #1.17
then
"$0" & sleep 0.5s && $DIR/zenstates.py -p0 --enable -f 98 -d 8 -v 3d | zenity --text-info $s

elif test "$selection" = "CPU - Set PState 3.6 GHz" #1.09
then
"$0" & sleep 0.5s && $DIR/zenstates.py -p0 --enable -f 90 -d 8 -v 49 | zenity --text-info $s

elif test "$selection" = "CPU - Set PState 3.3 GHz" #1
then
"$0" & sleep 0.5s && $DIR/zenstates.py -p0 --enable -f 84 -d 8 -v 58 | zenity --text-info $s

elif test "$selection" = "CPU - Set PState 2.9 GHz" #0.9
then
"$0" & sleep 0.5s && $DIR/zenstates.py -p0 --enable -f 74 -d 8 -v 68 | zenity --text-info $s

elif test "$selection" = "CPU - Set PState 1.7 GHz" #0.65
then
"$0" & sleep 0.5s && $DIR/zenstates.py -p0 --enable -f 44 -d 8 -v 90 | zenity --text-info $s

elif test "$selection" = "Diagnostics - RAM Info"
then
"$0" & sleep 0.5s && dmidecode --type 17 | zenity --text-info $s1

elif test "$selection" = "Diagnostics - Run Memtester"
then
"$0" & sleep 0.5s && konsole --nofork -e memtester $(zenity --scale --value="8" --min-value="2" --max-value="14" --text='Choose Memory test size in GB' --title='Memtester')G

elif test "$selection" = "Maintenance - Backup home dir using tar/pigz (fast)"
then
"$0" & pacman -Qe | tee /tmp/packages.txt && tar -I "pigz -9 -p 16 -v -f" -cf $(zenity --list "/mnt/HDD1/Backups/" "/mnt/veracrypt1/" --column="Sort" --text="Choose desired option:" --title="Backup path" $s)home-${dt}.tar.gz $files && rm /tmp/packages.txt

elif test "$selection" = "Maintenance - Backup home dir using tar/lrzip (best compression)"
then
"$0" & pacman -Qe | tee /tmp/packages.txt && tar -I "lrzip -v -L 6 -p 16 -N 0" -cf $(zenity --list "/mnt/HDD1/Backups/" "/mnt/veracrypt1/" --column="Sort" --text="Choose desired option:" --title="Backup path" $s)home-${dt}.tar.lrz $files && rm /tmp/packages.txt

elif test "$selection" = "Maintenance - Backup home dir using 7z (old, more compatible)"
then
"$0" & pacman -Qe | tee /tmp/packages.txt && 7z a $(zenity --list "/mnt/HDD1/Backups/" "/mnt/veracrypt1/" --column="Sort" --text="Choose desired option:" --title="Backup path" $s)home-${dt}.7z $files && rm /tmp/packages.txt

elif test "$selection" = "Misc - PcManFM (root)"
then
"$0" & sleep 0.5s && pcmanfm

elif test "$selection" = "Misc - Hardware Info (root)"
then
"$0" & sleep 0.5s && hardinfo

elif test "$selection" = "Misc - Bleachbit"
then
"$0" & sleep 0.5s && /bin/su -s /bin/bash -c 'bleachbit' $user

elif test "$selection" = "Misc - Bleachbit (root)"
then
"$0" & sleep 0.5s && bleachbit

elif test "$selection" = "Misc - Restart pulseaudio"
then
"$0" & sleep 0.5s && /bin/su -s /bin/bash -c 'pulseaudio -k' $user

elif test "$selection" = "Misc - Turn off display"
then
"$0" & sleep 0.5s && xset dpms force off

fi

exit

是否有任何解决方法,或者像这样易于使用的 zenity 的替代方法?

用什么代替zenity?一个快速的 Qt 程序?

标签: bashzenity

解决方案


推荐阅读