如何编写一个 bash 脚本,当使用键绑定调用时,该脚本将在各种命令之间切换(例如,下面显示的 4 个不同命令)。
该脚本应该记住上次调用它时停止在哪个命令上,并且能够在至少三个命令之间切换。
feh
例如,我想使用如下所示在四个屏幕壁纸之间切换,我该怎么做?
feh --bg-scale $dir_photos/20150620_182419_1.jpg
feh --bg-scale $dir_photos/20150620_182419_2.jpg
feh --bg-scale $dir_photos/20150620_182419_3.jpg
feh --bg-scale $dir_photos/20150620_182419_4.jpg
答案1
据我了解您的问题,您希望同一命令在每次调用时产生不同的输出,以便您按顺序循环遍历四个不同的文件。
为此,您需要维护状态,以便知道下一次调用需要什么。在我的示例中,我将保存当前壁纸的索引号 (0..3)。当索引号达到可用文件的数量时,它会使用模 ( %
) 运算符重置为零。
#!/bin/bash
#
files=(20150620_182419_1.jpg 20150620_182419_2.jpg 20150620_182419_3.jpg 20150620_182419_4.jpg)
dir_photos="$HOME/Pictures" # Directory of photos
state="$HOME/.${0##*/}.dat" # File to maintain state
index=$(cat "$state" 2>/dev/null) # Retrieve last index
nFiles=${#files[@]} # Number of entries in files()
# Set current index to zero if first time, otherwise next value in sequence
[[ -z "$index" ]] && index=0 || index=$((++index % nFiles))
printf "%d\n" $index >"$state" # Save new index
# echo "State index=$index: ${files[index]}"
# Apply the wallpaper
feh --bg-scale "$dir_photos/${files[index]}"
将其保存为文件并使其可执行。然后绑定你的密钥来调用这个脚本。
您可以修改它以迭代$dir_photos
目录中的所有文件:
dir_photos="$HOME/Pictures" # Directory of photos
files=("$dir_photos"/*) # All files in the directory
...
feh --bg-scale "${files[index]}"
答案2
这是我的解决方案。我想我可以在后台运行这个小循环,并将每个选项连接到一个热键,这是我的初衷,但我不知道如何通过热键连接到后台进程。
#!/bin/bash
# set an infinite loop until option is set and quit
while :
do
clear
# display menu
echo " --------------------------------------------------"
echo " W A L L P A P E R C H A N G E R - M E N U"
echo " --------------------------------------------------"
echo
echo " 1 wall paper 1"
echo
echo " 2 edit wall paper 1"
echo
echo " 3 wall paper 2"
echo
echo " 4 wall paper 3"
echo
echo " 5 wall paper changer - does not work yet"
echo
echo " 6 this script: pcmanfm to dir location"
echo
echo " 7 this script: edit this script"
echo
echo " 8 display network connections"
echo
echo " q exit"
echo
echo
# set-up reference directories
dir_z="/home/$USER/Dropbox/script_screen_SAVER_feh"
dir_photos="/home/$USER/wall_papers"
# get input from the user
read -p " enter your choice [ 1 - 7 or q/Q ] " choice
# make decision using case..in..esac
case $choice in
1)
feh --bg-scale $dir_photos/20210620_182419_3.jpg
echo "wall paper 1"
exit 0
;;
2)
gimp $dir_photos/20210620_182419_3.xcf
echo "wall paper 1, edit image"
;;
3)
feh --bg-scale $dir_photos/20210620_182419_1.jpg
echo "Quotes"
exit 0
;;
4)
feh --bg-scale $dir_photos/20210621_135308.jpg
echo "The sea - wall paper"
exit 0
;;
5)
# kill previously running wallpaper changer process
ps aux | grep "feh/" | awk '{print $2}' | xargs kill -15
# change the wallpaper every 5 seconds
bash -c 'while : ;do feh --randomize --bg-fill --auto-rotate $dir_photos/*.jpg; sleep 5; done' &
disown
echo "wall paper changer"
exit 0
;;
6)
pcmanfm $dir_photos
echo "pcmanfm directory"
;;
7)
nvim $dir_z/more_than_3_screens_switcher.sh
echo "editing this script"
;;
8)
netstat -nat
read -p "Press [Enter] key to continue..." readEnterKey
;;
q)
echo "Bye!"
exit 0
;;
Q)
echo "Bye!"
exit 0
;; *)
echo "Error: Invalid option..."
;;
esac
done