少なくとも3つのコマンドを切り替えるスクリプト

少なくとも3つのコマンドを切り替えるスクリプト

キーバインディングで呼び出すときにさまざまなコマンド(たとえば、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]}"

おすすめ記事