
キーバインディングで呼び出すときにさまざまなコマンド(たとえば、4つの異なるコマンドなど)を切り替えるbashスクリプトを作成する方法。
スクリプトは、最後に呼び出されたときにどのコマンドが停止したかを覚え、少なくとも3つのコマンドを切り替えることができるはずです。
feh
たとえば、次のようにして4つの画面の壁紙を切り替えたいとします。どうすればいいですか?
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
あなたの質問を理解したら、同じコマンドが呼び出されるたびに異なる出力を生成して、4つの異なるファイルを順番に繰り返すことを望みます。
これを行うには、次の呼び出しに必要なものがわかるように状態を維持する必要があります。私の例では、現在の壁紙のインデックス番号(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